[PHP] RE: Simple one I think

2004-03-31 Thread Dave Carrera
Thanks for the insight Ben,

Straight forward when I looked at your code examples and this answers a
couple of other things to.

Thanks once again for your clear help

Dave C
-Original Message-
From: Ben Ramsey [mailto:[EMAIL PROTECTED] 
Sent: 30 March 2004 17:08
To: Dave Carrera
Subject: Re: Simple one I think


DC Thanks for the help but adding curly dose not seem to work. Any 
DC 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 FlashJonny FlashJonny 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]}/tdtd{$val[skill]}/tdtd{$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]}/tdtd{$vocals[skill][$i]}/tdtd{$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



---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.645 / Virus Database: 413 - Release Date: 28/03/2004
 

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.645 / Virus Database: 413 - Release Date: 28/03/2004
 

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



[PHP] Re: Simple one I think

2004-03-30 Thread Ben Ramsey
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 FlashJonny FlashJonny 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]}/tdtd{$val[skill]}/tdtd{$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]}/tdtd{$vocals[skill][$i]}/tdtd{$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


[PHP] Re: Simple one I think

2004-03-29 Thread Ben Ramsey
DC I get and output of [0][0][0][1][1][1][2][2][2] why ?

Try putting curly braces around your variables.  Like this:

{$val[name][$i]}

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