I want to create a related post section

Hi, I want to create a related post section on my dynamically generate page.

I’m trying to use Rand() to display just random records but it only displays “Resource id #4”.

<?php
mysql_select_db($database_XXX, $XXX);
$result= mysql_query("SELECT COUNT(*) FROM news");
$total = mysql_result($result, 0, 0);

// create a random number
mt_srand((double)microtime()*1000000);
$number = mt_rand()%$total;

// get a random entry
$result= mysql_query("SELECT * FROM news LIMIT $number, 5");
?>

This is what I’m using to generate the random post but nothing is pulling from the table.

I want it to show records that match a certain category. They all have an ID that I use name post_id = primary key, but I want it to check the article it’s on by where it was pulled from since it was dynamically generated. Then scan that record for what category it came from then use that category to pull other random articles from that category and then have it display as a small picture, and use title for link. sorry if this isn’t specific enough. if you need any more information or need illustrations i can show what i want. thanks.

mysql_query returns a result resource. That’s what is supposed to be in $result. You have to then fetch the row from the resource.

$row = mysql_fetch_array($result);
echo $row['post_title']; //or whatever the column is actually named

Thank you Dan, that worked perfectly. I have another question. What can I do to make it check for current title and not display the current page as a related topic. if at all possible can I sepearate them by certain categories as well. Thank you again. I appreciate the help.

[FONT=verdana]Jukrzy,

I’ll leave it to Dan (and others) to deal with the technical aspects of your question. I’d just add this: Why does the related article have to be picked at random? Why not let the article author or editor (or yourself) pick the related article as part of the editorial process, and store a reference to it against the original article?

That would do away with any complicated logic in picking a random article. More importantly, you will be sure that the two articles really are related, as they will be hand-picked.

This is what I do on my own sites. (In fact, I have up to four related articles for each original article.) But, of course, every site is different, so what works for me might not work for you.

Mike
[/FONT]

I thought it would be too time consuming hand picking each article for each post I have up. I thought having it choose at random would be best. Random from the category the post was from would be great but Idont know how to set that up.

[FONT=verdana]

Well, when you think of the work you have to do to write the article in the first place, check the spelling and grammar, apply the formatting, and publish on your site, the extra effort needed to choose a related article must surely be negligible.

Still, it’s up to you, of course.

Mike
[/FONT]

you are right. How will I be able to do that when my content is dynamically generated from my database? Will I have to create a new slot in my table and pull from there to generate the related link? If I can’t do that I wouldn’t mind having a certain category selected. I still don’t have quite enough content on my website as of yet but once I have enough things will come together a little better. As of right now this is what I got.

I’m repeating the dynamic pull from table to 3 spots. they all display something different. when i click on the link at the bottom it changes to the correct link but the related articles aren’t changed. I would to get an opinion but I don’t want to have my website critiqued until it’s finished or at least good enough to show people.

<?php
mysql_select_db($database_XXX, $XXX);
$result= mysql_query(“SELECT COUNT(*) FROM news”);
$total = mysql_result($result, 0, 0);

// create a random number
mt_srand((double)microtime()*1000000);
$number = mt_rand()%$total;

// get a random entry
$result= mysql_query(sprintf(“SELECT * FROM news WHERE post_id <> %d LIMIT %d, 3”, $post->post_id, $number));
$row = mysql_fetch_array($result);
?>

[FONT=verdana]

That’s essentially correct. You need an additional column in your Articles table. The column would hold the ID (or other unique key) of the related article. It’s that simple. (Of course, your user interface will also need a way for you to insert the ID.)

One other argument against showing a random article: If a regular visitor comes back to read the same article again, they would expect to see the same related article each time. They might have made a mental note, on the first visit, to follow the link to the related article, and might not be pleased if they can no longer see it.

Mike[/FONT]