Welcome to the GoFuckYourself.com - Adult Webmaster Forum forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Post New Thread Reply

Register GFY Rules Calendar
Go Back   GoFuckYourself.com - Adult Webmaster Forum > >
Discuss what's fucking going on, and which programs are best and worst. One-time "program" announcements from "established" webmasters are allowed.

 
Thread Tools
Old 12-23-2008, 06:16 PM   #1
eMonk
Confirmed User
 
Industry Role:
Join Date: Aug 2003
Location: Canada
Posts: 2,310
PHP: Displaying random lines from text file

I want to display thumb links randomly from a text file in php. here's a sample link:

Code:
<a href="../models/samantha/" class="tip thumb"><img src="../models/samantha/thumb.jpg" width="88" height="88"><span>Samantha</span></a>
text file will have a max of 50 lines of so. this is a temp solution until i start playing with mysql. right now im looking for a pure php solution. how can this be done?
eMonk is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 12-23-2008, 06:18 PM   #2
psili
Confirmed User
 
Join Date: Apr 2003
Location: Loveland, CO
Posts: 5,526
<?php
echo array_rand(file('../path/to/text.txt'));
?>

shit.
i suck.
thus a bump for you.
__________________
Your post count means nothing.
psili is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 12-23-2008, 06:20 PM   #3
NickSunshine
Confirmed User
 
NickSunshine's Avatar
 
Join Date: Mar 2006
Posts: 1,196
PHP Code:
<?
$a=file('filename.txt');
$b=array_rand($a);
print($a[$b]);
?>
__________________
NickSunshine is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 12-23-2008, 06:25 PM   #4
eMonk
Confirmed User
 
Industry Role:
Join Date: Aug 2003
Location: Canada
Posts: 2,310
Quote:
Originally Posted by NickSunshine View Post
PHP Code:
<?
$a=file('filename.txt');
$b=array_rand($a);
print($a[$b]);
?>

Error: Warning: array_rand() [function.array-rand]: First argument has to be an array.

Do I need anything special in my text file?
eMonk is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 12-23-2008, 06:35 PM   #5
NickSunshine
Confirmed User
 
NickSunshine's Avatar
 
Join Date: Mar 2006
Posts: 1,196
Quote:
Originally Posted by eMonk View Post
Error: Warning: array_rand() [function.array-rand]: First argument has to be an array.

Do I need anything special in my text file?
sounds like it isn't reading the text file at all.

is the path correct?

try adding a print_r($a); after the file function to see whats in there.
__________________
NickSunshine is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 12-23-2008, 06:42 PM   #6
eMonk
Confirmed User
 
Industry Role:
Join Date: Aug 2003
Location: Canada
Posts: 2,310
after adding print_r($a); it displays the thumb link but the error still remains... any ideas?
eMonk is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 12-23-2008, 06:50 PM   #7
NickSunshine
Confirmed User
 
NickSunshine's Avatar
 
Join Date: Mar 2006
Posts: 1,196
sorry to ask:

is there a $ next to the a in the array_rand() function?

$b=array_rand($a);

short of that, no- i have no idea.
__________________
NickSunshine is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 12-23-2008, 06:54 PM   #8
psili
Confirmed User
 
Join Date: Apr 2003
Location: Loveland, CO
Posts: 5,526
Are you editing your text file of random lines on a windows box and pushing it to a unix box or vice-versa? Maybe your text file's all torqued up. Try something simple like just a line of:

a
b
c
d
e
f

and see what happens.
__________________
Your post count means nothing.
psili is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 12-23-2008, 07:10 PM   #9
eMonk
Confirmed User
 
Industry Role:
Join Date: Aug 2003
Location: Canada
Posts: 2,310
Quote:
Originally Posted by psili View Post
Are you editing your text file of random lines on a windows box and pushing it to a unix box or vice-versa? Maybe your text file's all torqued up. Try something simple like just a line of:

a
b
c
d
e
f

and see what happens.

