homepage Welcome to WebmasterWorld Guest from 54.166.108.167
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
How to Display a Random FAQ Record on Home Page?
Running PHPmyFaq
asas111




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

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.

<?php

// 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

 

d40sithui




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

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.

sample:

<?
function getRandomFaq(){
$sql = "SELECT * FROM FAQ_TABLE ORDER BY RAND() LIMIT 1";
$result = mysql_query($sql);
$content = mysql_fetch_assoc($result);
return $content;
}
?>

asas111




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

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
"$sql = "SELECT * FROM FAQ_TABLE ORDER BY RAND() LIMIT 1"; correct?

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

d40sithui




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

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.

asas111




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

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.

asas111




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

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

asas111




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

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) {
mysql_selectdb("phpmyfaq_database",$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,

d40sithui




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

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...]

g1smd




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

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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved