easyedy Posted January 3, 2008 Share Posted January 3, 2008 Hi I'm trying to rearrange my code so I can display my array in 2 columns with each column being 1 cell from each row being a cell like I got now. Any suggestions? Code I have now: define ("NUMCOLS",2); $count = 0; $counter= 1; echo "<table border='0' width='100%'>"; foreach ($result[$Array] as $row) { if ($count % NUMCOLS == 0) echo "<tr>\n"; # new row echo '<td width="50%"><a href="$row['ID'].'">'.$row['Name'].'</a></td>'; $count++; $counter++; if ($count % NUMCOLS == 0) echo "</tr>\n"; # end row } # end row if not already ended if ($count % NUMCOLS != 0) { foreach ($result[$FA][$SA] as $row) echo "<td> </td>"; echo "</tr>\n"; } echo "</table>"; It displays now as: <td>1</td><td>2</td> <td>3</td><td>4</td> when I need it to be: <td>1<br>2</td><td>3<br>4</td> I just can't get my code to work. thx. Quote Link to comment https://forums.phpfreaks.com/topic/84230-displaying-array-in-2-columns/ Share on other sites More sharing options...
teng84 Posted January 3, 2008 Share Posted January 3, 2008 can you show us the content of your array so we know how to loop it properly Quote Link to comment https://forums.phpfreaks.com/topic/84230-displaying-array-in-2-columns/#findComment-428940 Share on other sites More sharing options...
easyedy Posted January 3, 2008 Author Share Posted January 3, 2008 $numbers = array(1,2,3,4,5,6,7,8,9,10,11,12) Quote Link to comment https://forums.phpfreaks.com/topic/84230-displaying-array-in-2-columns/#findComment-428946 Share on other sites More sharing options...
teng84 Posted January 3, 2008 Share Posted January 3, 2008 i guess your sample out put is wrong can you post here your actual desired output not having htm tags Quote Link to comment https://forums.phpfreaks.com/topic/84230-displaying-array-in-2-columns/#findComment-428948 Share on other sites More sharing options...
teng84 Posted January 3, 2008 Share Posted January 3, 2008 <? define ("NUMCOLS",2); $count = 0; $counter= 1; echo "<table border='0' width='100%'>"; foreach ($result[$Array] as $row) { if ($count % NUMCOLS == 0){ echo "<tr>\n"; # new row echo '<td width="50%"><a href="'.$row['ID'].'">'.$row['Name'].'</a></td>'; $count++; $counter++; continue; } echo '<td width="50%"><a href="'.$row['ID'].'"><br />'.$row['Name'].'</a></td>'; $count++; $counter++; if ($count % NUMCOLS == 0) echo "</tr>\n"; # end row } # end row if not already ended if ($count % NUMCOLS != 0) { foreach ($result[$FA][$SA] as $row) echo "<td> </td>"; echo "</tr>\n"; } echo "</table>"; ok may this ... Quote Link to comment https://forums.phpfreaks.com/topic/84230-displaying-array-in-2-columns/#findComment-428952 Share on other sites More sharing options...
easyedy Posted January 3, 2008 Author Share Posted January 3, 2008 <? define ("NUMCOLS",2); $count = 0; $counter= 1; echo "<table border='0' width='100%'>"; foreach ($result[$Array] as $row) { if ($count % NUMCOLS == 0){ echo "<tr>\n"; # new row echo '<td width="50%"><a href="'.$row['ID'].'">'.$row['Name'].'</a></td>'; $count++; $counter++; continue; } echo '<td width="50%"><a href="'.$row['ID'].'"><br />'.$row['Name'].'</a></td>'; $count++; $counter++; if ($count % NUMCOLS == 0) echo "</tr>\n"; # end row } # end row if not already ended if ($count % NUMCOLS != 0) { foreach ($result[$FA][$SA] as $row) echo "<td> </td>"; echo "</tr>\n"; } echo "</table>"; ok may this ... No makes new table rows for each $row... I want 1 table row and 2 columns. ie: 1/2 of the array is displayed in the first <td></td> and the other half in the other <td></td> Quote Link to comment https://forums.phpfreaks.com/topic/84230-displaying-array-in-2-columns/#findComment-429019 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.