NewcastleFan Posted April 17, 2013 Share Posted April 17, 2013 Hey everyone, I'm trying to select multiple rows from one table, depending on the ID given from another table. I've got it half working with the code below, however it echo's out each blog multiple times depending on how many different tags are assigned to it, how would I go about so it displays multiple tag's on one copy of the blog post? $sqlCommand = "SELECT blogid, blogtitle, content, blogtime, category, blogseourl, author FROM blog ORDER BY blogtime DESC"; $query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error()); $blogDisplay = ''; while ($row = mysqli_fetch_array($query)) { $blogid = $row["blogid"]; $blogtitle = $row["blogtitle"]; $content = $row["content"]; $blogtime = $row["blogtime"]; $category = $row["category"]; $blogseourl = $row["blogseourl"]; $author = $row["author"]; $contentshort = substr($content, 0, 250); $sqlCommand2 = "SELECT tag FROM blogtags WHERE blogid='$blogid'"; $query2 = mysqli_query($myConnection, $sqlCommand2) or die (mysqli_error()); while ($row = mysqli_fetch_array($query2)) { $tag = $row['tag']; $blogDisplay .= '<h1><a href="/blog/'. $blogseourl .'"> ' . $blogtitle . ' </a></h1> ' . $contentshort . '... <a href="/blog/'. $blogseourl .'">Read More...</a><br /><br /> ' . $author . ' posted on ' . $blogtime . ' | Category: ' . $category . ' | Tags: ' . $tag . ' | <a href="/blog/'. $blogseourl .'#disqus_thread"></a>'; } } mysqli_free_result($query); So everything is working correctly apart from it echoing multiple $blogDisplay's for each tag. Anyone got any ideas? Quote Link to comment Share on other sites More sharing options...
Jessica Posted April 17, 2013 Share Posted April 17, 2013 Ack! No queries in loops! Read this tutorial I wrote: http://thewebmason.com/tutorial-parent-child-lists/ You need to join your tables. For tags you can probably use group_concat or the method in my tutorial. Quote Link to comment 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.