Formatting mysql_fetch_array results in HTML table

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bill

    Formatting mysql_fetch_array results in HTML table

    Hi,

    This looks like a simple problem, but I just can't seem to wrap my
    head around it.

    I'm trying to build a table dynamically, based on a result set from
    mysql. There will be 9 results for a full page, with 3 table rows of 3
    table cells. One result per cell. But all of the pages won't be full.

    Here's one of many ways I've tried to do it:

    <html>
    <head>
    <title>test</title>
    </head>
    <body>
    <table width="100%" border="1">
    <?php

    include_once('d b.inc.php');

    $query = "SELECT DISTINCT title FROM mytable LIMIT 9";
    $result = mysql_query($qu ery);

    while ($row = mysql_fetch_arr ay($result)) {
    $title = $row['title'];
    }

    for ($i = 0; $i < 3; $i++) {
    print ' <tr>' . "\n";

    for ($e = 0; $e < 3; $e++) {
    print ' <td align="center"> ' . "\n".
    ' title : ' . $title . "\n".
    ' </td>' . "\n";
    }
    print ' </tr>' . "\n";
    }


    ?>
    </table>
    </body>
    </html>

    This prints out 3 rows of three like I hoped, but $title is the same
    in all 9 cells. I also have tried it with the for loops inside the
    while loop, which prints out 9 rows of 9 cells 9 times!

    I know I'm probably missing something obvious, so maybe someone can
    point out what I'm doing wrong?
  • Geoff Berrow

    #2
    Re: Formatting mysql_fetch_arr ay results in HTML table

    I noticed that Message-ID: <s0bqi0lm31cqc8 b6i1jmetff6qh58 489p3@4ax.com>
    from bill contained the following:
    [color=blue]
    >This prints out 3 rows of three like I hoped, but $title is the same
    >in all 9 cells. I also have tried it with the for loops inside the
    >while loop, which prints out 9 rows of 9 cells 9 times![/color]

    Your problem is that you need to generate the table, whether or not you
    have results. This means that the table generation code must exist
    independently of the calls to the results array. Fortunately
    mysql_fetch_arr ay() steps to the next record each time it is called so
    if you put it in a function it should insert consecutive records until
    they are all used. However the table creation can continue.

    Something like (untested):

    <table width="100%" border="1">
    <?php

    include_once('d b.inc.php');

    $query = "SELECT DISTINCT title FROM mytable LIMIT 9";
    $result = mysql_query($qu ery);

    function next_row() {
    $row = mysql_fetch_arr ay($result);
    $title = $row['title'];
    if($title==""){ $title="&nbsp;" ;}
    return $title;
    }

    for ($i = 0; $i < 3; $i++) {
    print ' <tr>' . "\n";

    for ($e = 0; $e < 3; $e++) {

    print ' <td align="center"> ' . "\n".
    ' title : ' .next_row() . "\n".
    ' </td>' . "\n";
    }
    print ' </tr>' . "\n";
    }


    ?>
    </table>

    --
    Geoff Berrow (put thecat out to email)
    It's only Usenet, no one dies.
    My opinions, not the committee's, mine.
    Simple RFDs http://www.ckdog.co.uk/rfdmaker/

    Comment

    Working...