JANNURM
JANNURM

Reputation: 331

how to send array from controller to view

I have a array in controller I have to send it to view and then show the value

The Controller page code

 public function showbookedmenu(){

 $rowLength=$this->input->post('rowno');
 $check=$this->input->post('checkeed');

    $j=0;
    for($i=1;$i<=$rowLength;$i++)
    {
        $check=$this->input->post('checkeed'.$i);

        if($check== "checked")
        {
        $orderedItem[$j][$i]   = $this->input->post('txtMenuItem'.$i);
        $orderedItem[$j][$i+1]  = $this->input->post('quantity'.$i);
        $orderedItem[$j][$i+2]    = $this->input->post('lebPrice'.$i); 
        $orderedItem[$j][$i+3]= $this->input->post('grandtotal'.$i); 
        $j++;
        }
        else{}
    }
    $data['order']= $orderedItem;
    $this->load->view('orderpage', $data);
 }

The view page

What will be the code??

Upvotes: 1

Views: 3196

Answers (2)

Mischa
Mischa

Reputation: 43298

First rewrite your controller action like this:

public function showbookedmenu()
{
  $rowLength = $this->input->post('rowno');    
  $orderedItem = array();

  for($i = 1; $i <= $rowLength; $i++)
  {
    $check = $this->input->post('checkeed'.$i);

    if($check == "checked")
    {
      $orderedItem[] = array(
        'menu_item' => $this->input->post('txtMenuItem'.$i),
        'quantity' => $this->input->post('quantity'.$i),
        'leb_price' => $this->input->post('lebPrice'.$i),
        'grand_total' => $this->input->post('grandtotal'.$i)
      );
    }
  }

  $data['order'] = $orderedItem;
  $this->load->view('orderpage', $data);
}

Then in your view you can do this:

<? foreach($order as $order_item): ?>
  <? echo $order_item['menu_item']; ?>
  <? echo $order_item['quantity']; ?>
  <? echo $order_item['leb_price']; ?>
  <? echo $order_item['grand_total']; ?>
<? endforeach; ?>

Upvotes: 3

FrontEnd Expert
FrontEnd Expert

Reputation: 5803

first try to print array value in your view file..

print_r($order);

if it is coming fine..

you can give it on

foreach ($order as $value):

and use it :

Upvotes: 0

Related Questions