SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 33
  1. #1
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Simple Syntax Question

    The following script displays text from my database on my page:

    PHP Code:
    echo $sub_row[0]; 
    I'm just trying to figure out how to insert it into my dynamic table. Below is an example of a row that works fine:

    PHP Code:
    $block .= "<td'>{$row["Area"]}</td>"
    So how can I replace {$row["Area"]} with echo $sub_row[0];?...

    PHP Code:
    $block .= "<td'>echo $sub_row[0];</td>"
    I've tried it with and without "echo," with various combinations of quotes, ' . . ', etc.

    Thanks.

  2. #2
    SitePoint Addict
    Join Date
    Apr 2001
    Location
    Michigan
    Posts
    284
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $block .= '<td>' $sub_row[0] . '</td>'
    That should create a table cell with the contents of whatever text is in the $sub_row[0] variable.

    Hope this helps.

  3. #3
    SitePoint Evangelist dmsuperman's Avatar
    Join Date
    Feb 2005
    Location
    A box
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $block .= "<td>".$sub_row[0]."</td>";
    Something to that effect.
    <(^.^<) \(^.^\) (^.^) (/^.^)/ (>^.^)>
    Core 2 Duo E8400 clocked @ 3.375GHz, 2x2GB 800MHz DDR2 RAM
    5x SATA drives totalling 2.5TB, 7900GS KO, 6600GT

  4. #4
    SitePoint Addict
    Join Date
    Apr 2001
    Location
    Michigan
    Posts
    284
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Beat you to the punch

  5. #5
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by trhynard
    PHP Code:
    $block .= '<td>' $sub_row[0] . '</td>'
    That should create a table cell with the contents of whatever text is in the $sub_row[0] variable.

    Hope this helps.
    I already tried that and was surprised that it didn't work. It just displays this:

    ' . . '

    I also tried these variations:

    '. .', " . . ", ". ."

    With double quotes, it displays nothing. With single quotes, it displays ' . . ' or '. . '.

    Here's more of the code, to put everything in context:

    PHP Code:
    $block .= "<tr>";
    $block .= "<td>' . $sub_row[0] . '</td>";
    $block .= "<td'><a href='$LinkSymbol'>{$row['Order']}</a> ({$row['OrderCommon']})</td>";
    $block .= "</tr>"
    Thanks.

  6. #6
    *munch munch* killerkooki's Avatar
    Join Date
    Sep 2004
    Location
    Singapore
    Posts
    405
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Looks like your sub_row[0] is empty.
    Otherwise with the code you showed, " . sub_row[0] . " must work.
    Try echoing or print_r or something to see if sub_row[0] actually contains a value.

  7. #7
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by killerkooki
    Looks like your sub_row[0] is empty.
    Otherwise with the code you showed, " . sub_row[0] . " must work.
    Try echoing or print_r or something to see if sub_row[0] actually contains a value.
    That's odd; I could have sworn I tried " . sub_row[0] . ", but I tried it again just now and got this error:

    Parse error: syntax error, unexpected '[' in C:\sites\GeoSymbols\a1\articles\ani\tableclass.php on line 133

    I have echoed sub_row[0], and it did display text.

    Thanks.

  8. #8
    SitePoint Wizard Dylan B's Avatar
    Join Date
    Jul 2004
    Location
    NYC
    Posts
    1,150
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Geosite, show us exactly what you using now.

  9. #9
    SitePoint Evangelist dmsuperman's Avatar
    Join Date
    Feb 2005
    Location
    A box
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by geosite
    PHP Code:
    $block .= "<tr>";
    $block .= "<td>' . $sub_row[0] . '</td>";
    $block .= "<td'><a href='$LinkSymbol'>{$row['Order']}</a> ({$row['OrderCommon']})</td>";
    $block .= "</tr>"
    Try this:
    echo("<-".$sub_row[0]."->");
    And tell us what you get from it. If you get only <--> then $sub_row[0] is empty.
    <(^.^<) \(^.^\) (^.^) (/^.^)/ (>^.^)>
    Core 2 Duo E8400 clocked @ 3.375GHz, 2x2GB 800MHz DDR2 RAM
    5x SATA drives totalling 2.5TB, 7900GS KO, 6600GT

  10. #10
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry it took me so long to reply. I've been working and reworking this page, and I had to reconstruct it.

    Anyway, I pasted the entire source at the end of this post, but here are the important parts:

    QUERY

    PHP Code:
        $res mysql_query("SELECT DISTINCT(`Order`) FROM symbols WHERE Class = '$myname'");
        while (
    $row mysql_fetch_row($res))
        {
          
    //echo $query[0]; // Your individual Orders
              
    $sub_res mysql_query("SELECT Area FROM symbols WHERE `Order`='$row[0]'");
              while (
    $sub_row mysql_fetch_row($sub_res)) 
    ECHO STATEMENT

    PHP Code:
    {
     echo 
    $sub_row[0];

    This echo statement does work. It displays the following:

    QuebecJapanAlabamaAlabamaArizonaCaliforniaColoradoDelawareFloridaGeorgiaIdahoIllinoisKentuckyMarylandMinnesotaMississippiMontanaNew HampshireNew MexicoOklahomaOregonSouth CarolinaTennesseeTexasVermontVirginiaWest Virginia

    (At the moment, I'm looking at my Insects page, class Insecta, rather than mammals. But they use the same database, query, etc.)

    Here's the line where I'm trying to get it to "echo" in the table:

    PHP Code:
    $block .= "<td class = \"tdname\" style='background-color:" $color_sets[$c][0] . "'>' . sub_row[0] . '</td>"
    This just displays the following:

    ' . sub_row[0] . '

    If I change it to double quotes... " . sub_row[0] . " ...I get this error message:

    Parse error: syntax error, unexpected '[' in C:\sites\GeoSymbols\a1\articles\ani\tableclass.php on line 101

    Also, I now have this page online at http://geosymbols.org/animals/insects/ It's actually a different script, but the display is virtually identical to what I see on my local site right now.

    The top row displays Delaware | Coleoptera

    Instead, it should display something like Delaware, Massachusetts, New York, etc. | Coleoptera

    I think I'm getting close. I'm just making some stupid mistake somewhere or other.

    Thanks.

    PHP Code:
    <?php
    switch ($Animals['SymClass']) {
    case 
    'animals':
    $colors = array( '#f4eec2''' );
    $n=0;
    $size=count($colors);
    break;
    case 
    'plants':
    $colors = array( '#80ff80''''#bfffbf''' );
    $n=0;
    $size=count($colors);
    break;
    default:
    break;
    }

    switch (
    $Animals['SymClass']) {
    case 
    'animals':
    $BG '#900';
    $n=0;
    $size=count($colors);
    break;
    case 
    'plants':
    $BG '#090';
    $n=0;
    $size=count($colors);
    break;
    default:
    break;
    }

        
    $res mysql_query("SELECT DISTINCT(`Order`) FROM symbols WHERE Class = '$myname'");
        while (
    $row mysql_fetch_row($res))
        {
          
    //echo $query[0]; // Your individual Orders
              
    $sub_res mysql_query("SELECT Area FROM symbols WHERE `Order`='$row[0]'");
              while (
    $sub_row mysql_fetch_row($sub_res))
    {
     echo 
    $sub_row[0]; // Your location for this Order


        
    $block "<table class=\"sortphp_ref\" id=\"Table2\" style=\"margin: 0px auto 25px; border-bottom: 1px solid #000; font-size: 85%;\">";
        
    $block .="<thead>
    <tr><th style=\"background: #666; color: #fff;\">Places</th>
    <th style=\"background: 
    $BG; color: #fff;\">Orders</th></tr>
    </thead>
    <tbody>"
    ;
        
        
    // Look at this as rows & columns of a table,
        // the colors will alternate just like to do in this list.
    switch ($Animals['SymClass']) {
    case 
    'animals':
    // INTERNAL SWITCH
    switch ($Animals['ClassCommon']) {
    case 
    'fishes':
    $color_sets = array(
     array(
    '#eee''#cff'),
     array(
    '',''),
        );
    break;
    default:
    $color_sets = array(
     array(
    '#eee''#f4eec2'),
     array(
    '',''),
        );
    break;
    }
    // END INTERNAL SWITCH
    break;

    case 
    'plants':
    $color_sets = array(
     array(
    '#eee''#80ff80'),
     array(
    '',''),
     array(
    '#eee''#bfffbf'),
     array(
    '',''),
        );
    break;
    default:
    break;
    }
        
    // Count colors, used to determine which color set to use.
        
    $cset_size sizeof($color_sets);
        
        
    $i 0;
        while (
    $row mysql_fetch_array ($res)) {
            
    // Determine color set
            
    $c $i $cset_size;

    switch (
    $Animals['SymClass']) {
    case 
    'animals':
    $LinkSymbol '/animals/' strtolower($row["ClassCommon"]) . '/' strtolower(substr($row["Order"],0,3)) . '/';
    break;
    case 
    'plants':
    $LinkSymbol '/plants/' strtolower($row["ClassCommon"]) . '/' strtolower(substr($row["Order"],0,3)) . '/';
    break;
    default:
    break;
    }
      
    $block .= "<tr>";
      
    $block .= "<td class = \"tdname\" style='background-color:" $color_sets[$c][0] . "'>' . sub_row[0] . '</td>";
      
    $block .= "<td class = \"tdsymbol\" style='background-color:" $color_sets[$c][1] . "'><a href='$LinkSymbol'>{$row['Order']}</a> ({$row['OrderCommon']})</td>";
      
    $block .= "</tr>";
      
    $i++;
        }
      
    $block .= "</tbody></table>";
        
        echo 
    $block;
    }
    ?>

  11. #11
    SitePoint Wizard Dylan B's Avatar
    Join Date
    Jul 2004
    Location
    NYC
    Posts
    1,150
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $block .= '<tr>'
    $block .= '<td>' $sub_row[0] . '</td>'
    $block .= '<td><a href=' $LinkSymbol . {$row['Order']} . '</a> (' . {$row['OrderCommon']} . ')</td>'
    $block .= '</tr>'
    Try that instead.

  12. #12
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dylannn
    PHP Code:
    $block .= '<tr>'
    $block .= '<td>' $sub_row[0] . '</td>'
    $block .= '<td><a href=' $LinkSymbol . {$row['Order']} . '</a> (' . {$row['OrderCommon']} . ')</td>'
    $block .= '</tr>'
    Try that instead.
    I get parse erros on this line...

    PHP Code:
    $block .= '<td><a href=' $LinkSymbol . {$row['Order']} . '</a> (' . {$row['OrderCommon']} . ')</td>'
    ...so I removed it. The situation with this line...

    PHP Code:
    $block .= '<td>' $sub_row[0] . '</td>'
    is now reversed; it displays nothing as is, but if I change it to double quotes, then it displays " . . "

    Thanks.

  13. #13
    SitePoint Evangelist dmsuperman's Avatar
    Join Date
    Feb 2005
    Location
    A box
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Did you do what I asked like 3 posts up? Tell us what you get.
    <(^.^<) \(^.^\) (^.^) (/^.^)/ (>^.^)>
    Core 2 Duo E8400 clocked @ 3.375GHz, 2x2GB 800MHz DDR2 RAM
    5x SATA drives totalling 2.5TB, 7900GS KO, 6600GT

  14. #14
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dmsuperman
    Did you do what I asked like 3 posts up? Tell us what you get.
    Sorry, I missed your post. I tried it just now, and it displays this:

    PHP Code:
    echo("<-".$sub_row[0]."->"); 
    When I insert between PHP tags, just after the table, it displays this:

    PHP Code:
    <--> 
    Then I placed it above the table, next to the other echo statement, like this:

    PHP Code:
    {
     echo 
    $sub_row[0]; // Your location for this Order

    echo(
    "<-".$sub_row[0]."->"); 
    It again displayed <-->, even though the other echo statement works. Finally, I placed both echo statements inside brackets, like this:

    PHP Code:
    {
     echo 
    $sub_row[0]; // Your location for this Order
     
    echo("<-".$sub_row[0]."->");

    Now BOTH display the same text, like this:

    PHP Code:
    Quebec<-Quebec->Japan<-Japan->Alabama<-Alabama->Alabama<-Alabama->Arizona<-Arizona->California<-California->Colorado<-Colorado->Delaware<-Delaware->Florida<-Florida->Georgia<-Georgia->Idaho<-Idaho->Illinois<-Illinois->Kentucky<-Kentucky->Maryland<-Maryland->Minnesota<-Minnesota->Mississippi<-Mississippi->Montana<-Montana->New Hampshire<-New Hampshire->New Mexico<-New Mexico->Oklahoma<-Oklahoma->Oregon<-Oregon->South Carolina<-South Carolina->Tennessee<-Tennessee->Texas<-Texas->Vermont<-Vermont->Virginia<-Virginia->West Virginia<-West Virginia-> 
    So it looks like this function works, but it's sabotaged by something farther down in my script, right?

    Thanks.

  15. #15
    *munch munch* killerkooki's Avatar
    Join Date
    Sep 2004
    Location
    Singapore
    Posts
    405
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is where you capture values for $sub_row. Correct?

    PHP Code:
            while ($sub_row mysql_fetch_row($sub_res)) {
            echo 
    $sub_row[0]; // Your location for this Order
            

    Which means, further down the script, when you access $sub_row, it contains the last value of that loop (provided there are more than one row returned, as I understand from your previous post), correct?
    Ok, so if there is a result, you allocate a value and you can display it properly (within the loop).
    So what if there isn't a value in it?
    Nothing is allocated to it right?

  16. #16
    SitePoint Evangelist dmsuperman's Avatar
    Join Date
    Feb 2005
    Location
    A box
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So after the while loop, you want to access them again?
    I would try this for the while loop instead of the above one:

    PHP Code:
    $rows = array();
    while (
    $sub_row mysql_fetch_row($sub_res)) { 
            
    $rows[] = $sub_row[0]; // Add the location to the order
            
    echo $sub_row[0]; // Your location for this Order 
            

    Then $rows would be an array containing all of the $sub_row[0]'s
    <(^.^<) \(^.^\) (^.^) (/^.^)/ (>^.^)>
    Core 2 Duo E8400 clocked @ 3.375GHz, 2x2GB 800MHz DDR2 RAM
    5x SATA drives totalling 2.5TB, 7900GS KO, 6600GT

  17. #17
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by killerkooki
    This is where you capture values for $sub_row. Correct?

    PHP Code:
            while ($sub_row mysql_fetch_row($sub_res)) {
            echo 
    $sub_row[0]; // Your location for this Order
            

    Which means, further down the script, when you access $sub_row, it contains the last value of that loop (provided there are more than one row returned, as I understand from your previous post), correct?
    Ok, so if there is a result, you allocate a value and you can display it properly (within the loop).
    So what if there isn't a value in it?
    Nothing is allocated to it right?
    I'm not sure I understand. It looks like $sub_row captures the FIRST value. In some cases, that could be an essentially empty row. For example, I've left some table cells blank as I'm still gathering data.

    However, my other echo function displays data in this case, and $sub_row displays the same data. So there must be a value; it just doesn't display farther down the script.

  18. #18
    *munch munch* killerkooki's Avatar
    Join Date
    Sep 2004
    Location
    Singapore
    Posts
    405
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This would capture one row, the first (if there are more than one result rows).
    PHP Code:
    if ($sub_row mysql_fetch_row($sub_res)) {
            echo 
    $sub_row[0]; // Your location for this Order
            

    If you use the while loop, it would keep iterating through the results until it reaches the end and its time to exit the loop.

  19. #19
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by killerkooki
    This would capture one row, the first (if there are more than one result rows).
    PHP Code:
    if ($sub_row mysql_fetch_row($sub_res)) {
            echo 
    $sub_row[0]; // Your location for this Order
            

    If you use the while loop, it would keep iterating through the results until it reaches the end and its time to exit the loop.
    Thanks...can you show me how to insert the while loop?

  20. #20
    *munch munch* killerkooki's Avatar
    Join Date
    Sep 2004
    Location
    Singapore
    Posts
    405
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am not sure what you are trying to do yet...
    Anyhow, you have a while loop like so:
    PHP Code:
    while ($sub_row mysql_fetch_row($sub_res))
    {
    echo 
    $sub_row[0]; // Your location for this Order

    What I meant was to change that to:
    PHP Code:
    if ($sub_row mysql_fetch_row($sub_res)) {
            echo 
    $sub_row[0]; // Your location for this Order
            

    The question is, are you expecting only a single row from the result for your query:
    PHP Code:
    $sub_res mysql_query("SELECT Area FROM symbols WHERE `Order`='$row[0]'"); 
    If you are, then substitute the IF as above.
    If you need to use multiple values, then use WHILE.
    However, if you are using WHILE, that means whatever code that use the $sub_row needs to be within that WHILE loop. Hope what I am saying is coherent.

  21. #21
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm expecting several rows. Let me explain it from the beginning. Imagine a database table with the following rows:

    California | butterflies
    Florida | butterflies
    Michigan | beetles
    Oregon | butterflies
    Utah | honeybees
    Washington | butterflies
    Washington |dragonflies

    I would like to print out ONE row for each group of insects, but I'd like all the place names associated with that group on the same line. So the display would look like this:

    California, Florida, Oregon, Washington | butterflies
    Michigan | beetles
    Utah | honeybees
    Washington | dragonflies

    The echo statements aren't important; I was just using them to try and figure out what's wrong witih my script.

    So this is my basic query...

    PHP Code:
     $res mysql_query("SELECT DISTINCT(`Order`) FROM symbols WHERE Class = '$myname'");
     while (
    $row mysql_fetch_row($res))
    {
    $sub_res mysql_query("SELECT Area FROM symbols WHERE `Order`='$row[0]'"); 
    And you're saying I need to put $sub_res inside the while loop, right? How do I do that? This doesn't work:

    PHP Code:
    $res mysql_query("SELECT DISTINCT(`Order`) FROM symbols WHERE Class = '$myname'");
    while (
    $row mysql_fetch_row($res){
          
    //echo $query[0]; // Your individual Orders
    $sub_res mysql_query("SELECT Area FROM symbols WHERE `Order`='$row[0]'")); 
    Thanks.

  22. #22
    *munch munch* killerkooki's Avatar
    Join Date
    Sep 2004
    Location
    Singapore
    Posts
    405
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just put together this. It's not elegant, nor is it tested.
    But see if it does what you want.

    PHP Code:
    $res mysql_query("SELECT DISTINCT(`Order`) FROM symbols WHERE Class = '$myname'");
    $orderArray = array();
    while (
    $row mysql_fetch_row($res)) {
        
    //Dump all the orders into an array
        
    $orderArray[] = $row[0];
        }

    $placeArray = array();
    for (
    $counter 0$counter count($orderArray); $counter++) {
        
    $res mysql_query("SELECT Area FROM symbols WHERE `Order`='$placeArray[$counter]'");
        
    $placeArrayForOrder = array();
        while (
    $row mysql_fetch_row($res)) {
            
    //Dump all the places into an array
            
    $placeArrayForOrder[] = $row[0];
            }
        
    $placeArray[] = $placeArrayForOrder;
        }

    for (
    $counter 0$counter count($orderArray); $counter++) {
        echo 
    $orderArray[$counter] . ': ';
        
    $tempArray = array();
        
    $tempArray $placeArray[$counter];
        for (
    $counterI 0$counterI $count($tempArray); $counterI++) {
            if (
    $counterI == 0)
                echo 
    $tempArray[$counterI];
            else
                echo 
    ', ' $tempArray[$counterI];
            }
        echo 
    '<br>';
        } 
    Put it after your second switch block. You might also want to comment out the other parts that is trying to get the SQL results and print it while you test this out.
    Hope that does the trick.

  23. #23
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow, thanks for going to so much trouble.

    I get an error message:

    Fatal error: Function name must be a string in C:\sites\GeoSymbols\a1\articles\ani\tableclass.php on line 54

    The error is on the second line below:

    PHP Code:
        $tempArray $placeArray[$counter];
        for (
    $counterI 0$counterI $count($tempArray); $counterI++) {
            if (
    $counterI == 0
    Thanks.

  24. #24
    *munch munch* killerkooki's Avatar
    Join Date
    Sep 2004
    Location
    Singapore
    Posts
    405
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oopsy...
    PHP Code:
     $tempArray $placeArray[$counter];
        for (
    $counterI 0$counterI count($tempArray); $counterI++) {
            if (
    $counterI == 0
    Accidentally put a $ in front of the function. Give it a whirl now.

  25. #25
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hooray! It looks like you've solved it. Now I just need to display the text in table cells...

    So what would I replace ??? with if I want to display places in the first column and animal groups in the second?

    PHP Code:
    $block .= '<td>???</td>';
    $block .= '<td>???</td>'
    Thanks.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •