DC> Thanks for the help but adding curly dose not seem to work.
DC> Any ideas why ?
DC>
DC>>> I get and output of [0][0][0][1][1][1][2][2][2] why ?
DC>
BR>> Try putting curly braces around your variables.  Like this:
BR>>
BR>> {$val[name][$i]}

Please reply to the list so that others can help you out, as well--and so others can learn from your questions, if they happen to have the same ones.

All right, I've taken a closer look at the code, and here's what's going wrong:

When you do foreach($vocals as $val), it only sees 3 "rows" in $vocals, one for name, one for skill, and one for fee. You can see this when you do print_r($vocals). Thus, it can't find $val[name][$i] or any of the other variables because they don't exist in $val. You could change it to $val[$i], but then you're table would print out like this:

Jonny Flash    Jonny Flash    Jonny Flash
77             77             77
39000          39000          39000

So, now you see a little bit of where the problem is. You need to rework your foreach to get things right. There are several ways you can do this. One is to store your array differently, like this:

$vocals = array(
          array('name' => 'Jonny Flash', 'skill' => 87, 'fee' => 22000),
          array('name' => 'Bill Banger', 'skill' => 77, 'fee' => 18500),
          array('name' => 'Sarah Jane', 'skill' => 93, 'fee' => 39000)
);

Then, you would print it out like this:

$rows = "<table>";
foreach($vocals as $val){
 $rows .= "
 <tr>
  <td>{$val[name]}</td><td>{$val[skill]}</td><td>{$val[fee]}</td>
 </tr>
 ";
}
$rows .= "</table>";
echo $rows;

Another way, using the same array you had, would be to print it out like this:

$rows = "<table>";
for ($i = 0; $i < count($vocals['name']); $i++) {
 $rows .= "
 <tr>

<td>{$vocals[name][$i]}</td><td>{$vocals[skill][$i]}</td><td>{$vocals[fee][$i]}</td>
 </tr>
 ";
}
$rows .= "</table>";
echo $rows;

However, I feel that it is more logical and easier to use to store the array in the way I suggested above.

--
Regards,
 Ben Ramsey
 http://benramsey.com
 http://www.phpcommunity.org/wiki/People/BenRamsey

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Reply via email to