Results 1 to 5 of 5

Thread: How ddoes this loop

  1. #1
    Join Date
    May 2004
    Posts
    530
    Rep Power
    0

    Default How ddoes this loop

    Could someone read this code and tell me how it loops. Is it looping from left to right or from top to bottom to create the table. It seems silly but it will help in how i am trying to modify it. What i really want to know is if it is creating a column head and column body one column at a time or creating all the heads from left to right then all the column body cells from left to right. Hope i am not confusing. Here is the code:

    Code:
    mysql_select_db($database_myPlaceConn, $myPlaceConn);
    $query="select * from myp_massuploads";
    $result=mysql_query($query,$myPlaceConn) or die("Query ($query) sucks!");
    $fields=mysql_num_fields($result);
    
    echo "<table>\n<tr>";
    for ($i=0; $i < mysql_num_fields($result); $i++) //Table Header
    { print "<th>".mysql_field_name($result, $i)."</th>"; }
    echo "</tr>\n";
    while ($row = mysql_fetch_row($result)) { //Table body
    echo "<tr>";
        for ($f=0; $f < $fields; $f++) {
        echo "<td>$row[$f]</td>"; }
    echo "</tr>\n";}
    echo "</table><p>";
    Last edited by leocrawf; Oct 31, 2009 at 08:15 PM.
    "...men are that they might have joy."
    Nephi

  2. #2
    Join Date
    Oct 2005
    Posts
    745
    Rep Power
    0

    Default

    Table Head, one column at a time
    Code:
    echo "<table>\n<tr>";
    for ($i=0; $i < mysql_num_fields($result); $i++) //Table Header
    { 
    print "<th>".mysql_field_name($result, $i)."</th>"; 
    }
    echo "</tr>\n";

    THEN

    Table body, one column at a time for each row
    Code:
    while ($row = mysql_fetch_row($result)) { //Table body
    echo "<tr>";
        for ($f=0; $f < $fields; $f++) {
        echo "<td>$row[$f]</td>"; }
    echo "</tr>\n";}
    echo "</table><p>";
    3.14159265358979323846264338327950288
    4197169399375105820974944592307816406
    28620899862803482534211706798 pi 101

  3. #3
    Join Date
    May 2004
    Posts
    530
    Rep Power
    0

    Default

    Thats what i thought. Thank you.
    "...men are that they might have joy."
    Nephi

  4. #4
    Join Date
    Feb 2003
    Posts
    3,184
    Rep Power
    0

    Default

    It is the nature of html to be 1 tag after the other. so if you checked the html output of the code you should be able to easily see what is going. You could also put a print out the $i variable and then look at the source to see the order. Debuging 101

  5. #5
    Join Date
    Mar 2005
    Posts
    28
    Rep Power
    0

    Default

    i think once its a table, it will create it from left to right

Posting Permissions

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