输出二维数组 - CodeIgniter [英] Outputting two dimensional array - CodeIgniter

查看:168
本文介绍了输出二维数组 - CodeIgniter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将子类别与其各自的父类别进行匹配。现在我只是在视图(书籍,衣服等)的主要类别硬编码,并有一个函数来获取所有子类,然后在视图中,我可以做一个while循环匹配每个子类别与其主类。 p>

我已经设法获得我想要的数据到数组,现在看起来像这样:

  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'

其中,1,2,3是主类别ID, to subcat_id => subcat_name。



我在我的模型中得到了以下数组:

  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;
}

然后在我看来,显示与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>

但是我在一行上得到一个错误,使每个列表项,说'undefined index:'subcat_name '。同样的是该列表项中的subcat_id。有人能告诉我什么错了吗?谢谢

解决方案

它与您尝试访问数组元素的方式有关。您指定为

  $ subcategories [cat_id] [subcat_id] 

但访问它

  $ subcategories ['subcat_id / name'] 

这有两种错误:


  1. 您缺少cat_id。您需要类似于$ subcategories [1] [subcat_id]的

  2. 您没有定义字符串键。 $ subcategories [1] ['subcat_id']不存在。现在,如果你有一个变量$ subcat_id,那么$ subcategories [1] [$ subcat_id]将返回下一行中找到的subcat_name,而不是subcat_id ...

你介绍的另一个问题是你有一个无限循环。一个很好的方法来解决这个问题是一个foreach循环,像你在你的模型中一样。

 <?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; >

这将循环遍历所有类别,打印出一个无序列表,每个子类别作为列表项。


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

解决方案

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.

这篇关于输出二维数组 - CodeIgniter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