0

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

Qarar Ul Hassan
  • 141
  • 1
  • 13
  • `$pages['id']` from where you are getting this? – devpro Mar 19 '19 at 13:31
  • @devpro I have changed it. It is a page id on which product is showing. – Qarar Ul Hassan Mar 19 '19 at 13:33
  • so if `$item_id` is equal to 74 then featured item will be displayed? right? – devpro Mar 19 '19 at 13:34
  • @devpro correct. – Qarar Ul Hassan Mar 19 '19 at 13:35
  • is featured_in is comma seperated? why are u using strpos? – devpro Mar 19 '19 at 13:35
  • yes comma separated – Qarar Ul Hassan Mar 19 '19 at 13:35
  • instead of `strpos` use explode for `$items['featured_in']` then you can use in_array , becauase, for suppose if `$items['featured_in']` having 74,174,24 what should be happened? – devpro Mar 19 '19 at 13:36
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/190305/discussion-between-devpro-and-qarar-ul-hassan). – devpro Mar 19 '19 at 13:37
  • `foreach ($tariff as $tariff)` wouldn't this overwrite your $tariff cursor? (may not be related to your problem, but it wouldn't make solving it any easier if I am reading this correctly) – TheMouseMaster Mar 19 '19 at 14:03
  • @TheMouseMaster: no this is not an issue – devpro Mar 19 '19 at 14:04
  • look at this answer: https://stackoverflow.com/questions/2674011/mysql-check-if-numbers-are-in-a-comma-separated-list - using this you should be able to do all of this in a single query, if you JOIN panel_product on and the order by IF($pageID IN (featured_in),1,0). This will eliminate the connections in the loop which aren't ideal, and also you have the data in the order you want it and you just have to loop through and show the items with minimal work – imposterSyndrome Mar 19 '19 at 15:25

0 Answers0