开发者

Codeigniter: how can I send a foreach statement to the view?

Controller:

$categorys = array(
    '1234' => array('Car Audio','Car Subwoofers'), 
    '12'   => array('Car Stereos')
)

$category_id = $this->开发者_JAVA百科input->get('category_id');
$product_id  = $this->input->get('modelNumber');

if (array_key_exists($category_id,$categorys)) 
{
        foreach($categorys[$category_id] as $key => $value) 
    {
            echo $value;
    }
}

How can I echo the $value outputted from the foreach statement in my view file?


You can pass the whole array to the view and run the foreach directly in the view, e.g.:

$data['array'] = array(/* etc. */);
$this->load->view('view', $data);

And in the view:

<?php foreach($array as $key => $value): ?>
  <p>The key is <?php echo $key; ?><br>
  The value is <?php echo $value; ?></p>
<?php endforeach; ?>


Controller

if (array_key_exists($category_id,$categorys)) 
{
     $query['cats'] = $categorys[$category_id];
}

View

foreach($cats as $key => $value) 
{
   echo $value;
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