dongming5444 2018-04-09 17:19
浏览 22
已采纳

将php生成的html表并排设置

I made this code to extract data from mysql and bring the results on the page. The results that the code generated goes down the page. I would like them to be set side by side, may be 30 rows, then the next 30 rows. (After 3 columns of table,if there are still more data, they go on like this down the page if possible). I found a similar post about this, but since I am new to this,I couldn't apply the offered solution there to my code. Any help would be greatly appreciated. Thanks!

A simple illustration to show the solution I need:

enter image description here

<table class="result-table">
 <tr>
  <th>Week</th> 
  <th>Ball1</th> 
  <th>Ball2</th>
  <th>Ball3</th>
  <th>Ball4</th>
  <th>Ball5</th>
  <th>Ball6</th>
 </tr>


 <?php
$conn = mysqli_connect("localhost", "root", "", "db");
  // Check connection
  if ($conn->connect_error) {
   die("Connection failed: " . $conn->connect_error);
  } 

  $sql = "SELECT Week, Ball1, Ball2, Ball3, Ball4, Ball5, Ball6 FROM sample";
  $result = $conn->query($sql);
  if ($result->num_rows > 0) {
   // output data of each row
   while($row = $result->fetch_assoc()) 
   {
    echo "<tr><td>" . $row["Week"]."</td><td>" . $row["Ball1"] . "</td><td>"
. $row["Ball2"]. "</td><td>". $row["Ball3"]. "</td><td>". $row["Ball4"].  "</td><td>".$row["Ball5"]. "</td><td>". $row["Ball6"]. "</td></tr>";
}
echo "</table>";

} else { echo "0 results"; }
$conn->close();
?>     


</table>
  • 写回答

2条回答 默认 最新

  • dongnanbi4942 2018-04-09 18:07
    关注

    It sounds like you want separate tables for each 30 values displayed side-by-side, but with your image it appears that they would also go on to a separate row after three groups of 30. That part can be handled with CSS (i.e. width: 33%;), so I will provide a solution that handles the PHP portion. The following uses a counter to separate each of group of 30 into a new table.

    <table class="result-table">
     <tr>
      <th>Week</th> 
      <th>Ball1</th> 
      <th>Ball2</th>
      <th>Ball3</th>
      <th>Ball4</th>
      <th>Ball5</th>
      <th>Ball6</th>
     </tr>
    
    
     <?php
    $conn = mysqli_connect("localhost", "root", "", "db");
      // Check connection
      if ($conn->connect_error) {
       die("Connection failed: " . $conn->connect_error);
      } 
    
      $sql = "SELECT Week, Ball1, Ball2, Ball3, Ball4, Ball5, Ball6 FROM sample";
      $result = $conn->query($sql);
      if ($result->num_rows > 0) {
    
      // HERE IS WHERE I ADD A COUNTER
      $counter = 1;
       // output data of each row
       while($row = $result->fetch_assoc()) 
       {
            echo "<tr><td>" . $row["Week"]."</td><td>" . $row["Ball1"] . "</td><td>"
                . $row["Ball2"]. "</td><td>". $row["Ball3"]. "</td><td>". $row["Ball4"].  "</td><td>".$row["Ball5"]. "</td><td>". $row["Ball6"]. "</td></tr>";
    
    
                // HERE I INCREMENT THE COUNTER, AND REPEAT THE END OF THE CURRENT TABLE/BEGINNING OF THE NEXT IF $counter%30 == 0
                $counter++;
                if($counter % 30 == 0) { ?>
                    </table>
                    <table class="result-table">
                         <tr>
                          <th>Week</th> 
                          <th>Ball1</th> 
                          <th>Ball2</th>
                          <th>Ball3</th>
                          <th>Ball4</th>
                          <th>Ball5</th>
                          <th>Ball6</th>
                         </tr>
                <?php }
        }
    echo "</table>";
    
    } else { echo "0 results"; }
    $conn->close();
    ?>     
    
    
    </table>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!