crunchie Posted February 10, 2010 Share Posted February 10, 2010 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 https://forums.phpfreaks.com/topic/191705-need-help-getting-table-to-display-in-rows/ Share on other sites More sharing options...
Stuie_b Posted February 10, 2010 Share Posted February 10, 2010 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 https://forums.phpfreaks.com/topic/191705-need-help-getting-table-to-display-in-rows/#findComment-1010442 Share on other sites More sharing options...
crunchie Posted February 10, 2010 Author Share Posted February 10, 2010 Thank you so much Stuie you're a lifesaver. I knew it had to be something simple that I just wasn't seeing. crunchie Link to comment https://forums.phpfreaks.com/topic/191705-need-help-getting-table-to-display-in-rows/#findComment-1010447 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.