在多个html表中显示结果

时间:2012-01-24 02:23:20

标签: php mysql html-table

我一直为我们的曲棍球俱乐部制作一份志愿者报名表。我使用while循环在表格中显示结果。

一切都很好,但我现在想要让输出更加用户友好。我按日期订购查询并将其显示在一个表中。我想要做的是为每个不同的日期显示一个新表。该表的标题将是日期,行将按时间排序该日期的所有事件。我认为这会让人们更容易在特定日期找到活动。

以下是我的表格设置方式:id,日期,时间,游戏,name1,name2,name3,name4,name5

以下是我的主页上的查询:

<?php
//displays the table
$result = mysql_query("SELECT * FROM namestable ORDER BY date, time ASC");
displayTable($result);
?>

这是我的功能:

<?php
 function displayTable($result){

echo "
<table border='1'>
<tr class='top'>
<th class='date'>Date</th>
<th class='time'>Time</th>
<th class='game'>Game</th>
<th class='name'>Name</th>
<th class='name'>Name</th>
<th class='name'>Name</th>
<th class='name'>Name</th>
<th class='name'>Name</th>
<th class='sign'>Sign Up</th>
</tr>";

while($row = mysql_fetch_array($result))

{
//change the date format here
$fdate = date('M jS, Y l', strtotime($row['date']));
echo "<tr>";
echo "<td class='date'>" . $fdate . "</td>";
echo "<td class='time'>" . $row['time'] . "</td>";
echo "<td class='game'>" . $row['game'] . "</td>";
echo "<td class='nameA'>" . $row['name1'] . "</td>";
echo "<td class='nameB'>" . $row['name2'] . "</td>";
echo "<td class='nameA'>" . $row['name3'] . "</td>";
echo "<td class='nameB'>" . $row['name4'] . "</td>";
echo "<td class='nameA'>" . $row['name5'] . "</td>";
$id = $row['id'];
echo "<td class='sign'>" . "<form name='input' action='process.php' method='POST'>
             <input type='text' name='name' maxlength='25' value='Enter Name'                                                onfocus=\"if(this.value=='Enter Name') this.value='';\"/>
             <input type='hidden' name='id' value='$id'/>
             <input type='submit' value='Sign Up' />
             <input type='submit' name='dname' value='Remove Name' /></form>" .
            "<form name='delete' action='delete.php' method='POST'>
             <input type='hidden' name='id' value='$id'/>";
             if(isset($_SESSION['user_id'])){
             echo "<input type='submit' name='delete' value='Delete Event' /></form>" .
                   "</td>";
             }else{
                echo "</form>" .
                     "</td>";
             }
echo "</tr>";
}
echo "</table>";
echo "<br />";
}
?>

用户可以输入名称进行注册并删除其姓名。

管理员可以添加和删除事件。

有什么建议吗?

2 个答案:

答案 0 :(得分:2)

由于您的结果已按日期排序,因此您只需将上一个日期存储在变量中,并在每次更改时创建一个新表。

$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 like
    echo "
    <h3>$fdate</h3>
    <table border='1'>
    <tr class='top'>
    ...
    </tr>";
    }
// print a row as before.
echo "<tr>";
....
}
// end the last table
echo "</table>";

基本上唯一改变的是$fdate也存储在$olddate中。当我们处理新行时,如果日期已更改(即$olddate != $fdate),我们将创建一个新表。

对于mysql结果中的每一行,我们仍然像以前一样生成一个表行(您可能希望进行一些更改,例如不再包括Date列)。

答案 1 :(得分:1)

$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 here

            echo "<tr><td>ROW WITH EVENT INFO HERE</td></tr>";

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