that worked but it's only displaying 1 letter at a time.. i want all displayed randomly side-by-side. why doesn't the txt like my a href links?
eMonk is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 12-23-2008, 07:20 PM   #10
fris
Too lazy to set a custom title
 
fris's Avatar
 
Industry Role:
Join Date: Aug 2002
Posts: 55,235
Code:
<?

// urls.txt should be format of <a href="domain.com"><img src="domain.jpg"></a>
// 1 per line

$file = "urls.txt";
$fp = file($file);
srand((double)microtime()*1000000);
$urls = $fp[array_rand($fp)];
echo $urls;

?>
__________________
Since 1999: 69 Adult Industry awards for Best Hosting Company and professional excellence.


WP Stuff
fris is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 12-23-2008, 07:40 PM   #11
NickSunshine
Confirmed User
 
NickSunshine's Avatar
 
Join Date: Mar 2006
Posts: 1,196
Quote:
Originally Posted by eMonk View Post
that worked but it's only displaying 1 letter at a time.. i want all displayed randomly side-by-side. why doesn't the txt like my a href links?
this is my fault for not completely reading your first post.

for some reason i thought you only wanted one line...

PHP Code:
<?

$a=file('textfile.txt');
array_shuffle($a);
for each($a as $line){
   print("<a href='../models/" . $line . "'><img src='../models/" . $line . "/thumb.jpg' />" . ucwords($line) . "</a>");
}

?>
__________________

Last edited by NickSunshine; 12-23-2008 at 07:41 PM.. Reason: <? ?>
NickSunshine is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 12-23-2008, 07:40 PM   #12
eMonk
Confirmed User
 
Industry Role:
Join Date: Aug 2003
Location: Canada
Posts: 2,310
Quote:
Originally Posted by fris View Post
Code:
<?

// urls.txt should be format of <a href="domain.com"><img src="domain.jpg"></a>
// 1 per line

$file = "urls.txt";
$fp = file($file);
srand((double)microtime()*1000000);
$urls = $fp[array_rand($fp)];
echo $urls;

?>

displays one thumb at a time. how can we display them ALL side-by-side randomly?
eMonk is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 12-23-2008, 07:45 PM   #13
eMonk
Confirmed User
 
Industry Role:
Join Date: Aug 2003
Location: Canada
Posts: 2,310
Quote:
Originally Posted by NickSunshine View Post
this is my fault for not completely reading your first post.

for some reason i thought you only wanted one line...

PHP Code:
<?

$a=file('textfile.txt');
array_shuffle($a);
for each($a as $line){
   print("<a href='../models/" . $line . "'><img src='../models/" . $line . "/thumb.jpg' />" . ucwords($line) . "</a>");
}

?>

what should the txt file look like?

samantha thumb.jpg text here?
eMonk is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 12-23-2008, 07:45 PM   #14
psili
Confirmed User
 
Join Date: Apr 2003
Location: Loveland, CO
Posts: 5,526
Quote:
Originally Posted by eMonk View Post
displays one thumb at a time. how can we display them ALL side-by-side randomly?
Not sure about why the HREF's aren't display but try:

$deck = file('test.txt');
shuffle($deck);
echo implode(' ',$deck);
__________________
Your post count means nothing.
psili is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 12-23-2008, 07:54 PM   #15
NickSunshine
Confirmed User
 
NickSunshine's Avatar
 
Join Date: Mar 2006
Posts: 1,196
Quote:
Originally Posted by eMonk View Post
what should the txt file look like?

samantha thumb.jpg text here?
samantha
jenny
debbie
susan
christy
...
...
julia
__________________
NickSunshine is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 12-23-2008, 07:54 PM   #16
eMonk
Confirmed User
 
Industry Role:
Join Date: Aug 2003
Location: Canada
Posts: 2,310
Quote:
Originally Posted by psili View Post
Not sure about why the HREF's aren't display but try:

$deck = file('test.txt');
shuffle($deck);
echo implode(' ',$deck);

That worked... thanks!... nice & short too...

Thanks to everyone else that helped aswell! Much appreciated!

