0
我想動態生成多級頭表。不幸的是,經過漫長的試驗後,我意識到我無法單獨做到這一點。動態生成表clospan報頭
這是我工作的PHP代碼。只有一兩件事需要做的是特里爾1頭
PHP代碼:
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<!--
/**************
* CSS styles *
**************/
-->
<style type="text/css">
h1 {font-family:Segoe UI;font-size:18px;}
h2 {font-family:Segoe UI;font-size:14px;font-weight:normal;}
table {border-collapse:collapse;border-spacing:0;border-color:#ccc;}
th {font-family:Segoe UI;font-size:13px;font-weight:bold;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#ccc;color:#333;background-color:#f0f0f0;}
td {font-family:Segoe UI;font-size:13px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#ccc;color:#333;background-color:#fff;}
</style>
<!--
/**********************
* connection details *
**********************/
-->
<?php
$host = "127.0.0.1";
$user = "dnlw";
$pass = "";
$database = "ksiegarnia";
$port = 3306;
$connect = mysql_connect($host, $user, $pass, $port);
if (!$connect)
{
die("Can't connect to database");
}
if (!mysql_select_db($database))
{
die("Can't select database");
}
/*****************
* sending query *
*****************/
$query = file_get_contents("query.txt");
mysql_query("SET NAMES 'utf8'");
$result = mysql_query($query);
$row_num = mysql_num_rows($result);
$fields_num = mysql_num_fields($result);
if (!$result)
{
die("Query to show fields from table failed");
}
/**********************
* printing the table *
**********************/
echo "
<title>Query result</title>
<h2>znaleziono: <b>{$row_num}</b></h2>
<table>
<thead>
<tr>";
// ?????????????????????????????????????????????
for($i=0; $i<$fields_num; $i++)
{
$header = mysql_fetch_field($result);
echo "<th>{$header->table}</th>";
}
echo "
</tr>
<tr>";
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
$header2 = mysql_fetch_field($result);
echo "<th>{$header->name}</th>";
}
echo "
</tr>
</thead>
<tbody>
<tr>";
// printing table rows
while($row = mysql_fetch_row($result))
{
echo "<tr>";
// $row is array... foreach(..) puts every element
// of $row to $cell variable
foreach($row as $cell)
echo "<td>$cell</td>";
echo "</tr>\n";
}
mysql_free_result($result);
echo"
</tr>
</tbody>
</table>";
?>
</body>
</html>
鏈接到數據庫: https://1drv.ms/u/s!AvpVESbXIIvxhrByuEodnjt5JTdgLA
哇!太優雅了!非常感謝你!!! – Daniel
當然!所以$表最終將看起來像{klienci:4,zamowienia:5,ksiazki:5} foreach循環每個項目,並將關聯數組的鍵分配給$ table,並且它對應的值爲$ span。在第一個循環中,$ table將是「klienci」,$ span將保存值「4」。這有幫助嗎? – MacPrawn
是的,它有幫助,但我仍然不明白爲什麼在第一個循環中$ span是4。據我瞭解,它應該從第一回路的1增加到第四回路的4 ... – Daniel