Jump to content

Place foreach Items in Columns Instead of Rows


aquastream

Recommended Posts

So I went to http://www.learnphp-tutorial.com/Arrays.cfm and saw their code sample on two-dimensional arrays. Here's the code:

 

<html>
<head>
<title>Two-dimensional Arrays</title>
</head>
<body>
<h1>Two-Dimensional Arrays</h1>
<?php
$Rockbands = array(
array('Beatles','Love Me Do', 'Hey Jude','Helter Skelter'),
array('Rolling Stones','Waiting on a Friend','Angie','Yesterday\'s Papers'),
array('Eagles','Life in the Fast Lane','Hotel California','Best of My Love')
);
?>
<table border="1">
<tr>
<th>Rockband</th>
<th>Song 1</th>
<th>Song 2</th>
<th>Song 3</th>
</tr>
<?php
foreach($Rockbands as $Rockband)
{
echo '<tr>';
foreach($Rockband as $item)
{
  echo "<td>$item</td>";
}
echo '</tr>';
}
?>
</table>
</body>
</html>

 

Seeing the way that they used HTML in order to create the table headers, I thought I could do better than that and instead list the header names in an array, use a foreach function and then echo the results back. However, this isn't the result I'm getting as you can see below.

 

<html>
<head>
<title>Array Test</title>
</head>
<body>
<?php
    $Rockband_Header = array("Rockband","Song 1","Song 2","Song 3");
$Rockbands = array(
array("Beatles","Love Me Do","Hey Jude","Helter Skelter"),
array("Rolling Stones","Waiting on a Friend","Angie","Yesterday's Papers"),
array("Eagles","Life in the Fast Lane","Hotel California","Best of My Love")
);
?>
    <table border="1">
    <?php
foreach ($Rockband_Header as $Header) {
echo "<tr>";
foreach ($Rockbands as $Rockband) {
echo "<th>$Header</th>";
foreach ($Rockband as $Items)
echo "<td>$Items</td>";
}
echo "</tr>";
}
?>
    </table>
</body>
</html>

 

On looking back at the code, I noticed that instead of the <th> for $Header being located inside a single <tr>, somehow the code is resulting in a new <tr> being produced for each of the <th>'s being echoed. So how do I get the <th>'s to be located inside a single <tr>, and therefore result in the same appearance as the original code above?

try

<html>
<head>
<title>Two-dimensional Arrays</title>
</head>
<body>
<h1>Two-Dimensional Arrays</h1>
<?php
$Rockbands = array(
array('Rockband', 'Song 1', 'Song 2', 'Song 3'),
array('Beatles','Love Me Do', 'Hey Jude','Helter Skelter'),
array('Rolling Stones','Waiting on a Friend','Angie','Yesterday\'s Papers'),
array('Eagles','Life in the Fast Lane','Hotel California','Best of My Love')
);

?>
<table border="1">
<?php
foreach($Rockbands as $Rockband)
{
echo '<tr>';
foreach($Rockband as $item)
{
  echo "<td>$item</td>";
}
echo '</tr>';
}
?>
</table>
<hr />
<?php
$Rockbands1= array();
foreach ($Rockbands as $i => $Rockband){
foreach ($Rockband as $j => $value) $Rockbands1[$j][$i] = $value;
}
echo '<table border="1">';
foreach($Rockbands1 as $Rockband)
{
echo '<tr>';
foreach($Rockband as $item)
{
  echo "<td>$item</td>";
}
echo '</tr>';
}
?>
</table>
</body>
</html>

Thanks sasa for the reply. I already tried your first example before posting. While it places the contents into the table properly, I was looking to specifically set the first row as a table header so that it displays and bold. Your second example however was useful as it gives me a few things to keep in mind to change rows of an array into columns.

If you're using sasa's code above

try

...
<?php
$Rockbands1= array();
foreach ($Rockbands as $i => $Rockband){
foreach ($Rockband as $j => $value) $Rockbands1[$j][$i] = $value;
}
echo '<table border="1">';
foreach($Rockbands1 as $Rockband)
{
echo '<tr>';
foreach($Rockband as $item)
{
  echo "<td>$item</td>";
}
echo '</tr>';
}
?>
...

Change

foreach($Rockbands1 as $Rockband)
{
echo '<tr>';
foreach($Rockband as $item)
{
  echo "<td>$item</td>";
}
echo '</tr>';
}

To

foreach($Rockbands1 as $key => $Rockband)
{
$tag = ($key === 0) ? 'th' : 'td';
echo '<tr>';
foreach($Rockband as $item)
{
  echo "<$tag>$item</$tag>";
}
echo '</tr>';
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.