1

in controller i send the object to view like:

public function comapre_array()
{

 $data['pro1']="result from model";   //product details

 $data['pro2']="result from model";   //product details

 $data['pro3']="result from model";   //product details

  $this->load->view('user/product_comp',$data);
}

and in my view i need to display that in table to comparison,

i need to take the three array value in single loop using foreach,is it is possible,

i tried like this

foreach($pro1 as $m,$pro2 as $n,$pro3 as $o){
    //printing values
}

but it shows error,

please suggest me is there is any other ideas to implement this,

4
  • 1
    You already have the data you need in $data. Just loop over it, all the values are there. And no, you can't do what you suggested. Commented Sep 26, 2016 at 10:04
  • can you please show the method to print the data inside $data Commented Sep 26, 2016 at 10:06
  • print_r seems to work nice. Consider reading some tutorials or watching a few videos about basic things in php. Commented Sep 26, 2016 at 10:08
  • thanks for your information, i know print_r print all those things in array i don't want these explanation. i need to get all those record in single table to compare the products, if you know the method please otherwise i don't need any irrelevant ans. Commented Sep 26, 2016 at 10:15

2 Answers 2

1

Make your data set as,

$data['product']['pro1']="result from model";   //product details
$data['product']['pro2']="result from model";   //product details
$data['product']['pro3']="result from model";   //product details

Basically put your products under a new array key called product.

And in your view, get the $product first and loop it.

foreach( $product as $prod_key => $prod_value ){
    // $prod_key = pro1, pro2, pro3
    // $prod_value = product details
}

Hope this helps!

Sign up to request clarification or add additional context in comments.

Comments

0

If your different arrays have the same key structure you could do something like that :

foreach($pro1 as $key => $pro){


    if(isset($pro2[$key],$pro3[$key])) {
        //print your values here
    } else {
        //Your arrays don't have the same key structure
    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.