I just hope this phrases fast when 50 or so entries are added to the txt file...
eMonk is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 12-23-2008, 07:56 PM   #17
NickSunshine
Confirmed User
 
NickSunshine's Avatar
 
Join Date: Mar 2006
Posts: 1,196
correction: use shuffle($a); not array_shuffle($a);

i invented a function i guess :D
__________________
NickSunshine is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 12-23-2008, 08:07 PM   #18
psili
Confirmed User
 
Join Date: Apr 2003
Location: Loveland, CO
Posts: 5,526
This eMonk thread should be pinned somewhere.

It's a very simple question that had a pretty simple solution. However, the communication between ALL involved just wasn't "right" which is the reason it took a while to solve.

There's a lesson here kids, and it's not about how to properly sniff your own ass crack, you crazy monkeys, you.
__________________
Your post count means nothing.
psili is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 12-23-2008, 08:25 PM   #19
eMonk
Confirmed User
 
Industry Role:
Join Date: Aug 2003
Location: Canada
Posts: 2,310
thanks again... i'm still new to php... reading my way through, "PHP and MySQL Web Development 4th edition" which is a GREAT book btw, and I knew the method wouldn't be too much lines of code for this task. I also believe it's bad practice to use PHP to display HTML.
eMonk is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 12-23-2008, 08:27 PM   #20
NickSunshine
Confirmed User
 
NickSunshine's Avatar
 
Join Date: Mar 2006
Posts: 1,196
Quote:
Originally Posted by eMonk View Post
I also believe it's bad practice to use PHP to display HTML.
in my opinion, it really depends on the situation.

sometimes you just gotta loop.
__________________
NickSunshine is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 12-23-2008, 08:28 PM   #21
Bro Media - BANNED FOR LIFE
MOBILE PORN: IMOBILEPORN
 
Join Date: Jan 2004
Location: Tinseltown NL
Posts: 16,502
Easy peasy.
Bro Media - BANNED FOR LIFE is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 12-23-2008, 08:29 PM   #22
Voodoo
♥ ♦ ♣ ♠
 
Voodoo's Avatar
 
Industry Role:
Join Date: Sep 2002
Location: Porn Valley, CA
Posts: 10,590
Code:
<?php include("myrandomtextfile.txt"); ?>
Then once a day or once a minute (Whatever you want), open myrandomtextfile.txt and replace the text in it randomly!
__________________

"I'm selflessly supporting the common good, but only coincidentally looking out for No.1."
Voodoo is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 12-23-2008, 08:33 PM   #23
eMonk
Confirmed User
 
Industry Role:
Join Date: Aug 2003
Location: Canada
Posts: 2,310
Quote:
Originally Posted by Voodoo View Post
Code:
<?php include("myrandomtextfile.txt"); ?>
Then once a day or once a minute (Whatever you want), open myrandomtextfile.txt and replace the text in it randomly!

lol voodoo..
eMonk is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 12-23-2008, 08:51 PM   #24
psili
Confirmed User
 
Join Date: Apr 2003
Location: Loveland, CO
Posts: 5,526
Quote:
Originally Posted by eMonk View Post
thanks again... i'm still new to php... reading my way through, "PHP and MySQL Web Development 4th edition" which is a GREAT book btw, and I knew the method wouldn't be too much lines of code for this task. I also believe it's bad practice to use PHP to display HTML.
Dude, keep learning and asking questions. My last post was geared toward the train-wreck posts about how a client / designer / coder was fucked over because they just didn't communicate.

If you get comfortable with a skill, can communicate with a client on how to apply that skill, and can get both you and a client to agree and understand terms on the skill you apply please let me know. :-)
__________________
Your post count means nothing.
psili is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 12-23-2008, 09:14 PM   #25
NickSunshine
Confirmed User
 
NickSunshine's Avatar
 
Join Date: Mar 2006
Posts: 1,196
Quote:
Originally Posted by psili View Post
There's a lesson here kids, and it's not about how to properly sniff your own ass crack, you crazy monkeys, you.
monkey work

