Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

How to Display a Random FAQ Record on Home Page?

Running PHPmyFaq



5:25 pm on Nov 12, 2008 (gmt 0)

10+ Year Member

On my website homepage, I have a simple PHP script , that picks a random Youtube clip from an array (Youtube IDs stored in a text file) and basically everytime a user loads the home page, he sees a different Youtube clip. It is a very cool script and people love it.

I would like to do the same thing, using the same script, to display a different random FAQ record (using PHOmyFaq), everytime a user loads our page. I think I can use the same script, and it should work fine.

My only problem is, I don't know how to select my FAQ records? Where would the script look for, to get all my FAQ records?

Anyway, here is the code for the Youtube script, and I would really appreciate if someone can help me figure out how to do it for PHPMyFaq.


// Build an array from the list of YouTube videos
// Replace YourVideoList.txt with the path to your text file
// This will likely be something like /home/accountname/public_html/foldername/etc
$video_array = file('YourVideoList.txt');

// Randomly pick one video from the array
$video = $video_array[rand(0, count($video_array) - 1)];
$video = trim($video);


<object width="425" height="350"><param name="movie" value="http://www.youtube.com/v/<?php echo $video;? >"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/<?php echo $video;?>" type="application/x-shockwave-flash" wmode="transparent" width="425" height="350"></embed></object>

Thanks a lot


8:38 pm on Nov 12, 2008 (gmt 0)

5+ Year Member

Have you tried posting in PHPMyFaq's forum?
It should be pretty similar to the youtube script. The only difference is that rather than accessing the text file, you'll be accessing the database instead. All you do then is to connect to the database, form a query to select a random faq, execute it, and retrieve the data. And this can be done in a short function, which you can call anywhere on the page.


function getRandomFaq(){
$result = mysql_query($sql);
$content = mysql_fetch_assoc($result);
return $content;


9:11 pm on Nov 12, 2008 (gmt 0)

10+ Year Member

Thanks d40, really appreciate it. That is a great step for me forward.

Just wondering, when you select from the table, are you selecting from a specific table? in other words, I have to know what the table is called, where the FAQ records are stored, and replace it in

As for PHPmyFaq forum, yes I already asked there, but no reply yet.


10:06 pm on Nov 12, 2008 (gmt 0)

5+ Year Member

Yes you would have to know the table name as well as the fields in it.Should be pretty simple, but if you do not have the database experience, we can go into further details.


2:15 am on Nov 13, 2008 (gmt 0)

10+ Year Member

Appreciate your help a lot.

I am just trying to determine what the table for PHPmyFaq is called, and what the field for the facts is called.

Problem is I can't open mysql to see these paramaters.


2:45 am on Nov 13, 2008 (gmt 0)

10+ Year Member

Ok I am making some progress. I found the following:

//phpmyfaq_ : I believe this is the database name/prefix?
//phpmyfaq_faqdata : this is the table where the records are stored
//id : this is how each fact is identified


6:20 pm on Nov 13, 2008 (gmt 0)

10+ Year Member

I am so happy, with your help, and others, I am finally able to display contents from myphpfaq onto my home page.

The only problem now is, the display, which I would like to tweak a bit to show the following:

-The record name: The field name in sql is 'thema'
-The first 10-15 words of the record, and then to read more, people have to click on a link. Field name in sql is 'content'

Like this for example:

Yahoo: An online search engine, as well as a portal, offering....(click to read more)

How can I accomplish the above please? I will be so happy if I can finally achieve this.

And here is the code I am using for this script

// Create the connection and select the DB
$link = mysql_connect("host","user","password");

if ($link) {

// Select records from the DB
$query = "SELECT content FROM pmf_faqdata ORDER BY Rand() LIMIT 1";
$result = mysql_query($query);

// Display records from the table
echo "<table border='1'>";
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
echo "<tr><td>$row[0]</td><td>$row[1]</td><td>$row[1]</td></tr>";
echo "</table>";
} else {
echo "Can't connect to the database!";

Thanks a lot guys,


6:55 pm on Nov 13, 2008 (gmt 0)

5+ Year Member

Congrats to you for getting this far. It is a good feeling to have accomplish such a feat isn't it?
For this task of shortening the link, the first thing I can think of is to use the substr() function, which basically cuts off a part of the whole string. Take a look at [us.php.net...]
on how to use it properly. This method only selects characters, not words. You may want to write a small algorithm to select words (with spaces are the delimiter). Consider also looking at the strrchr() function: [ca.php.net...]


1:12 am on Nov 14, 2008 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

Maybe not the most efficient way, but I would chop it back to a certain number of characters, and then use REGEX to chop off everything after the final space that it finds in the reduced size text block.

Oh, and one thing to help you debug your code; where you have the "can't connect to database" or other such error message, add a three digit number (that you randomly make up and type in) to that error message so that you can find where in the source code your failure occurred.


Featured Threads

Hot Threads This Week

Hot Threads This Month