apotterdd Posted July 17, 2008 Share Posted July 17, 2008 I've been at this for a couple of days now but can't seem to get my code to do what I want it to do. $sql = "SELECT category, name, url FROM links ORDER BY category, name"; if ($result = mysqli_query($cxn,$sql)) while($row = mysqli_fetch_assoc($result)) { echo "<table cellpadding='5' cellspacing='0' border='1' bordercolor='#5F2A4C' align='center' width='75%'> <tr><th>{$row['category']}</th></tr>" . "<tr><td><a href='{$row['url']}' target='_blank'>" . "{$row['name']}</a></td></tr></table>"; } It shows all the links just fine but I want it to keep from repeating the category name over and over. The output I get is something like this. Category site url Category site url Another Category site url Another Category site url It'll repeat that until it's run through all of the data in that particular table. I want the category the links are in to only show up once and all of the links in that category to show up under it. Does that make any sense? I've tried nesting if statements and it gave me the same output as described above. Any help is appreciated, Anita Link to comment https://forums.phpfreaks.com/topic/115182-solved-showing-links-from-the-database-one-column-keeps-repeating/ Share on other sites More sharing options...
c_shelswell Posted July 17, 2008 Share Posted July 17, 2008 If I think i've understood this correctly could you not make your query 'GROUP by category' instead? You could then output all your data. Cheers Link to comment https://forums.phpfreaks.com/topic/115182-solved-showing-links-from-the-database-one-column-keeps-repeating/#findComment-592272 Share on other sites More sharing options...
apotterdd Posted July 17, 2008 Author Share Posted July 17, 2008 I've done the GROUP BY thing and it works but not as I expected. It will only return one link in each category and not all of them. I'm thinking it's got to be something with the table code but not sure where it's stopping at. Is there something I'm missing in that code to return all rows for each category? Link to comment https://forums.phpfreaks.com/topic/115182-solved-showing-links-from-the-database-one-column-keeps-repeating/#findComment-592283 Share on other sites More sharing options...
mmarif4u Posted July 17, 2008 Share Posted July 17, 2008 U want to show it like this: Category category-2 site url site url-2 site url site url-2 Link to comment https://forums.phpfreaks.com/topic/115182-solved-showing-links-from-the-database-one-column-keeps-repeating/#findComment-592286 Share on other sites More sharing options...
apotterdd Posted July 17, 2008 Author Share Posted July 17, 2008 Yeah I would love to have links side by side instead of showing up one link per line. I did find a tutorial on dividing rows into columns it last night but it confused me a lot. I really appreciate the help I'm getting Link to comment https://forums.phpfreaks.com/topic/115182-solved-showing-links-from-the-database-one-column-keeps-repeating/#findComment-592289 Share on other sites More sharing options...
mmarif4u Posted July 17, 2008 Share Posted July 17, 2008 If i understand you correctly,try like this: 1st echo your table outside while loop. An example: <table border='0'> <tr> <td>Category</td> <td>URL</td> </tr> while($row=mysql_fetch_array($q)){ echo "<tr>"; echo "<td>$row['category']</td>"; echo "<td>$row['url']</td>"; echo "</tr>"; } </table> Link to comment https://forums.phpfreaks.com/topic/115182-solved-showing-links-from-the-database-one-column-keeps-repeating/#findComment-592293 Share on other sites More sharing options...
apotterdd Posted July 17, 2008 Author Share Posted July 17, 2008 It's still not returning all of the links under the specified category. I wanted to avoid making separate pages for three little sections and there isn't going to be that many links either. I'll stare at it again tomorrow with fresh eyes. Link to comment https://forums.phpfreaks.com/topic/115182-solved-showing-links-from-the-database-one-column-keeps-repeating/#findComment-592307 Share on other sites More sharing options...
apotterdd Posted July 18, 2008 Author Share Posted July 18, 2008 I figured out a few hours ago that I needed more columns in my database as it kept returning all of the links instead of the ones I've specified. I'm also looking to use the SELECT COUNT(columnname) so I can divide them evenly. The two tutorials I did look at were not explanitory enough. Just enough to know that I don't know enough. Any definitive tutorials on this that is worded really well so that this relative noob can understand it? Link to comment https://forums.phpfreaks.com/topic/115182-solved-showing-links-from-the-database-one-column-keeps-repeating/#findComment-593149 Share on other sites More sharing options...
apotterdd Posted July 19, 2008 Author Share Posted July 19, 2008 Turns out I was overthinking it. SELECT DISTINCT is my friend Link to comment https://forums.phpfreaks.com/topic/115182-solved-showing-links-from-the-database-one-column-keeps-repeating/#findComment-593998 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.