【问题标题】:I want to put this resulting query into a 3 by 3 html table我想将此生成的查询放入 3 x 3 html 表中
【发布时间】:2020-02-03 18:48:25
【问题描述】:

我正在尝试从我的数据库中获取最近时间的 PPM 值,并在按下“显示所有数据”按钮后将其打印到我的 html 页面中

<?php

$mysqli = new mysqli("localhost","root","","CO");

$sql = "SELECT PPM
FROM sensor S
WHERE Time1=(SELECT MAX(Time1) FROM sensor WHERE ID = S.ID);
";
$result=mysqli_query($mysqli,$sql);

if (!$result) {
    die("database connection failed");
}

echo "<table>";


while($data = mysqli_fetch_row($result))
{   

    echo "<tr>";
    echo "<td>$data[0]</td>";
    echo "</tr>";


}
echo "</table>";
?>

这是在单列中打印结果,而我想在 3 x 3 表中打印结果 这是html

<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.4.1.min.js"
  crossorigin="anonymous"></script>
<script type="text/javascript">

 $(document).ready(function() {

    $("#display").click(function() {                

      $.ajax({    
        type: "POST",
        url: "index1.php",             
        dataType: "html",                   
        success: function(response){                    
        $("#tab").html(response); 
        //alert(response);
        }
      });
    });

  });
</script>
</head>
<body>

<h2>CO Sensor Detector</h2>

<p>Table shows different values of CO sensors</p>

<table align="center">
   <tr>
       <td> <input type="button" id="display" value="Display All Data"> </td>

   </tr>
</table>
<div id="responsecontainer" align="center">
  <table id="tab">

  </table>
</div>

</body>
</html>

抱歉,如果我的问题格式不正确,我是个不擅长提问的菜鸟。任何帮助深表感谢。

【问题讨论】:

  • 您需要拆分单个值并将它们添加到每个 &lt;tr&gt;&lt;/tr&gt; 内的多个 &lt;td&gt;&lt;/td&gt; 元素中
  • @JDunken 也可以用theadtbody 来分隔你的代码。
  • @JDunken 是的,但问题是所有值都来自单个查询
  • 所以每个$data[0] 都是一个字符串?如果是这样,您需要先进行一些字符串操作。
  • @JDunken 在我的数据库中,我的 id 为 1-8,每个都有多个值,但 $data[0] 给了我每个值的最近时间

标签: javascript php jquery html css


【解决方案1】:

查看文档https://www.php.net/manual/en/mysqli-result.fetch-row.php

<?php
echo "
<table>
  <thead>
   <tr>
     <th>Fila 1</th>
     <th>Fila 2</th>
     <th>Fila 3</th>
   </tr>
  </thead>
  <tbody>
";
while($data = mysqli_fetch_row($result))
{   
    echo "
        <tr> 
          <td> $data[0] </td>
          <td> $data[1] </td>
          <td> $data[2] </td>
        </tr>
    ";
}
echo "</tbody></table>";
?>

【讨论】:

    【解决方案2】:

    我不知道这个 sql 语句收集了哪些数据,但是类似的东西可以工作

     <?php
    echo "
    <table>
      <thead>
       <tr>
         <th>Fila 1</th>
         <th>Fila 2</th>
         <th>Fila 3</th>
       </tr>
      </thead>
    ";
    while($data = mysqli_fetch_row($result))
    {   
        echo "
          <tbody>
            <tr> 
              <td> dato[0] </td>
            </tr>
            <tr> 
              <td> dato[1] </td>
            </tr>
            <tr> 
              <td> dato[2] </td>
            </tr>
    
          </tbody>
        ";
    
    
    }
    echo "</table>";
    ?>
    

    【讨论】:

    • 我试过这样做,但我所有的值都只为 $data[0] 输入,所以对于 $data[0],我想将结果分成 3 乘以 3
    • 然后将此数据传递给array 并创建for each 滚动并传递给表格
    • 目前这个答案是,它为每一行创建一个新的表体。而且里面还有很多错别字。
    猜你喜欢
    • 1970-01-01
    • 2017-10-06
    • 2016-07-05
    • 2015-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-12
    • 2019-08-26
    相关资源
    最近更新 更多