开发者

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

开发者 https://www.devze.com 2023-04-07 01:23 出处:网络
Controller: $categorys = array( \'1234\' => array(\'Car Audio\',\'Car Subwoofers\'), \'12\'=> array(\'Car Stereos\')

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

精彩评论

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

关注公众号