computermax2328 Posted August 27, 2012 Share Posted August 27, 2012 Hello Again, I am building a blog from scratch and below the articles on the article pages I have a related articles section for obvious reasons. The way I built is to search the articles database for every article with a similar tag to the article above. The obvious problem here is that the article that is on the page already will be selected with this query. How can I go about selecting every article except for the one above? My code is below. Thanks in advance! <div id="relatedhead"> <h3 class="related">Related Articles</h3> </div> <?php $rquery = "SELECT * FROM blog WHERE Tags LIKE '%" . $tags . "%' OR '%" . $tags . "%' OR '%" . $tags . "%' OR '%" . $title . "%' OR '%" . $sub . "%'"; $rselect = mysql_query($rquery); if (!rselect) { echo "Query Not Working" . mysql_error(); } while ($rrow = mysql_fetch_array($rselect)): $rid = $rrow['id']; $rtitle = $rrow['Title']; $rsub = $rrow['Subtitle']; $rdesc = $rrow['Description']; $rauth = $rrow['Author']; $rdate = $rrow['Date']; $rtags = $rrow['Tags']; ?> <div id="related"> <div id="rtitle"> <h1 class="related"><? echo $rtitle;?></h1> </div> <div id="rsub"> <h2 class="related"><? echo $rsub;?></h2> <p3>Posted By: <?php echo $rauth;?></p3> </div> <div id="rtime"> <p2>Posted: <?php echo $rdate;?></p2></br> <p3 class="tags">Tags: <?php echo $rtags;?></p3> </div> <div id="rdesc"> <p><?php echo $rdesc;?></p> <a class="toarticle" href="article.php?id=<?php echo $rid;?>&<?php echo $rtitle;?>"><p2>Read More-></p2></a> </div> <img class="photoright" src="#"/> </div> <?php endwhile; ?> Quote Link to comment Share on other sites More sharing options...
tibberous Posted August 27, 2012 Share Posted August 27, 2012 I don't think you are even coding PHP... I *think* that *might* be Wordpress's templating system? I mean, "while() :", "endwhile;", doesn't look like any PHP I've ever seen. Quote Link to comment Share on other sites More sharing options...
Christian F. Posted August 27, 2012 Share Posted August 27, 2012 tibberous: It is PHP, and it's even explained in the manual. It's an alternative syntax, though used very seldom. computermax: You already know the ID of the article, so you can easily a constraint stating that you want the articles that has a different ID. Also, you really should move the tags into a table of their own, and then use a many-to-many relationship to associate them with articles. Will make things a lot easier for you in the long run. Quote Link to comment Share on other sites More sharing options...
computermax2328 Posted August 27, 2012 Author Share Posted August 27, 2012 tibberous: I know I am only a noob at this PHP thing and I probably shouldn't be telling anyone how to code, but you should try the while(): endwhile; sometime. I really only use it when I need the while loop to output another language, usually HTML. It is just ALOT easier than echoing everything and adding all of the single and double quotes and escaping everything. Give it a try! You might like it ChristianF: Thank you for your suggestion. So, I googled what you said and from what I got I should be using MYSQL CHECK(). Is that correct? Will CHECK() automatically just not select that id variable? And one more thing, could you explain the separate table for tags? I am actually having trouble with my tag system. I am just using single phrases right now (ex. php story), no multiple tags separated with commas. I was using MYSQL LIKE to select them. I dont know if that is the right way to go. Again, I am new at this. Thanks again, Quote Link to comment Share on other sites More sharing options...
trq Posted August 27, 2012 Share Posted August 27, 2012 I don't think you are even coding PHP... Hmmm... foot in mouth? Quote Link to comment Share on other sites More sharing options...
computermax2328 Posted August 27, 2012 Author Share Posted August 27, 2012 I don't think you are even coding PHP... My coding is just so advance it's like I am coding in Ancient Aztec language or something. Hahaha! Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 27, 2012 Share Posted August 27, 2012 tibberous: I know I am only a noob at this PHP thing and I probably shouldn't be telling anyone how to code, but you should try the while(): endwhile; sometime. I really only use it when I need the while loop to output another language, usually HTML. It is just ALOT easier than echoing everything and adding all of the single and double quotes and escaping everything. Give it a try! You might like it I just wanted to point out that it doesn't matter if you use while() endwhile; or if you use while(){}, you can always exit PHP and do HTML rather than echo/escape. <?php $x=1; while($x<10){ ?> <p>A bunch of HTML here</p> <?php } ?> So yes, while() endwhile; and the other alternative syntaxes are certainly valid and useful, I don't think that you *need* to use it the way you think. I'm only mentioning this because your comment implies that you think you can't do this with the more common syntax, and you can indeed. Quote Link to comment Share on other sites More sharing options...
Christian F. Posted August 27, 2012 Share Posted August 27, 2012 No, you should not be using CHECK(). You already have all of the basic elements in your query, with the WHERE clause adding several constraints on what you want. All you need to do is add one more step to it, namely constraining it to ID's that are not equal to the current article. If you search for "many-to-many relations mysql" you'll find a whole lot of articles that will explain this in great detail. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted August 27, 2012 Share Posted August 27, 2012 Also, the alternate syntax takes more typing. The closing keyword, in all cases, is longer then the two character {} syntax. Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 27, 2012 Share Posted August 27, 2012 Also to answer the original question, add to your query " AND id != $id". You'll probably need to add parens. Also you had $tags in there three times? I also changed the syntax of the string concatenation. $rquery = "SELECT * FROM blog WHERE (Tags LIKE '%$tags%' OR Tags LIKE '%$title%' OR Tags LIKE '%$sub%') AND (id != $id)"; Quote Link to comment Share on other sites More sharing options...
computermax2328 Posted August 27, 2012 Author Share Posted August 27, 2012 Yeah, I just came back to say that I threw WHERE id!=$id into the MYSQL and that worked. Thanks for all of the suggestions! Can someone elaborate on a tagging system. Right now in my database I just have a column with each article that is a single word or phrase (ex. article about PHP). The way I had it and want it is single words sperated by columns (ex. php, mysql, coding languages). I was having problems selecting it. I was using LIKE, but I was not getting anything back. The only way I can think of is to make an entire database just with tags and associate each row with an article. Then have each column be a tag, so there would be a maximum of like 5 or 10 tags for each article. Is that the right idea? Quote Link to comment Share on other sites More sharing options...
Christian F. Posted August 27, 2012 Share Posted August 27, 2012 If you mean "table", and not "database", then you're almost on the right track. Had you done as I suggested, and searched for "m2m relationtions" you'd known exactly what you needed to do. 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.