设置php生成的html表中的部分并排

3mpgtkmj  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(401)

我编写这段代码是为了从mysql中提取数据并将结果显示在页面上。代码生成的结果会在页面上显示。我想他们是并排设置,可能是30行,然后下30行(在表的3列之后,如果还有更多的数据,它们会像这样继续(如果可能的话)。我发现了一个类似的帖子,但由于我是新来的,我无法将提供的解决方案应用到我的代码中。任何帮助都将不胜感激。谢谢!
一个简单的例子来说明我需要的解决方案:

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

n8ghc7c11#

如果要获得所描述的输出,则可以一次将所有行提取到关联数组中,然后根据要显示的列数分组遍历列。
注意:我在sql语句中添加了orderby子句。

<?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();
?>
w6lpcovy

w6lpcovy2#

听起来您希望为每30个值并排显示一个单独的表,但是对于您的图像,它们似乎也会在30个值的三组之后转到一个单独的行。这个部分可以用css(即。 width: 33%; ),因此我将提供一个处理php部分的解决方案。下面使用一个计数器将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>

相关问题