开发者

PHP foreach sub categories trouble

开发者 https://www.devze.com 2023-02-10 08:05 出处:网络
I\'m having trouble creating a script where products with a certain sub category need to be printed within a certain DIV. At the moment I\'m pulling all the data I need but what\'s happening at the mo

I'm having trouble creating a script where products with a certain sub category need to be printed within a certain DIV. At the moment I'm pulling all the data I need but what's happening at the moment if for example I have 4 categories, it creates the DIV 4 times but the products in the DIV are all the same but I need them to be specific to the relevant ca开发者_StackOverflowtegory, hope that makes sense. This is what I have so far:

$categories = getCategories();

        foreach($categories as $category)
        {
        echo '<div class="panel">
            <div class="panel-wrapper"><pre>';
            echo print_r(getProducts($category['id_category']));
            echo '</pre>
                <h2 class="title">ff</h2>';


        echo '</div>
        </div>';
        }

$categories = array('1', '2', '3'); Products are pulled this way

$i = 0;
while ($row = mysql_fetch_assoc($result)) 
{
    $products[$i] = $row;
    $i++;
}
return  $products;

Which returns:

Array
(
    [0] => Array
        (
            [title] => Hat
            [description] =>nice description
            [visible] => 1
            [date-added] => 2011-02-10 20:02:02
            [date-updated] => 0000-00-00 00:00:00
            [price] => 10
            [id_category] => 2
        )
     [1] => Array etc etc etc 
 )

I'm quite new to php so I don't know how to run this loop without a foreach which is where I think i'm going wrong.

Thanks in advance!


First off, I'd like to mention that categories and subcategories should be in the same DB table, and subcategories should be marked with a field (such as subcategory_of) and should be associated with the ID of the category. So we don't have a mix-up of IDs, because products can be under both categories OR subcategories.

Initialize a general array that will go $array['category']['subcategory']['product_id'], or in some cases products may directly be in a category without a subcategory classification. You don't have to do this, you can just print outputs without saving them in an array.

Run through them starting from categories, for each category:

  1. Check if category has subcategories, if it does, get the products under that subcategory.
  2. Check if there are any products within the category.

So what you will have would be similar to this:

$cat = 0;
$categories = getCategories();

foreach ($categories as $category) {
    // If you want to print something about the category, do it here.
    $subcategories = getSubcategories($category_id);

    $subcat = 0;
    foreach ($subcategories as $subcategory) {
        $products = getProducts($category_id_for_subcategory) // see note 1

        foreach ($products as $product) {
            // This product is under a subcategory. Do what you want here.
            $array[$cat][$subcat++][] = $product; // or echo the product...
        }
     }

     $products = getProducts($category_id);
     foreach ($products as $product) {
         // This product is under a general category, and not a subcategory.
         // Do what you want to do with this product here.
         $array[$cat++][] = $product; // or echo the product.
     }
}

Note 1: Every subcategory is also considered a category, so we will use the subcategories' category IDs here. However, when we get subcategories into array, we should only get the ones that are associated with the $category_id provided in the parameter of the getSubcategories method. And these "get methods" should return arrays, so we can loop our way through them.

Note 2: I know that this is a potentially dangerous way of writing this code, since we are querying the database every time we iterate in a foreach loop. And like Marc said, this can be very inefficient if you have a lot of categories and products. The reason I wrote it like this is because it is verbose and allows a new programmer to understand the idea behind what we're doing easier (at least, it made it easier for me to understand it).

I hope this makes things clear for you.

0

精彩评论

暂无评论...
验证码 换一张
取 消