简体   繁体   English

在HTML中创建表以在MySQL中显示查询结果

[英]Creating a table in HTML to display the result of a query in MySQL

I want to create a PHP page to display the results of a query in a MySQL database under the format of a table. 我想创建一个PHP页面,以表格格式在MySQL数据库中显示查询结果。 By spending quite some time on different forums, I ended with something that is somehow satisfying me but that is strongly affecting the design and the layout of my webpage. 通过在不同的论坛上花费相当长的时间,我得到了某种令我满意的东西,但是这极大地影响了我的网页的设计和布局。 Due to the fact that I wrote the code by a test-fail strategy, it is far from being straightforward and I am sure it is possible to shorten and simplify it and, therefore, make it more compatible with the format of my webpage. 由于我是通过测试失败策略编写代码的,因此它远非一帆风顺,而且我确信可以缩短和简化它,并使其与我的网页格式更加兼容。 Could anybody have a look at it and give some suggestions of general interest about how to solve this kind of issues? 有人可以看一下它,并提出一些有关如何解决此类问题的一般性建议吗?

  <div id="main">
  <?php
  require_once('../mysqli_connect.php');
  $response = $db->query("SELECT * FROM metabolite");
  echo '<table align="center" cellspacing="2" cellpadding="5" border = "1">
  <tr><td align="center"><b>Metabolites</b></td>
  <td align="center"><b>KEGG Id</b></td>
  <td align="center"><b>Synonyms</b></td></tr>';
  while ($data = $response->fetch())
  {
  ?>
  <tr><td align="left">
  <?php echo $data['Metabolite_name']; ?></td>
  <td align="left">
  KEGG: <?php echo $data['Synonyms']; ?></td>
  <td align="left">
  <?php echo $data['Synonyms']; ?></td>
  </tr>
  <?php
  }
  $response->closeCursor();
  ?>
  </div>

I thank you in advance for all your effort and your help. 我预先感谢您的所有努力和帮助。

Tom. 汤姆

There's no way we can improve the design and layout of your webpage with the code you've given us. 我们无法使用您提供给我们的代码来改善您网页的设计和布局。 What I can do is write 'better' readable code. 我所能做的就是编写“更好”的可读代码。

<?php

function tableCell($content)
{
  echo '<td align="left">'.$content.'</td>';
}

// database access 
require_once('../mysqli_connect.php');
// get all records from the metabolite table
$response = $db->query("SELECT * FROM metabolite");
// start main division
echo '<div id="main">';
// start the table
echo '<table align="center" cellspacing="2" cellpadding="5" border = "1">';
// walk through all the metabolite records
while ($data = $response->fetch()) 
{
  // start a row
  echo '<tr>';
    // create the cells
    tableCell($data['Metabolite_name']);
    tableCell('KEGG: '.$data['Synonyms']);
    tableCell($data['Synonyms']);
  // finish a row
  echo '</tr>';
}
// close the table
echo '</table>';
// close main division
echo '</div>';
// close query
$response->closeCursor();

But this is not worth much, the output should remain the same. 但这不值多少,输出应保持不变。

if ($response->num_rows > 0) {
    while($data = $response->fetch_assoc()) {
    echo "<tr><td>" . $data["Metabolite_name"]. "</td></tr>" . ;
        }
    }
else {
    echo "0 results";
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM