Im making a filter where you can select colors and select newest. It would filter them, but the order by doesn’t work for some reason.
I tried it this way. It outputs the colors that match the database table, but it doesn’t sort them by date.
$color_arr = ["red", "blue", "white"];
foreach($color_arr as $color) {
$data = $conn->query("SELECT * FROM `prod_items` WHERE item_color LIKE '%$color%' ORDER BY `item_date` DESC");
while ($row = $data->fetch()) {
print_r($row);
}
}
2
Answers
Don’t run sql query inside loop.
You can order by multiple fields, below is the MYSQL query:
This will first sort by item_date and then by price.