RJP1 Posted July 14, 2010 Share Posted July 14, 2010 Hi guys, This is hard to explain... I'll try. I have a MySQL table for articles, each article is in a section (id). I can do a while loop to create a HTML table of title, section etc. How do I split the code so that I create a new HTML table per $row['sectionid']? Any ideas would be great! I can group by section and then title in the table, but i'd rather not have the section id/or name repeated in a column for every article in a particular section if you get me...? Simplified code below. Cheers, RJP1 public function listArticle() { require_once('../includes/DbConnector.php'); $connector = new DbConnector(); $result = $connector->query('SELECT * FROM article ORDER BY section, title'); echo '<h2>Articles:</h2>'; if ( $result !== false && mysql_num_rows($result) > 0 ) { echo '<table id="table"> <thead> <tr> <th scope="col">Title</th> <th scope="col">Section</th> <th scope="col" style="text-align:center;">Edit?</th> <th scope="col" style="text-align:center;">Delete?</th> </tr> </thead> <tbody>'; while ($row = $connector->fetchArray($result)){ echo '<tr><td>'.$row['title'].'</td>'; echo '<td>'.$row['section'].'</td>'; } } echo '</tbody> </table>'; return; } Link to comment https://forums.phpfreaks.com/topic/207779-new-html-table-within-a-while-loop-foreach-rowsectionid/ Share on other sites More sharing options...
Psycho Posted July 15, 2010 Share Posted July 15, 2010 I would suggest using a single table with intermediary headers for each section. It will make this much easier. Or, put an empty row between sections. Here's a quick rewrite (not tested) public function listArticle() { require_once('../includes/DbConnector.php'); $connector = new DbConnector(); $result = $connector->query('SELECT * FROM article ORDER BY section, title'); echo '<h2>Articles:</h2>'; if ($result === false) { $output = "Unable to query results."; } else if(mysql_num_rows($result) < 1) { $output = "There were no results."; } else { $header = "<tr>\n"; $header .= "<th scope=\"col\">Section</th>\n"; $header .= "<th scope=\"col\" style=\"text-align:center;\">Edit?</th>\n"; $header .= "<th scope=\"col\" style=\"text-align:center;\">Delete?</th>\n"; $header .= "</tr>\n"; $output = "<table id=\"table\">\n"; $output .= "<tbody>\n"; $current_title = ''; while ($row = $connector->fetchArray($result)) { if($current_title != $row['title']) { $current_title = $row['title']; $output .= "<tr><td colspan=\"3\"> </td></tr>\n"; $output .= "<tr><td colspan=\"3\"><b>{$current_title}</b></td></tr>\n"; $output .= $header; } $output .= "<tr>\n"; $output .= "<td>{$row['section']}</td>\n"; $output .= "<td>[Edit Link]</td>\n"; $output .= "<td>[Delete Link]</td>\n"; $output .= "</tr>\n"; } $output .= "</tbody>\n"; $output .= "</table>\n"; } echo $output; return; } Link to comment https://forums.phpfreaks.com/topic/207779-new-html-table-within-a-while-loop-foreach-rowsectionid/#findComment-1086175 Share on other sites More sharing options...
joel24 Posted July 15, 2010 Share Posted July 15, 2010 do something like this, it will echo a new section name as a row/header if it differs from the last article. and it will echo a blank row to separate them. you can play around with the HTML to style it how you like. public function listArticle() { require_once('../includes/DbConnector.php'); $connector = new DbConnector(); $result = $connector->query('SELECT * FROM article ORDER BY section, title'); echo '<h2>Articles:</h2>'; if ( $result !== false && mysql_num_rows($result) > 0 ) { echo '<table id="table"> <thead> <tr> <th scope="col">Title</th> <th scope="col">Section</th> <th scope="col" style="text-align:center;">Edit?</th> <th scope="col" style="text-align:center;">Delete?</th> </tr> </thead> <tbody>'; $lastSection = ''; while ($row = $connector->fetchArray($result)){ if ($lastSection != $row['section']) { echo "<tr><td colspan='4'> </td></tr> <tr><td colspan ='4'>{$row['section']}</td></tr>"; } echo '<tr><td>'.$row['title'].'</td>'; echo '<td>'.$row['section'].'</td>'; } } echo '</tbody> </table>'; return; } Link to comment https://forums.phpfreaks.com/topic/207779-new-html-table-within-a-while-loop-foreach-rowsectionid/#findComment-1086221 Share on other sites More sharing options...
RJP1 Posted July 15, 2010 Author Share Posted July 15, 2010 Guys, this was great! mjdamato especially, I learnt a lot from your post, thanks dearly! Really appreciated! Now to edit the style... RJP1 Link to comment https://forums.phpfreaks.com/topic/207779-new-html-table-within-a-while-loop-foreach-rowsectionid/#findComment-1086400 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.