Yohan Blake Yohan Blake - 5 months ago 8
SQL Question

Add values from db to array, sort and display

I am getting some values from a mysql database, but the values in the db are not in order. I want to order the days of the week as SUN, MON, TUE, etc. and display the corresponding values from the db table. I am currently displaying the values from the db without sorting. And I add the values to an array. How can I sort and show the values in an html table? This is my code

$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>
$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);
<tr><td><?php echo $day; ?></td><td><?php echo $op; ?></td><td><?php echo $cl; ?></td></tr>


What you could do is the ORDER BY FIELD(id,3,2,1,4) solution. Then in the query you would specify the sort order for the Day_Name field, like so:

$q = sprintf("SELECT * FROM stores_op_hours WHERE Store_Id=%d 
order by field(Day_name,'SUN','MON','TUE','WED','THU','FRI','SAT','SUN')",(int) $stid);