i learned a couple things...

a) read the first post COMPLETELY before you try to help.
b) array_shuffle() is not a function.
c) PHP and MySQL Web Development 4th edition is a GREAT book.
__________________
NickSunshine is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 12-23-2008, 09:30 PM   #26
psili
Confirmed User
 
Join Date: Apr 2003
Location: Loveland, CO
Posts: 5,526
Quote:
Originally Posted by NickSunshine View Post
monkey work

i learned a couple things...

a) read the first post COMPLETELY before you try to help.
b) array_shuffle() is not a function.
c) PHP and MySQL Web Development 4th edition is a GREAT book.
I re-read the original post and still would go for a solution of outputting a random one line of a text file. That's why this thread is so perfect in it's ability to illustrate how communication is so important; for all parties.

Regardless. I like my crab cioppino served with warm garlic bread.
Huh?
__________________
Your post count means nothing.
psili is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 12-23-2008, 09:32 PM   #27
Bro Media - BANNED FOR LIFE
MOBILE PORN: IMOBILEPORN
 
Join Date: Jan 2004
Location: Tinseltown NL
Posts: 16,502
Quote:
Originally Posted by psili View Post
I re-read the original post and still would go for a solution of outputting a random one line of a text file. That's why this thread is so perfect in it's ability to illustrate how communication is so important; for all parties.

Regardless. I like my crab cioppino served with warm garlic bread.
Huh?
PHP Code:
<?php
$lines 
file('yourfile.txt');
$num count($lines);
$r rand(1$num)-1;
echo 
$lines[$r];
?>
Bro Media - BANNED FOR LIFE is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 12-23-2008, 10:00 PM   #28
eMonk
Confirmed User
 
Industry Role:
Join Date: Aug 2003
Location: Canada
Posts: 2,310
Yeah, I could have been more clear in my original post. I never did mention to output ALL the links at once. Could have added more lines in the sample link aswell.

However this is great... GFY PHP Support Group!
eMonk is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 12-24-2008, 02:43 AM   #29
pornbling
Registered User
 
Join Date: Apr 2008
Location: underground
Posts: 251
try this:

$fp=file('yourfile.txt');
shuffle($fp);
echo $fp[0];

This should do it. If you want to see more
echo $fp[1]; echo $fp[2]; ...
pornbling is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 12-24-2008, 03:01 AM   #30
quantum-x
Confirmed User
 
quantum-x's Avatar
 
Join Date: Feb 2002
Location: ICQ: 251425 Fr/Au/Ca
Posts: 6,863
OK.
Seeing as noone else has solved this yet. (IE, haven't used the 'FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES' flags, meaning output would be fubar)
PHP Code:
<?
$f = file('/path/to/names/names.txt',FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES);
shuffle($f);
foreach($f as $k => $v) echo '<a href="../models/'.$v.'/" class="tip thumb"><img src="../models/'.$v.'/thumb.jpg" width="88" height="88"><span>'.strtoupper($v[0]).substr($v,1).'</span></a>'."\n";
?>
Renders:
Code:
<a href="../models/silvia/" class="tip thumb"><img src="../models/silvia/thumb.jpg" width="88" height="88"><span>Silvia</span></a>
<a href="../models/billy/" class="tip thumb"><img src="../models/billy/thumb.jpg" width="88" height="88"><span>Billy</span></a>
<a href="../models/jenna/" class="tip thumb"><img src="../models/jenna/thumb.jpg" width="88" height="88"><span>Jenna</span></a>
<a href="../models/adam/" class="tip thumb"><img src="../models/adam/thumb.jpg" width="88" height="88"><span>Adam</span></a>
With names capitalised as requested.
quantum-x is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Post New Thread Reply
Go Back   GoFuckYourself.com - Adult Webmaster Forum > >

Bookmarks



Advertising inquiries - marketing at gfy dot com

Contact Admin - Advertise - GFY Rules - Top

©2000-, AI Media Network Inc



Powered by vBulletin
Copyright © 2000- Jelsoft Enterprises Limited.