display list of data related data in column

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Tokunbo
Forum Commoner
Posts: 46
Joined: Thu Sep 29, 2011 8:53 am

display list of data related data in column

Post 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
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: display list of data related data in column

Post by Celauran »

Why the underscores? Why not use arrays?

Code: Select all

$fname = array(1 => 'dog', 2 => 'cat', 3 => 'mouse');
Tokunbo
Forum Commoner
Posts: 46
Joined: Thu Sep 29, 2011 8:53 am

Re: display list of data related data in column

Post 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?
jraede
Forum Contributor
Posts: 254
Joined: Tue Feb 16, 2010 5:39 pm

Re: display list of data related data in column

Post 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>';
} 
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: display list of data related data in column

Post 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.
Tokunbo
Forum Commoner
Posts: 46
Joined: Thu Sep 29, 2011 8:53 am

Re: display list of data related data in column

Post by Tokunbo »

@Celauran

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

regards
Tokunbo
Post Reply