将mySQL记录显示为HTML表列

时间:2014-02-19 03:09:34

标签: php

我想生成一个HTML表,并将MySQL表中的记录显示为列,而不是行,如:

enter image description here

周名称在表'周'中,每周记录还包含该周的会话数:

+---------+-----------+----------+-----------+
| week_pk | week_name | sessions | cohort_fk |
+---------+-----------+----------+-----------+
|       1 | Week 1    |        3 |         1 |
|       2 | Week 2    |        2 |         1 |
|       3 | Week 3    |        1 |         1 |
+---------+-----------+----------+-----------+

队列表是:

+-----------+-------------+-------------+-------------+
| cohort_pk | cohort_name | cohort_code | cohort_year |
+-----------+-------------+-------------+-------------+
|         1 | Some name   | MICR8976    |        2014 |
+-----------+-------------+-------------+-------------+ 

我发现一些代码会在表格中生成HTML表格列的记录,好的(我确定这段代码可以改进......)。

那么,问题是如何修改此代码以在HTML表格中为每周coloumn生成会话列?例如,对于HTML表格中的第1周列,3个会话列,依此类推其他周列?

对解决方案的任何帮助表示赞赏。

   $query = "SELECT * FROM cohort, week 
            WHERE week.cohort_fk = cohort.cohort_pk 
            AND cohort.cohort_year = '$year' 
            AND cohort.cohort_pk = '$cohort'";
   $result = mysql_query($query, $connection) or die(mysql_error());

    echo "<table width = 50% border = '1' cellspacing = '2' cellpadding = '0'>";

    $position = 1;
    while ($row = mysql_fetch_array($result)){

    if($position == 1){
        echo "<tr>";
        }

    echo " <td width='50px'>" . $row['week_name'] . "</td> ";

    if($position == 3){
        echo "</tr> "; 
        $position = 1;
        }else{ 
        $position++;
        }
    }

    $end = "";
    if($position != 1){
    for($z=(3-$position); $z>0; $z--){
    $end .= "<td></td>";
    }
    $end .= "</tr>";
    }

    echo $end."</table> ";

2 个答案:

答案 0 :(得分:1)

mysqli_ *函数在以下示例中使用。

$year = 2014; //for the testing purpose
$cohort = 1;  //for the testing purpose
$query = "SELECT * FROM cohort, week 
WHERE week.cohort_fk = cohort.cohort_pk 
AND cohort.cohort_year = '$year' 
AND cohort.cohort_pk = '$cohort'";

$dblink = mysqli_connect("localhost", "root", "", "test");
$result = mysqli_query($dblink, $query);

echo "<table border='1'>";
echo "<tr><td>Name</td>";
$second_row = "<tr><td>Session</td>";
while( $row = mysqli_fetch_assoc($result) ){
    $weekname   = $row["week_name"];
    $n_session  = $row["sessions"];
    echo "<td colspan='$n_session'>$weekname</td>";
    for($i=1; $i<=$n_session; $i++){
        $second_row .= "<td>S$i</td>";
    }
}//end while
echo "</tr>";
echo "$second_row</tr>";
echo "</table>";

增加: 生成第三行 -

$year = 2014;
$cohort = 1;
$query = "SELECT * FROM cohort, week 
WHERE week.cohort_fk = cohort.cohort_pk 
AND cohort.cohort_year = '$year' 
AND cohort.cohort_pk = '$cohort'";

$dblink = mysqli_connect("localhost", "root", "", "test");
$result = mysqli_query($dblink, $query);

echo "<table border='1'>";
echo "<tr><td>Name</td>";
$second_row = "<tr><td>Session</td>";
$totalcolumn = 1;                               //for the third row
while( $row = mysqli_fetch_assoc($result) ){
    $weekname   = $row["week_name"];
    $n_session  = $row["sessions"];
    $totalcolumn += $n_session;                 //for the third row
    echo "<td colspan='$n_session'>$weekname</td>";
    for($i=1; $i<=$n_session; $i++){
        $second_row .= "<td>S$i</td>";
    }
}//end while
echo "</tr>";
echo $second_row . "</tr>";

echo "<tr>";                            //for the third row
for($i=1; $i<=$totalcolumn; $i++){      //for the third row
    echo "<td>data-set</td>";           //for the third row
}                                       //for the third row
echo "</tr>";                           //for the third row

echo "</table>";

您可以在<table>

的位置添加其他HTML data-set

答案 1 :(得分:0)

$query = "SELECT * FROM cohort, week WHERE week.cohort_fk = cohort.cohort_pk AND cohort.cohort_year = '$year' AND cohort.cohort_pk = '$cohort'";
$result = mysql_query($query, $connection) or die(mysql_error());

$table = array("<table width = 50% border = '1' cellspacing = '2' cellpadding = '0'>");
$table[] = "</tr>";
$table[] = "<th>Name</th>";
$rows = array();
while ($rows[] = $row = mysql_fetch_array($result, MYSQL_ASSOC))
    $table[] = "<td colspan='" . (int) $row['sessions'] . "'>" . $row['week_name'] . "</td>";

$table[] = "</tr>";
$table[] = "<tr>";
$table[] = "<th>Sessions</th>";
foreach ($rows as $row) {
    for ($i = 1; $i <= (int) $row['sessions']; $i++)
        $table[] = "<td>S" . $i . "</td>";
}
$table[] = "</tr>";
$table[] = "</table>";

echo join("", $table);