Outputting two dimensional array - CodeIgniter

。_饼干妹妹 提交于 2020-01-14 05:17:07

问题


I'm trying to match subcategories with their respective parent categories. For now I just hard-coded in the main categories on the view (Books, Clothes etc) and have a function to get all subcategories, then in the view I can make a while loop to match every subcategory with its main one.

I've managed to get the data I want into an array, which now looks like this:

  array
  1 => 
    array 
      1 => string 'Medicine'
      2 => string 'Fairy tales' 
      3 => string 'Novels' 
    2 => 
    array
      44 => string 'Men's Clothing'
      45 => string 'Men's Accessories'
      49 => string 'Women's Swimwear' 
      50 => string 'Boys' 
      51 => string 'Girls' 
  3 => 
    array
      52 => string 'Perfume Sprays'
      53 => string 'Roll ons'
      54 => string 'Deodorants'
      64 => string 'Miscellaneous'

Where 1, 2, 3 are the main category ID's, and the array inside corresponds to subcat_id => subcat_name.

I've gotten this array by the following in my model:

function getsub()
{
    $this->db->select('sub_id,sub_name,cat_id');
    $q = $this->db->get('subcategories');

    $subcats = array();

    foreach($q->result() as $row)
    {
        $subcats[$row->cat_id][$row->sub_id] = $row->sub_name;
    }

    return $subcats;
}

Then in my view, under whatever category I have, I make a while loop to display all subcategories matching that cat_id:

<ul class="dropdown-menu">
<?php while($subcats['cat_id'] == 1)?>

<?php{?>

  <li><a href="main/sub_prod/<?php echo $subcats['sub_id'];?>">
<?php echo $subcats['sub_name'];?></a></li>

<?php}?>

</ul>

But I get an error on the line that makes each list item, saying 'undefined index: 'subcat_name'. The same is for subcat_id in that list item. Could someone tell me whats wrong? Thank you


回答1:


It has to do with the way you're trying to access the array elements. You're assigning it as

$subcategories[cat_id][subcat_id]

but accessing it as

$subcategories['subcat_id/name']

This is wrong in two ways:

  1. You're missing the cat_id. You need something like $subcategories[1][subcat_id]
  2. You don't have string keys defined. $subcategories[1]['subcat_id'] does not exist. Now, if you have a variable $subcat_id, then $subcategories[1][$subcat_id] would return the subcat_name you're looking for in the next line, not the subcat_id...

Another problem you've introduced is that you have an infinite loop. A good way to fix this is with a foreach loop, like the one you have in your model.

<?php foreach ($subcategories as $subcategory): ?>
<ul class="dropdown-menu">

  <?php foreach ($subcategory as $subcat_id => $subcat_name):
  <li><a href="customer/subcat_products/<?php echo $subcat_id; ?>">
  <?php echo $subcat_name; ?></a></li>
  <?php endforeach; ?>

</ul>
<?php endforeach; ?>

This will loop through all of your categories, printing out an unordered list with each subcategory as a list item.




回答2:


Try this:

    <ul class="dropdown-menu">
<?php $i=0; 
 while($subcategories[$i]['cat_id'] == 1){?>

  <li><a href="customer/subcat_products/<?php echo $subcategories[$i]['subcat_id'];?>">
<?php echo $subcategories[$i]['subcat_name'];?></a></li>

<?php $i++;
 }?>

</ul>

I've added a counter, the $i variable. The while loop doesn't iterate arrays



来源:https://stackoverflow.com/questions/15483536/outputting-two-dimensional-array-codeigniter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!