Reputation: 940
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>
Upvotes: 0
Views: 209
Reputation: 5040
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> </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();
?>
Upvotes: 2
Reputation: 1515
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>
Upvotes: 1