Setting php generated html table in parts side by side

168
April 10, 2018, at 09:05 AM

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:

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

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

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.

<?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();
?>
Rent Charter Buses Company
READ ALSO
Possible to do a MySQL foreign key to one of two possible tables?

Possible to do a MySQL foreign key to one of two possible tables?

Well here's my problem I have three tables; regions, countries, statesCountries can be inside of regions, states can be inside of regions

135
Delete a row after 1 month or 3 month based on selected validity

Delete a row after 1 month or 3 month based on selected validity

I want to delete old records in my database that stores an entry from an HTML form which consists of

143
Audit Trail concept in php, mysql [on hold]

Audit Trail concept in php, mysql [on hold]

I want to use Audit trail concept in php and mysql web applicationI need to track which user edit which field at what time frame? Also i need to compare the current version of saved data with previous version

131
Laravel eloquent query table names case insensitive

Laravel eloquent query table names case insensitive

Is it possible to make eloquent queries case insensitive? An issue came when I decided to move my website to production (move from my local machine to the host server)MariaDB on my host server is case sensitive and I don't have an access to its config...

240