Jump to content

PHP Related Articles


computermax2328

Recommended Posts

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;
                ?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :D

 

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,

Link to comment
Share on other sites

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 :D

 

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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)";
     

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.