简体   繁体   English

并排设置PHP生成的HTML表

[英]Setting php generated html table in parts side by side

I made this code to extract data from mysql and bring the results on the page. 我编写了此代码以从mysql提取数据并将结果显示在页面上。 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. 我希望它们并排放置,可能是30行,然后是下30行。 (After 3 columns of table,if there are still more data, they go on like this down the page if possible). (在表的3列之后,如果还有更多数据,则如果可能,它们会在页面下这样继续下去)。 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: 一个简单的例子来显示我需要的解决方案:

在此处输入图片说明

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

Do get the output you describe, you can fetch all rows at once into an associative array, then step through the columns in groups based on the number of columns to display. 得到您描述的输出后,您可以一次将所有行提取到关联数组中,然后根据要显示的列数逐步浏览各组中的列。

NOTE: I added an order by clause to your SQL statement. 注意:我将order by子句添加到您的SQL语句。

<?php
    $numRowsPerGroup = 30;
    $rowTitles = array(
        array('title' => "Ball1",'colname' => "Ball1"), 
        array('title' => "Ball2",'colname' => "Ball2"),
        array('title' => "Ball3",'colname' => "Ball3"),
        array('title' => "Ball4",'colname' => "Ball4"),
        array('title' => "Ball5",'colname' => "Ball5"),
        array('title' => "Ball6",'colname' => "Ball6")
    );
    $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 ORDER BY week";
    $result = $conn->query($sql);
    $numrows = $result->num_rows;
    $allrows = $result->fetch_all(MYSQLI_ASSOC);

    if($allrows) {
        $rownum = 0;
        while($rownum < $numrows) {
            //  Output heading row for this group
            echo "<table>\n";
            echo "<thead>\n";
            echo "<tr>\n";
            echo "  <th>&nbsp;</th>\n";
            //  Calculate the starting and ending column numbers. These correspond to the rows in the results
            $startColNo = $rownum;
            $endColNo = $rownum + $numRowsPerGroup;
            if($endColNo > $numrows) {
                $endColNo = $numrows;
            }
            //  Output the week column headers
            for($colNo = $startColNo;$colNo < $endColNo;$colNo++) {
                echo "  <th>".$allrows[$colNo]['week']."</th>\n";
            }
            echo "</tr>\n";
            echo "</thead>\n";
            echo "<tbody>\n";
            //  Output each item type row of the columns for this group.
            foreach($rowTitles as $idx => $rowInfo) {
                echo "<tr>\n";
                //  Step through the columns for this group for this item within the range.
                echo "  <td class='rowtitle'>".$rowInfo['title']."</td>\n";
                for($colNo = $startColNo;$colNo < $endColNo;$colNo++) {
                    echo "  <td>".$allrows[$colNo][$rowInfo['colname']]."</td>\n";
                }
                echo "</tr>\n";
            }
            echo "</tbody>\n";
            echo "</table>\n";
            $rownum = $endColNo + 1;
        }
    } else { 
        echo "<p>0 results</p>\n"; 
    }
    $conn->close();
?>

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 (ie width: 33%; ), so I will provide a solution that handles the PHP portion. 听起来好像您想要为每30个值并排显示的单独的表格,但是从您的图像看来,它们也将在3组30个组之后继续进入单独的行。该部分可以使用CSS(即width: 33%;进行处理width: 33%; ),所以我将提供一个解决PHP部分的解决方案。 The following uses a counter to separate each of group of 30 into a new table. 下面使用计数器将30个一组中的每一个分隔到一个新表中。

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

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

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