Splitting array table

Hi all, i am quite new to PHP and i was after a little help.
I am building a leaderboard using PHP & SQL for a talent competition, it is working as i want except for one little thing. I have written a page that displays all of the id’s (for text voting), names and scores auto arranging by score and formats it into a 3 column table . If i have 30 names this would obviously give me a table with 30 rows. My question is is it possible to split this into 2 tables side by side of 15 rows so i can make the font bigger and easier to read on a screen?

The database table has 3 columns (id, name, vote_count)

Here is my code so far.


<?php require_once('connectvars.php'); // Connect to the database $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); // Retrieve the user data from MySQL $query = "SELECT * FROM students WHERE name IS NOT NULL ORDER BY vote_count DESC, name"; $data = mysqli_query($dbc, $query); // Loop through the array of user data, formatting it as HTML echo ''; echo ''; while ($row = mysqli_fetch_array($data)) echo ' '; echo '
id Act Score
' . $row['id'] . ' ' . $row['name'] . ' ' . $row['vote_count'] . '
'; mysqli_close($dbc); ?>


Any help would be greatly appreciated

Its possible, but putting them side-by-side would make the font smaller, not larger since you’re putting more information on the screen. You only have 3 columns, how are you displaying it now?

At the moment it displays 1 table with 3 columns and 30 rows. My idea is that this would be displayed on one page that i will display full screen within my browser and it is set to auto update. I don’t want to have to scroll the page to see the bottom entries so i have to make the font small. I thought if i could split the table in 2 (side by side) i could make the font bigger and there won’t be any need to scroll.

does this make sense?

I can split the results into 2 tables by sorting by ‘id’ and the requesting the first table to display ‘id < 16’ and the second table ‘id >14’. This works but i really want to sort the tables by vote_count DESC and therefore the id’s aren’t in order and the above solution doesn’t work.

Sure you can. I think what you should do is read all rows into an array, then use array_chunk() to split the array into sections. Then you can simply loop and create a new table for each section.

Thanks for the reply m@tt, I’ve had a look into this and i can see how it would be the solution but its a bit above my skill level at the moment. I get how it works with inputing array values but how would i get my values from my database into the array? ‘id’, ‘name’ & ‘vote_count’.
My original code is above if you could give me some clues to a solution that would be amazing.


$query = “SELECT * FROM students WHERE name IS NOT NULL ORDER BY vote_count DESC, name”;
$data = mysqli_query($dbc, $query);

// build rows array
$rows = array();
while($row = mysqli_fetch_array($data)) {
$rows[] = $row;

$chunk_size = ceil(count($rows) / 2); // divide total number of rows by number of tables
$chunks = array_chunk($rows, $chunk_size); // create chunks

// loop chunks
foreach($chunks as $chunk) {
// start table
echo “<table border=“1” cellpadding=“5”>\n”;
echo “

echo “id\n”;
echo “Act\n”;
echo “Score\n”;
echo “\n”;
// loop rows
foreach($chunk as $row) {
	echo "<tr>\n";
	echo 	"<td>" . $row['id'] . "</td>\n";
	echo 	"<td>" . $row['name'] . "</td>\n";
	echo 	"<td>" . $row['vote_count'] . "</td>\n";
	echo "</tr>\n";

// end table
echo "</table>\n";


m@tt you are a legend but can i be really cheeky and ask just one more thing of you. Is there any possible way the 2 tables could be side by side?

Ha Ha cracked it myself thanks to a bit of searching on google

Changed the line

[php]echo <table border=“0” cellpadding=“5”>\n";[/php]


[php]echo <table style=“float: left;” border=“0” cellpadding=“5”>\n";[/php]

Thanks a lot m@tt for all your help.

You could float them like that, or you could just define the width.

echo “<table border=“1” cellpadding=“5” width=“300”>\n”;[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service