Greenguy's Board


Go Back   Greenguy's Board > Programming & Scripting
Register FAQ Calendar Today's Posts

Reply
 
Thread Tools Search this Thread Rate Thread Display Modes
Old 2021-08-24, 03:15 AM   #1
jollyhumper
That's what she said
 
jollyhumper's Avatar
 
Join Date: Feb 2012
Location: Norway
Posts: 2,559
Send a message via ICQ to jollyhumper
PHP pulling random line

Hi

Let's say a text document has 30 lines in it.
How could PHP be used to randomly pull one of those lines, each time the web page is built?
I'm looking to pull a random category or partner, or something, on my pages.

PHP is not my strongest side

Thanks anyhow
Jlly
jollyhumper is offline   Reply With Quote
Old 2021-08-24, 06:53 AM   #2
Greenguy
The Original Greenguy (Est'd 1996) & AVN HOF Member - I Crop Pics For Thumbs In My Sleep
 
Greenguy's Avatar
 
Join Date: Feb 2003
Location: Blasdell, NY (shithole suburb south of Buffalo)
Posts: 41,915
Send a message via ICQ to Greenguy
I actually know this one!!! (not that I wrote it lol)

Code:
<?php
$file = "/home/domain.com/public_html/textfile.txt";
$banners = file($file);
srand((float) microtime() * 1235689);
$selected = array_rand($banners);
$SelBanner = trim($banners[$selected]);
if ($SelBanner=="") {
$SelBanner = trim($banners[0]);
};
echo $SelBanner;
?>
That will pull 1 line from the text file & insert it on the page. I've used it as a cheap banner rotator for years:
https://www.link-o-rama.com/greenguy/mlinks.htm
__________________

Promote POV Porn Cash By Building & Submitting Galleries to the Porn Luv Network
Greenguy is offline   Reply With Quote
Old 2021-08-24, 06:53 AM   #3
Greenguy
The Original Greenguy (Est'd 1996) & AVN HOF Member - I Crop Pics For Thumbs In My Sleep
 
Greenguy's Avatar
 
Join Date: Feb 2003
Location: Blasdell, NY (shithole suburb south of Buffalo)
Posts: 41,915
Send a message via ICQ to Greenguy
...and yes I know there's useless code in there
__________________

Promote POV Porn Cash By Building & Submitting Galleries to the Porn Luv Network
Greenguy is offline   Reply With Quote
Old 2021-08-24, 10:46 AM   #4
jollyhumper
That's what she said
 
jollyhumper's Avatar
 
Join Date: Feb 2012
Location: Norway
Posts: 2,559
Send a message via ICQ to jollyhumper
Thanks!

I will try it as soon as I can. Hopefully this week.



Jlly
jollyhumper is offline   Reply With Quote
Old 2021-11-24, 11:55 AM   #5
jollyhumper
That's what she said
 
jollyhumper's Avatar
 
Join Date: Feb 2012
Location: Norway
Posts: 2,559
Send a message via ICQ to jollyhumper
Quote:
Originally Posted by Greenguy View Post
I actually know this one!!! (not that I wrote it lol)
It took a while before I tested it, but FYI, it works :-)
(Of course you knew it worked, I mean, it works for me too :-D )

Thanks again, appreciated

Jlly

Last edited by jollyhumper; 2021-11-24 at 12:35 PM.. Reason: added [/quote]
jollyhumper is offline   Reply With Quote
Old 2021-11-25, 09:42 AM   #6
Greenguy
The Original Greenguy (Est'd 1996) & AVN HOF Member - I Crop Pics For Thumbs In My Sleep
 
Greenguy's Avatar
 
Join Date: Feb 2003
Location: Blasdell, NY (shithole suburb south of Buffalo)
Posts: 41,915
Send a message via ICQ to Greenguy
__________________

Promote POV Porn Cash By Building & Submitting Galleries to the Porn Luv Network
Greenguy is offline   Reply With Quote
Old 2022-01-16, 10:27 AM   #7
balas
Just because I don't care doesn't mean I don't understand!
 
Join Date: Jan 2004
Location: Poland
Posts: 91
<?php
$f_contents = file("random_source.txt");
$line = $f_contents[rand(0, count($f_contents) - 1)];
?>
__________________
have phun
balas is offline   Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 06:53 PM.


Mark Read
Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
© Greenguy Marketing Inc