将MySQL记录显示为HTML表

将MySQL记录显示为HTML表

本文介绍了将MySQL记录显示为HTML表列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to generate an HTML table and display records from MySQL tables as columns, not rows, like:

星期名称在表"week"中,并且每个星期的记录还包含该周的会话数:

The week names are in table 'week' and each week record also contains the number of sessions for that week:

+---------+-----------+----------+-----------+
| 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表格列的形式生成记录,好的(我确定可以改进此代码...).

I found some code which generates records in a table as HTML table columns, OK (I'm sure this code could be improved...).

所以,问题是我如何才能修改此代码以在每周的对话中生成HTML表中的会话列?例如,对于HTML表中的第1周"列,第3个会话列,以及其他周"列,依此类推?

So, the question is how can I modify this code to generate the session columns in the HTML table for each week coloumn? For example, for the Week 1 column in the HTML table, the 3 session columns, and so on for the other week columns?

对寻求解决方案的任何帮助表示赞赏.

Any help towards a solution appreciated.

   $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> ";

在以下示例中使用

推荐答案

mysqli_ *函数.

mysqli_* functions are used in the following example.

$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>";


已添加:生成第三行-


ADDED:to generate third row -

$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>";

您可以在data-set

这篇关于将MySQL记录显示为HTML表列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 02:33