我正在尝试制作一个按字母顺序轮换的简单名单。
我有一个数据库,其中包含按名称为First_Name的列排序的10个用户的列表。我正在使用PHP从数据库中检索所有人的名字,并按字母顺序将它们放入数组中。
我需要做的是创建另外4个数组来列出每个数组中的3个人。我可以通过执行以下操作获取单个阵列中的所有用户:$query = "SELECT * FROM carpark";
阵列应该总是由3个人填充,当人数用完时,应该从循环开始添加人员以填补空白。
我现在拥有它的方法是从数据库表中返回接下来的3个名字,如果我想旋转它们可能不会起作用。
我希望我所要求的是有道理的。
这是我到目前为止所做的:
<?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>';
?>
以上是上述内容的输出,您可以看到最后两个框应填充Chris
,Eric
。
答案 0 :(得分:3)
也许是这样的,假设你有一个$names
数组中的所有名字,带有数字键(array_merge
所需功能所需)。
$numBays = 3;
$numWeeks = 4;
$weeks = [];
$pool = [];
do {
// Fill pool of names to a point where we have at least $numBays names
while (count($pool) < $numBays) {
$pool = array_merge($pool, $names);
}
// Extract an array containing $numBays names from the name pool
$weeks[] = array_splice($pool, 0, $numBays);
} while(count($weeks) < $numWeeks);
这应该为您留下一个$weeks
数组,其中包含$numWeeks
个数组,每个数组都有$numBays
个名称,您可以像现在这样循环,以产生输出。
for ($bay = 0; $bay < $numBays; $bay++) {
echo '<tr><td>Bay '.$bay.'</td>';
for ($week = 0; $week < $numWeeks; $week++) {
echo '<td>'.$weeks[$week][$bay].'</td>';
}
echo '</tr>';
}
答案 1 :(得分:3)
根据系统的复杂程度,您可以将结果http://php.net/manual/en/function.array-merge.php与自身合并,然后使用array_chunk http://php.net/manual/en/function.array-chunk.php将其拆分。
$query = "SELECT * FROM carpark ORDER BY First_Name";
$result = mysqli_query($mysqli, $query) or die("error getting data");
$arr = array();
while ($data = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
array_push($arr, $data['First_Name']);
}
sort($arr);
$combined=array_merge($arr,$arr);
$output=array_chunk($combined,3);
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>'.$output[0][$x].'</td> <td>'.$output[1][$x].'</td><td>'.$output[2][$x].'</td><td>'.$output[3][$x].'</td></tr>';
}
echo '</table>';
答案 2 :(得分:1)
根据需要创建数组会很容易。希望这会奏效。数组将根据为$ NoOfRows&amp;分配的值生成$ NoOfWeeks
$NoOfRows = 3;
$NoOfWeeks = 4;
$totalLots = $NoOfRows * $NoOfWeeks;
$dataArr = array();
//fetch all persons
$query = "SELECT * FROM carpark ORDER BY First_Name";
$result = mysqli_query($conn, $query) or die("error getting data");
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
array_push($dataArr, $row['First_Name']);
}
$noOfPersons = count($dataArr);
//Create a new array with filling all lots
while ($noOfPersons < $totalLots) {
$dataArr = array_merge($dataArr, $dataArr);
$noOfPersons = count($dataArr);
}
$weekArr = array_chunk($dataArr , $NoOfRows);
//print the table
echo "<table border=1>";
for ($row = 0; $row < $NoOfRows; $row++) {
if ($row == 0) {
echo "<tr><td></td>";
for ($col = 1; $col <= $NoOfWeeks; $col++) {
echo "<td>Week $col</td>";
}
echo "</tr>";
}
for ($col = 0; $col < $NoOfWeeks; $col++) {
if ($col == 0) {
echo "<tr><td>Bay $row</td>";
}
echo "<td>";
echo $weekArr[$col][$row];
echo "</td>";
}
echo "</tr>";
}
echo "</table>";