我从mysql数据库中获取了一些值,但db中的值不是有序的。我想将一周中的几天命名为SUN,MON,TUE等,并显示db表中的相应值。我目前正在显示db中的值而不进行排序。我将值添加到数组中。如何在html表中排序和显示值?这是我的代码
<?php
$q3 = mysqli_query($link,"SELECT * FROM stores_op_hours WHERE Store_Id='$stid' ");
?>
<table class="table ophours">
<thead class="thead-inverse">
<tr><th>Day</th><th>Open Time</th><th>Close Time</th></tr>
</thead>
<?php
$myarray = array();
while($rw = mysqli_fetch_array($q3)){
$day = $rw['Day_Name'];
$op = $rw['Open_Time'];
$cl = $rw['Close_Time'];
//$day = strtoupper($day);
$myarray[] = array("day" => $day, "open" => $op, "close" => $cl);
?>
<tbody>
<tr><td><?php echo $day; ?></td><td><?php echo $op; ?></td><td><?php echo $cl; ?></td></tr>
</tbody>
<?php
}
?>
</table>
答案 0 :(得分:1)
您可以做的是ORDER BY FIELD(id,3,2,1,4)
解决方案。然后在查询中,您将指定Day_Name字段的排序顺序,如下所示:
$q = sprintf("SELECT * FROM stores_op_hours WHERE Store_Id=%d
order by field(Day_Name,'SUN','MON','TUE','WED','THU','FRI','SAT')",(int) $stid);
答案 1 :(得分:0)
所以最后的编辑(感谢@Erik Dohmen)应该是这样的:
$q = sprintf("SELECT * FROM stores_op_hours WHERE Store_Id=%d
order by field(Day_Name,'SUN','MON','TUE','WED','THU','FRI','SAT')",
(int) $stid);
$q3 = mysqli_query($link,$q);