IPB

Welcome Guest ( Log In | Register )



Tags
This content has not been tagged yet
 
Reply to this topicStart new topic

Php - Fetching Random Line From A Text File

, and displaying it using AJAX/iframe


pasten
no avatar
Advanced Member
*******
Group: [HOSTED]
Posts: 119
Joined: 19-September 06
From: In front of my monitor.
Member No.: 30,215
myCENT:34.77



Post #1 post Nov 2 2008, 09:09 AM
What you should know: HTML, PHP basics (executing php scripts)
Examples:
Using AJAX
Using iframe
Download Examples

Using this script you can display a random quote/fact every time a visitor clicks the link. It happens dynamically without reloading the whole page. Useful for keeping a site fresh and lively. For example, you can have it in "About Me" page of a website which is rarely updated. All the quotes are stored in a text file separated by a line break. Although I have included a sample collection, you are free to modify or create your own. Thanks to David Pye for offering such a huge collection of facts. His site too contains a random line fetcher, but what I am going to tell is a bit different and will also contain a link to view another random quote without refreshing.

If you are interested in this script, I recommend you go through this tutorial instead of just downloading the script. Because, the files in the download was actually made for one of my friends static php website and hence contains a lot of bulky code and styling which might confuse you.

Using AJAX:

Ok, so first we will use AJAX and the php script which displays the output. Let's save the script as did-you-know.php:

CODE
<?php
srand ((double)microtime()*1000000);
$f_contents = file ("did-you-know.txt");
$line = $f_contents[array_rand ($f_contents)];
print $line;
?>


You can download "Attached File  did_you_know.txt ( 67.42K ) Number of downloads: 4
" and save it to the same directory as your php script. Bascially, the whole text file is saved into an array and then a random row is extracted using php's built in function array_rand(). array_rand() can also extract several lines by supplying an extra argument that specifies how many random elements you want and it returns the keys for them in an array.

Now for the main display file which shows the quotes. Save it as anything like random-quote.php:
HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<title>Random line fetcher - Using Ajax</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<script type="text/javascript">
//Create a boolean variable to check for a valid Internet Explorer instance.
var xmlhttp = false;

//Check if we are using IE.
try {

//If the Javascript version is greater than 5.
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {

//If not, then use the older activex object.
try {

//If we are using Internet Explorer.
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {

//Else we must be using a non-IE browser.
xmlhttp = false;
}
}

//If we are using a non-IE browser, create a javascript instance of the object.
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
xmlhttp = new XMLHttpRequest();
}
function makerequest(serverPage, objID) {
var obj = document.getElementById(objID);
xmlhttp.open("GET", serverPage);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
obj.innerHTML = xmlhttp.responseText;
}
}
xmlhttp.send(null);
}
</script>
</head>

<body onload="makerequest('did-you-know.php','fact')">
<div>
 <div id="fact"></div>
 <a href="did-you-know.php" onclick="makerequest('did-you-know.php','fact'); return false;">Yes! Next one please...</a>
</div>
</body>
</html>




The basic javascript defines an instance of the object depending on whether the browser is IE or not.
HTML
<body onload="makerequest('did-you-know.php','fact')">


The body onload displays random quote when the page is first viewed or when it is refreshed.
HTML
<a href="did-you-know.php" onclick="makerequest('did-you-know.php','fact'); return false;">Yes! Next one please...</a>


onclick invokes the function makerequest which calls the php script and puts the quote in the div with id "fact".

That\'s all. Now upload all the three files and view "ranom-quote.php".

I was unable to execute it correctly in IE. Any help, please? Here is the complete short code as given above: Attached File  barebones_using_ajax.zip ( 69.64K ) Number of downloads: 0
.

Using iframe:
In the mean time, I have created the script using iframe. Its very simple, any way here is the modified visual for it (random-quote.php). The php script (did-you-know.php) is the same as given above. Here we don't require javascript at all.
HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<title>Random line fetcher - Using iframe</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
</head>

<body>
<div>Did You Know?</div>
<div>
 <iframe src="did-you-know.php" id="fact" title="fact"></iframe>
 <a href="did-you-know.php" target="fact">Yes! Next one please...</a>  
</div>
</body>
</html>


Update: External download links not working. Download the attachments.

This post has been edited by pasten: Jun 19 2009, 04:06 PM
Attached File(s)
Attached File  did_you_know.zip ( 291.92K ) Number of downloads: 8
 
Go to the top of the page
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

    Topic Title Replies Topic Starter Views Last Action
No New Posts   8 rob86 191 18th October 2009 - 01:52 AM
Last post by: inverse_bloom
No New Posts   4 noxit 8,953 19th June 2004 - 05:45 PM
Last post by: OpaQue
No New Posts   3 Plenoptic 1,381 17th July 2006 - 12:06 PM
Last post by: Plenoptic
No New Posts   6 Dagoth Nereviar 3,426 11th March 2007 - 11:38 PM
Last post by: jlhaslip
No New Posts   8 -Pandemonium- 9,300 25th August 2004 - 04:00 PM
Last post by: -Pandemonium-
No New Posts 5 BoSZ 10,306 8th January 2009 - 07:35 PM
Last post by: Arthur Dent
No New Posts 6 dozen 4,653 9th September 2004 - 11:58 PM
Last post by: Triple X
No New Posts   4 annylei 4,651 14th September 2004 - 09:38 PM
Last post by: Triple X
No New Posts 10 jailbox 4,907 19th August 2009 - 07:17 PM
Last post by: iworld200
No New Posts   4 electriic ink 1,738 25th July 2006 - 07:28 PM
Last post by: electriic ink
No New Posts   7 -Cooper_NFFC- 5,090 11th October 2004 - 05:52 AM
Last post by: ashiezai
No New Posts   4 hansley 4,759 6th November 2004 - 11:03 PM
Last post by: ronin
No New Posts   3 josh_sg1 4,240 12th October 2004 - 12:02 AM
Last post by: s2city
No New Posts   8 sohahm 5,139 21st October 2004 - 08:44 PM
Last post by: beg4mercy
No New Posts   0 sohahm 3,558 16th October 2004 - 01:46 AM
Last post by: sohahm


 



RSS Open Discussion Time is now: 8th November 2009 - 09:44 AM

Web Hosting Powered by ComputingHost.com.