Search code examples
jquerylistcakephpjquery-ui-accordionsubitem

jquery accordian List submenu item can't display values from database in cakephp


i am using a jquery accordian for display a list with sub items. i have a category table and product table. i want to fetch these categoris and corresponding products to the list. in my controller i add the below code.

    $category=$this->Category->find('list',
    array('fields'=>array('id','category_name')));
    $this->set('category',$category);

    $productlist=$this->Product->find('list',
    array('fields'=>array('id','product_name','category_id')));
    $this->set('productMenu',$productlist);

And in my list i added the list values

        <ul id="nav"><?php
  foreach ($category as $key => $value)
  {
  ?><li><a href="#"><?php echo $value; ?></a>
    <ul class="sub">
    <?php foreach ($productMenu as $key => $value )
  { ?>

  <li><a href="#"><?php echo $value; ?></a>
 <?php } ?>
      </ul>
     </li><?php
  }
  ?></ul>

Now the category is showing well, and i didont get the logic to add products that belongs to particular categry. if anybody know this please help me. iam stucking..

also when i debug this result i got the result

        debug($catgory)

        array(
(int) 1 => 'Crowns And Bridges Non Metal',
(int) 2 => 'Crowns And Bridges Metal',
(int) 3 => 'Implants',
(int) 4 => 'Dentures'
             )

        debug($product)

        array(
(int) 1 => array(
    (int) 1 => 'Lava',
    (int) 2 => 'ZR Crown'
),
(int) 2 => array(
    (int) 3 => 'Porcelain fused to metal crowns'
),
(int) 3 => array(
    (int) 4 => 'General Restorative',
    (int) 5 => 'Customised Abutments',
    (int) 6 => 'Avinent Implants'
),
(int) 4 => array(
    (int) 7 => 'Partial Dentures',
    (int) 8 => 'Complete Dentures:'
)

Solution

  • Your quite near, assuming that the products in the nested array belong to the category with the same ID (I mean, that product 4, 5 and 6 actually belong to category 3 'implants'.

    Then this should work when it comes to nesting. I didn't look at the format required for the jQuery thing:

    <ul id="nav">
        <?php
        foreach ($category as $cat_id => $cat_value) { ?>
            <li><a href="#"><?php echo $cat_value; ?></a>
                <ul class="sub">
                <?php foreach ($productMenu[$cat_id] as $product_id => $product_value )
                    {
                    ?>
                        <li><a href="#"><?php echo $product_value; ?></a>
                    <?php } // end foreach product ?>
                </ul>
            </li>
        <?php } // end foreach category?>
    </ul>
    

    This is a bit of work which Cake can do for you by binding the models together. Take a look at: http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html for more information.