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.

Link to comment
Share on other sites

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

 

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.