Zend - The PHP Company




Files and Directories

Add Code


Pick up a random item from a file  

Type: code fragment
Added by: jay_chack
Entered: 18/09/2000
Last modified: 01/12/2000
Rating: - (fewer than 3 votes)
Views: 5912
Without loading the memory pick up a random item from a text file to display a random item e.g. Quotable quotes, a URL, address of a graphics file or even a code segment etc.


<?php

/*

Images.txt is a text file which can have any number of text lines like Quotable quotes, a URL, address of a  graphics file or even a code segment etc.to be randomly selected. Of course if the file is not in the same directory, correct path to be provided (sorry for the obvious sermon :) ).

You can edit Images.txt by adding deleting items as you wish. No more hard coding those damned Javascript arrays (YUCK!. It doesnt matter if there are 10 or 100 items - the code doesnt change as it dynamically selects the random item out of any number........wow

The $buffer at the end will have the magic random stuff that you can shove in any thing you want. YIKES!

I have just given an example and will come up with more in near future.

I have used some DHTML stuff in the example just for the heck of it. Its not a must. Thats why the need for 4.x browsers. But I guess all of you guys would have it.

*/
 

$fp fopen ("images.txt""r");
    if (!
$fp) {
        echo 
"file cant be opened";
        exit();
    }
    
$n 0;
    while (!
feof ($fp)) {
        
$buffer fgets ($fp4096);
        ++
$n;
    }
    --
$n;
    
srand ((double) microtime() * 1000000);
    
$randno rand(1$n);
    
rewind ($fp);
    
$n 0;
    while (
$n $randno) {
        
$buffer fgets ($fp4096);
        ++
$n;
    }
    
$buffer trim($buffer);
    
fclose ($fp);

/* Code fragment ends here */

?>


Usage Example


THE FOLLOWING IS AN EXAMPLE FOR USING THE ABOVE CODE. BUT YOU CAN USE AS A FUNCTION OR AN INCLUDE FILE FOR USING ITS POWER FOR OTHER PURPOSES TOO

<!doctype html public "-//w3c//dtd html 3.2//en">

<html>

<head>
<title>(Type a title for your page here)</title>
<style type="text/css">
<!--
#link3 {position:absolute; left: 150; top:100; width: 350px; height: 60px;  clip: rect (0, 350, 60, 0); layer-background-color: "#ffffff"; background-color: "#ffffff"; visibility: "hide";}
-->
</style>

<?php
$fp 
fopen ("images.txt""r");
    if (!
$fp) {
        echo 
"file cant be opened";
        exit();
    }
    
$n 0;
    while (!
feof ($fp)) {
        
$buffer fgets ($fp4096);
        ++
$n;
    }
    --
$n;
    
srand ((double) microtime() * 1000000);
    
$randno rand(1$n);
    
rewind ($fp);
    
$n 0;
    while (
$n $randno) {
        
$buffer fgets ($fp4096);
        ++
$n;
    }
    
$buffer trim($buffer);
    
fclose ($fp);

echo 
"<SCRIPT LANGUAGE="JavaScript">n";
echo 
"text1 = '$buffer'n";

?>

IE4 = (document.all) ? 1:0
NN4 = (document.layers) ? 1:0
ver4 = (IE4 || NN4) ? 1:0

function init() {
if(NN4) {
block3 = document.link3
}
if(IE4) {
block3 = link3.style
}
writediv('link3',null,text1)
showdiv(block3)
}

function showdiv(divname) {
if (NN4) divname.visibility="show"
else if(IE4) divname.visibility="visible"
}

function writediv(id, nestref, text) {
if(NN4) {
var lyr = (nestref)? eval('document. '+nestref+' .document. '+id+' .document.') : document.layers[id].document
lyr.open()
lyr.write(text)
lyr.close()
}
else if(IE4) document.all(id).innerHTML=text
}

</SCRIPT>

</head>

<body ONLOAD="init()">

<DIV ID="link3">
&nbsp
</DIV>

</body>

</html>


Rate This Script





Search



This Category All Categories