CodeIgniter Forums

Full Version: Retrieving information from arrays
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

I'm having trouble getting this to work.

Fist, I save information to theese arrays;
$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"}


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

<?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;

El Forum

[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:

$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:
&lt;? foreach ($i as $product) {
  echo '<img src="' . $product['Image'] . '" />';
  echo $product['Name'] . 'costs $' . $product['Price'];
  echo '<br />' . $product['Desc'];