[英]How to convert mysql table to html table to match columns and rows?
我在理解mysql表->数组->循环->用php打印查询时遇到了严重的问题。
我想将mysql表回显到带标题的html表(html,而不是mysql)。 COLUMN“标题”应显示为:
<tr><th>header</th><th>header</th><th>header</th><th>header</th></tr>
和“字段”为:
<tr><td>field</td><td>field</td><td>field</td><td>field</td></tr>
<tr><td>field</td><td>field</td><td>field</td><td>field</td></tr>
<tr><td>field</td><td>field</td><td>field</td><td>field</td></tr>
<tr><td>field</td><td>field</td><td>field</td><td>field</td></tr>
问题是:如何遍历此类查询(或如何进行此类查询)到ECHO头(如mysql表列)和循环字段?
也许这会有所帮助:
`id` int(8) NOT NULL AUTO_INCREMENT,
`section_id` int(8) NOT NULL DEFAULT '0',
`header` varchar(64) NOT NULL,
`position` int(2) NOT NULL,
`field` varchar(16) NOT NULL,
`sorting` int(1) NOT NULL,
`visible` int(1) NOT NULL,
`width` int(3) NOT NULL,
PRIMARY KEY (`id`)
我在这一点上:
<table>
<?php
$gsh = mysqli_query( $connector, "SELECT header, width FROM crm_sections_fields WHERE section_id='$sectionID' ORDER BY position ASC");
if(!$gsh) { MessageView('111'); }
else {
?>
<tr>
<?php while($h = mysqli_fetch_array($gsh))
{
echo "<th width=".$h['width'].">".$h['header']."</th>";
}
?> </tr> <?php
}
///////// NOW IT SHOULD LOOP THROUGH ROWS
</table>
这样尝试。
<table>
<tr><td>Id</td><td>Data</td></tr>
<?
$Sql = mysql_query("SELECT * FROM `YOURTABLE`");
while($dataSQL = mysql_fetch_array($Sql)){
?>
<tr><td><?=$dataSQL[id];?></td><td><?=$dataSQL[field];?></td></tr>
<?
}
?>
</table>
据我了解,您想知道如何动态显示标题吗? 如果您不想在HTML表中明确提及列名,可以在这里查看我的答案: 向MYSQL表添加新列后在PHP中编辑表数据
尝试:
<html>
<body>
<table>
<?php
$que = $dbh->query('SELECT * FROM TABLE');
$header=false;
while ($row = $que->fetch()) {
if($header===false){
echo '<tr><td>'. implode('</td><td>',array_keys($row)) . '</td></tr>';
$header=true;
}
echo '<tr><td>'. implode('</td><td>',$row) . '</td></tr>';
}
?>
</table>
</body>
</html>
<table>
<?php
$gsh = mysqli_query( $connector, "SELECT header, width FROM crm_sections_fields WHERE section_id='$sectionID' ORDER BY position ASC");
if(!$gsh) { MessageView('111'); }
else {
?>
<tr>
<?php while($h = mysqli_fetch_array($gsh))
{
echo "<th width=".$h['width'].">".$h['header']."</th>";
}
?> </tr>
after this
<?php
//here place you query and loop though as above and print the data in tr td instead of th above use td
?>
</table>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.