thecase Posted July 15, 2011 Share Posted July 15, 2011 Hi, I'm trying to populate an HTML table horizontally, so each new record on the table is inserted in the next cell until it reaches the fouth one, then an new row is added and the cycle repeats hopefully the code I have started will explain better echo " <table width='70%' cellspacing='0' cellpadding='0' border='1' align='center'> <tr> <td align='center' width='18%'><b><p>Date / Time</p></b></td> <td align='center' width='25%'><b><p>Presenter 1</p></b></td> <td align='center' width='25%'><b><p>Presenter 2</p></b></td> <td align='center' width='25%'><b><p>Engineer</p></b></td> </tr> "; $colors = array("#df8700", "#006700", "#cf0000"); $count = 0; while ($row = mysql_fetch_assoc($query)) { $bg = ($bg == '#FFD5A8' ? '#BBBBBB' : '#FFD5A8'); if ($count == 0){ echo '<tr>'; } echo "<td bgcolor=\"$bg\" align=\"center\" width='25%' ><p class='table'>{$row['date']}</p></td>"; if ($row['userid'] == '31'){ echo "<td bgcolor=\"$bg\" align=\"center\" width='25%' ><font color=\"{$colors['1']}\"><p class='table'>None</p> </font></td>"; } else { echo " <td bgcolor=\"$bg\" align=\"center\" width='25%' ><font color=\"{$colors[$row['status']]}\"><p class='table'>{$row['user']}</p> </font></td>"; } if ($count + 1 == 4){ echo "</tr> "; }else{ ++$count; } } echo "</table><BR>"; unset($colors, $row); } Although this does not work. It adds extra cells and doesn't complete all before starting the next, can anyone see the problem. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/242065-html-table-populated-from-mysql-horizontally/ Share on other sites More sharing options...
AyKay47 Posted July 15, 2011 Share Posted July 15, 2011 the code that you posted shows me a table with one row and 4 columns, plain and simple.. are you trying to add to that table and it's not working? Or is the code that you posted displayed funky results? Quote Link to comment https://forums.phpfreaks.com/topic/242065-html-table-populated-from-mysql-horizontally/#findComment-1243104 Share on other sites More sharing options...
thecase Posted July 15, 2011 Author Share Posted July 15, 2011 The code is throwing weird results, see image attached. [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/242065-html-table-populated-from-mysql-horizontally/#findComment-1243107 Share on other sites More sharing options...
AyKay47 Posted July 15, 2011 Share Posted July 15, 2011 didn't scroll down the entire code... normally the best/easiest way to spot these type of errors is to look at a "view source" of the code to see what it is actually doing Quote Link to comment https://forums.phpfreaks.com/topic/242065-html-table-populated-from-mysql-horizontally/#findComment-1243110 Share on other sites More sharing options...
Adam Posted July 15, 2011 Share Posted July 15, 2011 Looks like you're only adding the "</tr>" after 4 rows from the DB table? Shouldn't you start a new HTML table row for each DB row? Quote Link to comment https://forums.phpfreaks.com/topic/242065-html-table-populated-from-mysql-horizontally/#findComment-1243111 Share on other sites More sharing options...
thecase Posted July 15, 2011 Author Share Posted July 15, 2011 Looks like you're only adding the "</tr>" after 4 rows from the DB table? Shouldn't you start a new HTML table row for each DB row? I thought the if ($count == 0){ echo '<tr>'; } Will do the trick Quote Link to comment https://forums.phpfreaks.com/topic/242065-html-table-populated-from-mysql-horizontally/#findComment-1243115 Share on other sites More sharing options...
Adam Posted July 15, 2011 Share Posted July 15, 2011 Allow me to re-phrase.. You're only adding the "<tr>" on the first row, and "</tr>" on the fourth row. What you're effectively doing is creating a new row in the HTML table after the fourth row in the database.. Which is why you see the broken structure shown in that image. Quote Link to comment https://forums.phpfreaks.com/topic/242065-html-table-populated-from-mysql-horizontally/#findComment-1243117 Share on other sites More sharing options...
thecase Posted July 15, 2011 Author Share Posted July 15, 2011 I think I get it now, so I set the $count back to 0 when the $count reaches 4. It looks fine in the view source but now the date has got stuck in the loop and inserts itself between every cell instead of just the first cell on each row. Version 2 echo " <table width='70%' cellspacing='0' cellpadding='0' border='1' align='center'> <tr> <td align='center' width='25%'><b><p>Date</p></b></td> <td align='center' width='25%'><b><p>Presenter 1</p></b></td> <td align='center' width='25%'><b><p>Presenter 2</p></b></td> <td align='center' width='25%'><b><p>Engineer</p></b></td> </tr> "; $colors = array("#df8700", "#006700", "#cf0000"); $count = 0; while ($row = mysql_fetch_assoc($query)) { $bg = ($bg == '#FFD5A8' ? '#BBBBBB' : '#FFD5A8'); if ($count == 0){ echo '<tr>'; } echo "<td bgcolor=\"$bg\" align=\"center\" width='25%' ><p class='table'>{$row['date']}</p> </td>"; if ($row['userid'] == '31'){ echo "<td bgcolor=\"$bg\" align=\"center\" width='25%' ><font color=\"{$colors['1']}\"><p class='table'>None</p> </font></td>"; } else { echo " <td bgcolor=\"$bg\" align=\"center\" width='25%' ><font color=\"{$colors[$row['status']]}\"><p class='table'>{$row['user']}</p> </font></td>"; } if ($count + 1 == 4){ $count = '0'; echo "</tr> "; }else{ ++$count; } } echo "</table><BR>"; Thanks [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/242065-html-table-populated-from-mysql-horizontally/#findComment-1243142 Share on other sites More sharing options...
thecase Posted July 16, 2011 Author Share Posted July 16, 2011 I have fixed this but I have added another cell to display the comments of each record from the table. The problem is it only displays the every third record in the database three times, how do I get it to get the correct comment. Hope this makes sence. echo " <table width='70%' cellspacing='0' cellpadding='0' border='1' align='center'> <tr> <td align='center' width='25%'><b><p>Date</p></b></td> <td align='center' width='25%'><b><p>Presenter 1</p></b></td> <td align='center' width='25%'><b><p>Presenter 2</p></b></td> <td align='center' width='25%'><b><p>Engineer</p></b></td> </tr> "; $colors = array("#df8700", "#006700", "#cf0000"); $count = 0; while ($row = mysql_fetch_assoc($query)) { if ($count == 0) { echo '<tr>'; $bg = ($bg == '#FFD5A8' ? '#BBBBBB' : '#FFD5A8'); echo "<td bgcolor=\"$bg\" align=\"center\" width='25%' ><p class='table'>{$row['show']}</p> </td>"; } if ($row['userid'] == '31') { echo "<td bgcolor=\"$bg\" align=\"center\" width='25%' ><font color=\"{$colors['1']}\"><p class='table'>None</p> </font></td>"; } else { echo " <td bgcolor=\"$bg\" align=\"center\" width='25%' ><font color=\"{$colors[$row['status']]}\"><p class='table'>{$row['user']} </font></p> </td>"; } if ($count + 1 == 3) { echo "</tr> <tr> <td></td> <td bgcolor=\"$bg\" align=\"center\" width='25%'><p class='table'>{$row['comment']}</p></td> <td bgcolor=\"$bg\" align=\"center\" width='25%'><p class='table'>{$row['comment']}</p></td> <td bgcolor=\"$bg\" align=\"center\" width='25%'><p class='table'>{$row['comment']}</p></td> </tr> "; $count = '0'; } else { ++$count; } } echo "</table><BR>"; Quote Link to comment https://forums.phpfreaks.com/topic/242065-html-table-populated-from-mysql-horizontally/#findComment-1243516 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.