#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2000
    Posts
    4
    Rep Power
    0
    I have a results page that has to display records in a table that is three columns by an infinite number of rows.

    for example:
    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre><TR>
    <TD>item1</TD><TD>item2</TD><TD>item3</TD>
    </TR>
    <TR>
    <TD>item4</TD><TD>item5</TD><TD>item6</TD>
    </TR>
    etc...[/code]

    I use the following code to run the MySQL statement and format the results into an array

    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
    /* select items from table */
    $sql = "SELECT *
    FROM Table
    WHERE ID = '$ID'";

    /* execute sql query and get result */
    $sql_result = mysql_query($sql, $connection) or die ("Couldn't execute query.");

    /* format results by row */
    while($row = mysql_fetch_array($sql_result)) {
    $item1 = $row["item1"];
    $item2 = $row["item2"];
    };
    [/code]

    I'm assuming I have to create a for loop to create table rows of three columns each, but am unsure of how to do this.

    Any suggestions.
  2. #2
  3. .Net Developer
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2000
    Location
    London
    Posts
    987
    Rep Power
    15
    acmearts,

    you can use the looping for fetching those record.

    try the following ....


    if ($row = mysql_fetch_array($sql_result)) {

    do {
    print $row["item1"];
    print $row["item2"];
    print $row["item3"];
    } while($row = mysql_fetch_array($sql_result));

    }else{
    print "No Records!!";
    }

    GOOD LUCK!!!


    ------------------
    SR -
    shiju.dreamcenter.net
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2000
    Posts
    4
    Rep Power
    0
    I see now that my example was not very clear. My sql statement was correct...
    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
    /* select items from table */
    $sql = "SELECT *
    FROM Table
    WHERE ID = '$ID'";

    /* execute sql query and get result */
    $sql_result = mysql_query($sql, $connection) or die ("Couldn't execute query.");

    /* format results by row */
    while($row = mysql_fetch_array($sql_result)) {
    $item1 = $row["item1"];
    $item2 = $row["item2"];
    };
    [/code]

    but my table example was misleading. A better representation of what my table needs to look like is:

    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
    <TR>
    <TD>item1 item2</TD><TD>item1 item2</TD><TD>item1 item2</TD>
    </TR>
    <TR>
    <TD>item1 item2</TD><TD>item1 item2</TD><TD>item1 item2</TD>
    </TR>
    [/code]

    Each table cell should show a different instance of data pulled from the fields item1 and item2.

    For example: If item1 is a persons First name, and item2 is the Last name, I could have a 3(columns) by 2(rows) table with different names in each cell.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 1999
    Location
    Seattle
    Posts
    133
    Rep Power
    16
    I would do something like:
    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
    /* select items from table */
    $sql = "SELECT * FROM Table WHERE ID = '$ID'";

    /* execute sql query and get result */
    $sql_result = mysql_query($sql, $connection) or die ("Couldn't execute query.");

    /* format results by row */
    $x = 0;
    while($row =mysql_fetch_array($sql_result)) {
    if ($x == 0) print "<TR>";
    print "<TD>$row['item1'] $row['items2']</TD>";
    $x++;
    if ($x == 2) {
    $x = 0;
    print "</TR>";
    }
    }
    [/code]

    Of course you'll have to add code after the loop to add columns if the returned count is not a multiple of three and to close out the table row tag (</TR> ).

    Don

Similar Threads

  1. Replies: 5
    Last Post: July 29th, 2011, 10:21 PM
  2. Inserting multiple rows to mysql table
    By Roaddust in forum PHP Development
    Replies: 6
    Last Post: February 4th, 2004, 09:28 AM
  3. how to keep table on top, not centered??
    By vinyl in forum HTML Programming
    Replies: 2
    Last Post: January 20th, 2004, 07:19 PM
  4. Replies: 2
    Last Post: January 18th, 2004, 11:10 PM
  5. updating a field in a table with the rows rank
    By thatcoryguy in forum MySQL Help
    Replies: 0
    Last Post: January 3rd, 2004, 01:11 AM

IMN logo majestic logo threadwatch logo seochat tools logo