Welcome Guest, Not a member yet? Register   Sign In
Retrieving information from arrays
#1

[eluser]Robb__[/eluser]
I'm having trouble getting this to work.

Fist, I save information to theese arrays;
Code:
$data['Image'][$i] = $resultrow2[$i] ->Image; // {../Images/test.jpg, ../Images/test2.jpg, ../Images/test.jpg)
$data['Name'][$i] = $resultrow3[$i] ->productName; //{"one", "two", "three"]
$data['Price'][$i] = $resultrow4[$i] ->Price; // {10,20,30)
$data['Desc'][$i] = $resultrow5[$i] ->Description; // {"first one, second, third"}

Code:
$this->load->view('index',$data);

Then in my index.php I want to retrieve this information, but it doesn't work?

Code:
<?php for ($i=0; $i< $5; $i++): ?>
<img src="&lt;?=${"Image$i"};?&gt;" onclick="showImage(&lt;?=${"Name$i"};?&gt;,&lt;?=${"Price$i"};?&gt;,'&lt;?=${"Desc$i"};?&gt;'); alt=""/&gt;</a>
&lt;?php endfor; ?&gt;
#2

[eluser]Michael Wales[/eluser]
I would store my information a bit differently. Rather than making a variable amount of 'Image' or 'Name' arrays, I would make a variable amount of arrays, each with a 'Name' or 'Image' key - for example:

Code:
$data[$i]['Image'] = $resultrow2[$i] ->Image; // {../Images/test.jpg, ../Images/test2.jpg, ../Images/test.jpg)
$data[$i]['Name'] = $resultrow3[$i] ->productName; //{"one", "two", "three"]
$data[$i]['Price'] = $resultrow4[$i] ->Price; // {10,20,30)
$data[$i]['Desc'] = $resultrow5[$i] ->Description; // {"first one, second, third"}

Then in your view:
Code:
&lt;? foreach ($i as $product) {
  echo '<img src="' . $product['Image'] . '" />';
  echo $product['Name'] . 'costs $' . $product['Price'];
  echo '<br />' . $product['Desc'];
?&gt;




Theme © iAndrew 2016 - Forum software by © MyBB