Jump to content

need help getting table to display in rows


crunchie

Recommended Posts

I'm pretty much new at php so I'm wondering if anyone can explain why this won't display in rows instead of columns.

I have a txt file with this info in it

 

amy,anderson,aldergrove,archery,anchovies,110

bill,bonds,burnaby,bowling,beer,100

cameron,carson,cameroon,cars,candy,120

 

and this is my code:

 

<?php

$fileContentsArray = file_get_contents("./data.txt");

 

$lines = explode("\n", $fileContentsArray);

 

echo "<table border = '1'>";

 

$aArray = preg_split("/,/", $lines[0]);

                foreach ($aArray as $adata)

                                {

                                                echo "<tr><td>$adata</td></tr>";

                                }

$bArray = preg_split("/,/", $lines[1]);

                foreach ($bArray as $bdata)

                                {

                                                echo "<tr><td>$bdata</td></tr>";

                                }

$cArray = preg_split("/,/", $lines[2]);

                foreach ($cArray as $cdata)

                                {

                                                echo "<tr><td>$cdata</td></tr>";

                                }

echo "</table>";

?>

Any help would be appreciated.

thanks.

Because <tr> tells the table to create a new column not a new row, <td> is used for rows

 

You will need to create the row outside of the for each loop and place only the <td> inside the loop,

 

<?php
$fileContentsArray = file_get_contents("./data.txt");
$lines = explode("\n", $fileContentsArray);

echo "<table border = '1'>";

$aArray = preg_split("/,/", $lines[0]);
echo "<tr>";
                foreach ($aArray as $adata)
                                {
                                                echo "<td>$adata</td>";
                                }
echo "</tr>";
$bArray = preg_split("/,/", $lines[1]);
echo "<tr>";
                foreach ($bArray as $bdata)
                                {
                                                echo "<td>$bdata</td>";
                                }
							echo "</tr>";
$cArray = preg_split("/,/", $lines[2]);
echo "<tr>";
                foreach ($cArray as $cdata)
                                {
                                                echo "<td>$cdata</td>";
                                }
							echo "</tr>";
echo "</table>";

 

Stuie

 

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.