我对php很新,我不知道如何很好地使用数组。这是交易,我想在我的数据库中添加三个或更多值的多维数组,然后我想根据时间戳(其中一个值)对它们进行排序。之后,我想显示所有已排序的值。我似乎无法做到这一点,这是代码
$queryWaitingPatients = 'SELECT ArrivalTime, TargetTime, Order, Classification FROM exams WHERE (CurrentState = "Pending")';
$results = mysql_query($queryWaitingPatients) or die(mysql_error());
if (mysql_num_rows($results) == 0) {
echo '<p>There\'s currently no patient on the waiting list.</p>';
return;
}
while ($rows = mysql_fetch_array($results)) {
extract($rows);
//now is the part that I don't know, putting the values into an array
}
// I'm also not sure how to sort this according to my $TargetTime
asort($sortedTimes);
//the other part I don't know, showing the values,
感谢您的帮助!
答案 0 :(得分:0)
$arr = array();
while ($rows = mysql_fetch_array($results)) {
array_push ($arr, $row);
}
print_r($arr);
答案 1 :(得分:0)
好吧,让我们来看看你的代码。首先,您有一个返回结果集的查询。我不建议使用mysql_fetch_array
,因为它不仅仅是deprecated(而是使用mysqli
函数),但它往往会导致错误的代码。当你的所有钥匙都是数字时,很难弄清楚你所引用的是什么。所以我建议mysqli_fetch_assoc(确保您已完全切换到mysqli
函数,例如mysql_connect
和mysqli_query
)
其次,我真的不喜欢使用提取物。我们需要直接使用数组。这就是我们如何做到这一点
$myarray = array();
while ($rows = mysqlI_fetch_assoc($results)) {
$myarray[] = $rows;
}
echo $myarray[0]['ArrivalTime'];
让我们回过头来看看。首先,我们正在构建一个数组数组。因此,我们初始化整体array
。然后我们想将行推到这个数组上。这就是$myarray[]
的作用。最后,我们推送的数组是关联的,这意味着该行的所有键都与查询的字段名称匹配。
现在,排序确实需要在您的查询中完成。所以让我们调整你的查询
$queryWaitingPatients = 'SELECT ArrivalTime, TargetTime, `Order`, Classification
FROM exams
WHERE CurrentState = "Pending"
ORDER BY TargetTime';
这样,当您的PHP运行时,您的数据库现在会以正确的数组顺序对它们进行搅拌。不需要排序代码。
答案 2 :(得分:0)
<?php
$queryWaitingPatients = ' SELECT ArrivalTime, TargetTime, Order, Classification, CurrentState
FROM exams
WHERE CurrentState = "Pending"
ORDER BY TargetTime ';
$results = mysql_query($queryWaitingPatients) or die(mysql_error());
if ($results -> num_rows < 1)
{
echo '<p>There\'s currently no patient on the waiting list.</p>';
}
else
{
while ($rows = mysqli_fetch_array($results))
{
$arrivaltime = $row['ArrivalTime'];
$targettime = $row['targettime'];
$order = $row['Order'];
$classification = $row['Classification'];
echo "Arrival: ".$arrivaltime."--Target time: ".$targettime."--Order: ".$order."--Classification: ".$classification;
}
}
echo "Done!";
//or you could put it in a json array and pass it to client side.
?>