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?

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>';
}

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.