如何MySQL表转换为HTML表,以匹配的行和列?(How to convert mysql tab

2019-10-18 02:13发布

我有严重的问题,了解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>

现在的问题是:如何通过这样的查询循环(或如何进行这样的查询)来呼应标题为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>

Answer 1:

试试这个样子。

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


Answer 2:

我的理解,你想知道如何使动态地显示标题? 如果你不想明确提及在HTML表中的列名,你可以看看在我的答案在这里: 在PHP编辑表中的数据添加新列的MySQL表后



Answer 3:

尝试:

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

这是假设你使用的是PDO接口来访问你的数据库,你应该是因为mysql_query及其同类已弃用。



Answer 4:

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


文章来源: How to convert mysql table to html table to match columns and rows?