Skip to content Skip to sidebar Skip to footer

Display Results In Multiple Html Tables

I have been making a volunteer sign up sheet for our hockey club. I display the results in a table using a while loop. Everything works good, but I am now wanting to make the outp

Solution 1:

Since your results are already ordered by date, you can just store the previous date in a variable and create a new table every time this changes.

$olddate = '';

while($row = mysql_fetch_array($result))
{
$fdate = date('M jS, Y l', strtotime($row['date']));
if ( $olddate != $fdate ) { // date has changed:// end the previous table (if it exists)if ( $olddate != '' ) {
        echo"</table>"
    }
    // start the new table. Do something with $fdate here if you likeecho"
    <h3>$fdate</h3>
    <table border='1'>
    <tr class='top'>
    ...
    </tr>";
    }
// print a row as before.echo"<tr>";
....
}
// end the last tableecho"</table>";

Basically the only thing that has changed is that the $fdate is stored also in $olddate. When we process a new row, if the date has changed (ie $olddate != $fdate), we create a new table.

For each row in the mysql result we still generate a table row as before (you may want to make some changes like not including the Date column any more).

Solution 2:

$getdates = mysql_query("SELECT DISTINCT date FROM namestable ORDER BY date DESC");
$dates = mysql_num_rows($getdate);

if($dates > 0){
    echo"<table width='100%' cellspacing='0' cellpadding='0'>";

    while ($rowdates = mysql_fetch_assoc($getdates)) {

    $date = $rowdates['date'];

    $getevents = mysql_query("SELECT * FROM namestable WHERE date = '$date' ORDER BY time ASC");
    $events = mysql_num_rows($getevents);


        if($events > 0){
        echo"<tr><td>$date</td></tr>";

            while ($rowevents = mysql_fetch_assoc($getevents)) {

            $event_time = $rowevents['time'];
            // all other info you want to pull hereecho"<tr><td>ROW WITH EVENT INFO HERE</td></tr>";

            } // end event loop
        } // end if events > 0echo"</table>";
    } // end if dates > 0
} // end date loop

Post a Comment for "Display Results In Multiple Html Tables"