I am trying to make a simple roster which will rotate alphabetically.
I have a database with a list of 10 users sorted by column called First_Name. I am using PHP to retrieve all the names of people from the database and put them into an array alphabetically.
What I need to be able to do is create a further 4 arrays to list say 3 people in each array. I can get all the users in a single array by doing: $query = "SELECT * FROM carpark";
The arrays should always be filled with 3 people and when the number of people run out it should add people from the start of the loop to fill the gaps.
The way I have it right now, is to return the next 3 names from the database table which is probably not going to work if i want to rotate them.
I hope what I am asking for makes some sense..
Here is what I have so far:
<?php
include_once 'connect.php';
dbconnect("roster");
//=============================================
$w1_query = "SELECT * FROM carpark ORDER BY First_Name LIMIT 0,3";
$w1_result = mysqli_query($conn, $w1_query) or die("error getting data");
$w1_arr = array();
while ($w1_data = mysqli_fetch_array($w1_result, MYSQLI_ASSOC)) {
array_push($w1_arr, $w1_data['First_Name']);
}
sort($w1_arr);
print_r($w1_arr);
//=============================================
//=============================================
$w2_query = "SELECT * FROM carpark ORDER BY First_Name LIMIT 3,5";
$w2_result = mysqli_query($conn, $w2_query) or die("error getting data");
$w2_arr = array();
while ($w2_data = mysqli_fetch_array($w2_result, MYSQLI_ASSOC)) {
array_push($w2_arr, $w2_data['First_Name']);
}
sort($w2_arr);
//print_r($w2_arr);
//=============================================
//=============================================
$w3_query = "SELECT * FROM carpark ORDER BY First_Name LIMIT 6,8";
$w3_result = mysqli_query($conn, $w3_query) or die("error getting data");
$w3_arr = array();
while ($w3_data = mysqli_fetch_array($w3_result, MYSQLI_ASSOC)) {
array_push($w3_arr, $w3_data['First_Name']);
}
sort($w3_arr);
//print_r($w3_arr);
//=============================================
//=============================================
$w4_query = "SELECT * FROM carpark ORDER BY First_Name LIMIT 9,11";
$w4_result = mysqli_query($conn, $w4_query) or die("error getting data");
$w4_arr = array();
while ($w4_data = mysqli_fetch_array($w4_result, MYSQLI_ASSOC)) {
array_push($w4_arr, $w4_data['First_Name']);
}
sort($w4_arr);
//print_r($w4_arr);
//=============================================
//print_r($week1_arr);
echo '<table border=1><tr><th></th><th>week 1</th><th>week 2</th><th>week 3</th><th>week 4</th></tr>';
for ($x = 0; $x <= 2; $x++) {
echo '<tr><td>Bay '.$x.'</td><td>'.$w1_arr[$x].'</td><td>'.$w2_arr[$x].'</td><td>'.$w3_arr[$x].'</td><td>'.$w4_arr[$x].'</td></tr>';
}
echo '</table>';
?>
Here is the output of the above, as you can see the last two boxes should be filled with Chris, Eric.
