I am trying to show featured items on top in foreach loop and after showing featured items then it will show remaining items. For now I am using two loops to get feature first and then remaining after.
I have a column named featured_in
in a table name panel_products
and the parent loop is coming from table name tariff
because prices and other filtering options are in tariff
table.
So, I am doing like first get all options from tariff
table using foreach loop and then using foreign key post_id
from tariff
table to get other details of items.
so can we solve this in a single loop?
My code is:
$sql = $db_con->prepare("SELECT * FROM `panel_tariff` ORDER BY `price` ASC");
$sql->execute();
$tariff = $sql->fetchAll();
if (count($tariff) > 0) {
foreach ($tariff as $tariff) {
$item_id = $tariff['post_id']; // item id to use as foreign key
$sql = $db_con->prepare("SELECT * FROM `panel_product` WHERE `id` = '".$item_id."'");
$sql->execute();
$items = $sql->fetch(PDO::FETCH_ASSOC);
$itemsFID = $items['featured_in']; // To check if this item is featured
$pageID = '74'; // To check if this item featured in specific page
if (strpos($itemsFID, $pageID) === false) {
// Show featured items
}
}
}
So, what do you think. I know I should first try to google and search on internet and I tried but didn't find any solution so that's why I am writing here.
Regards, Qarar