Jump to content

[SOLVED] 2 columns when displaying array


Recommended Posts

With this example script on tizag.com I can display the array with 1 record per line. But how can I make it display in 2 columns?

<?php
echo "<table>";

$employeeAges["Lisa"] = "28";
$employeeAges["Jack"] = "16";
$employeeAges["Ryan"] = "35";
$employeeAges["Rachel"] = "46";
$employeeAges["Grace"] = "34";
$employeeAges["Robert"] = "32";

foreach( $employeeAges as $key => $value){
echo "<tr>";
echo "<td style=\"border: 1px solid black;\">";
echo "Name: $key, Age: $value <br />";
echo "</td>";
echo "</tr>";
}
echo "</table>";
?>

 

it comes up like...

Name: Lisa, Age: 28

Name: Jack, Age: 16

Name: Ryan, Age: 35

Name: Rachel, Age: 46

Name: Grace, Age: 34

Name: Robert, Age: 32

 

but I want to make it come up like...

Name: Lisa, Age: 28  Name: Jack, Age: 16

Name: Ryan, Age: 35  Name: Rachel, Age: 46

Name: Grace, Age: 34  Name: Robert, Age: 32

 

Regards, ACE

Link to comment
Share on other sites

did this quickly but it should work.


<?php
echo "<table>";

$employeeAges["Lisa"] = "28";
$employeeAges["Jack"] = "16";
$employeeAges["Ryan"] = "35";
$employeeAges["Rachel"] = "46";
$employeeAges["Grace"] = "34";
$employeeAges["Robert"] = "32";

$count = 0;

foreach( $employeeAges as $key => $value){

if($count%2 == 0)
	echo "<tr>";

echo "<td style=\"border: 1px solid black;\">";
echo "Name: $key, Age: $value ";
echo "</td>";

if($count%2 == 1) 
	echo "</tr>";

$count++;
}
echo "</table>";
?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.