Page 1 of 1

Parse/Setup a dynamic Multi-Dimensional Array??

Posted: Thu Jun 18, 2009 12:41 pm
by mischievous
Hey, I got a dooozy here... I believe I need to setup a three dimensional array. Im building a shopping cart and this is for the category page which contains sub categories which contain products.

So... the last array level is already completed i have a function that builds that for me which i posted below.

Code: Select all

 
function findProducts($cat_id){
        $query = "SELECT * FROM product LEFT JOIN skus ON product.cat_id=$cat_id WHERE product.pd_parent_sku_id = skus.id";
        $queryresult = $this->db->query($query);
        $resultarray = $queryresult->result_array();
        return $resultarray;
    }
 
Now the question is... im unsure how to setup the 2nd dimension to be the categories that those products lie in?

ex:

Code: Select all

<?php
$shop = array(array(array("rose", 1.25, 15),
                    array("daisy", 0.75, 25),
                    array("orchid", 1.15, 7) 
                   ),
              array(array("rose", 1.25, 15),
                    array("daisy", 0.75, 25),
                    array("orchid", 1.15, 7) 
                   ),
              array(array("rose", 1.25, 15),
                    array("daisy", 0.75, 25),
                    array("orchid", 1.15, 7) 
                   )
             );
?>
Here is the code running to try and generate the categories with the products

Code: Select all

 
function makeCategories($category)
    {
        $query = "SELECT * FROM category WHERE cat_alias_name = '$category'";
        $queryresult = $this->db->query($query);
        $row = $queryresult->row();
        $cat_id = $row->cat_id;
        $catquery = "SELECT * FROM category WHERE cat_parent_id = '$cat_id'";
        $catqueryresult = $this->db->query($catquery);
        $numofcats = $catqueryresult->num_rows();
        $n = 0;
        $cat_ids = array();
        foreach($catqueryresult->result() as $catqrow){
            $cat_ids[$n] = $catqrow->cat_id;
            $n++;
        }
        $category = array();
        for($i = 0; $i <= $numofcats-1; $i++)
        {
            $category[$i] = $this->findProducts($cat_ids[$i]);
        }
        return $category;
    }
 
The reason im doing it this way is I am running a parser in Code Igniter which is the MVC framework im running to do this application.
Here is the template being parsed:

Code: Select all

 
<div id='subpage_header'>
          <h1>{title}</h1>
          <?=quick_tempimage('page_divider.png')?> 
          {breadcrumbs}
      </div>
      <div id="subpage_container">
{categories}
    {category}
        <div class="products_container">
                    <div class="products_questions">
                      <div class="products_top"><!– filler for ie –></div>
                      <div class="products_mid">
                        <div class="product_category"><a href="garrett/{category}/{subcategory}">{pd_name}</a></div>
                        <div class="gototop"><a href="#top"></a></div>
                      </div>
                      <div class="products_bot"><!– filler for ie –></div>
                    </div>
                    <div class="product_items">
                    <div class="product_categoryimg"><img src="product_images/{sku}sm.jpg" width="180" height="180" /></div>
                    <div class="products_categoryitems"><a href="garrett-gti.php">GTI Series</a>
                    <p>{subcategory_description}</p>
                    <ul>
                        {product}
                          <li><a title="{pd_name}" href="/product/{pd_cartname}">{pd_name}</a></li>
                          {/product}
                    </ul>
                </div>
            </div>
        </div>
    {/category}
{/categories}
</div><!-Close subpage_container–>
 
:banghead: :banghead: :banghead: