Page 1 of 1

display list of data related data in column

Posted: Tue Oct 11, 2011 8:22 pm
by Tokunbo
hello sirs,

suppose I have some list of data such as below where $fname_xyz is related to $age_xyz:

$fname_1 = dog;
$fname_2 = cat;
$fname_3 = mouse;
$age_1 = 5;
$age_2 = 10;
$age_3 = 15;

the data is not in a database. Now I want to display the above in columns with headers "animal, age, etc... and color for example, an additional column of data is there in the future"

here is what ive been able to cook up, its not working coz im trying to figure out the underscore part and use it to display the above list:

Code: Select all


<?php
$d=1;
$under=_;
echo "<table border=\"1\" align=\"center\">";
echo "<tr><th>Name</th>";
echo "<th>Ages</th></tr>";
while ( $d <= 3 ) 
{
	echo "<tr><td>";
	echo $fname$under$d;
	echo "</td><td>";
	echo "$fname$under$d";
	echo "</td></tr>";
	$d = $d + 1;
}
echo "</table>"; 

pls help

Re: display list of data related data in column

Posted: Tue Oct 11, 2011 8:40 pm
by Celauran
Why the underscores? Why not use arrays?

Code: Select all

$fname = array(1 => 'dog', 2 => 'cat', 3 => 'mouse');

Re: display list of data related data in column

Posted: Tue Oct 11, 2011 8:48 pm
by Tokunbo
the underscores are just to know which data is associated with which other data

arrays?

what if the data itself is coming from a form - as in how would $fname look like if values "dog, cat and mouse" come from a form with post?

and would this mean I would have 2-arrays: one for the names and one for the ages, etc?

Re: display list of data related data in column

Posted: Wed Oct 12, 2011 1:41 am
by jraede

Code: Select all

$ages = array('dog'=>5, 'cat'=>10, 'mouse'=>15);
foreach($ages as $animal=>$age) {
     echo '<tr><td>';
     echo $animal;
     echo '</td><td>';
     echo $age;
     echo '</td></tr>';
} 

Re: display list of data related data in column

Posted: Wed Oct 12, 2011 6:18 am
by Celauran
Tokunbo wrote:what if the data itself is coming from a form - as in how would $fname look like if values "dog, cat and mouse" come from a form with post?
You could have the form return arrays by using, say, name="age[]" rather than name="age_1", name="age_2" etc.

Re: display list of data related data in column

Posted: Thu Oct 13, 2011 12:54 pm
by Tokunbo
@Celauran

ow, thanks for the tip, I didnt know that.

regards
Tokunbo