CodeIgniter Forums
Retrieving information from arrays - Printable Version

+- CodeIgniter Forums (
+-- Forum: Archived Discussions (
+--- Forum: Archived Development & Programming (
+--- Thread: Retrieving information from arrays (/thread-3775.html)

Retrieving information from arrays - El Forum - 10-21-2007

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;

Retrieving information from arrays - El Forum - 10-21-2007

[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'];