GoFuckYourself.com - Adult Webmaster Forum

GoFuckYourself.com - Adult Webmaster Forum (https://gfy.com/index.php)
-   Fucking Around & Business Discussion (https://gfy.com/forumdisplay.php?f=26)
-   -   PHP: Displaying random lines from text file (https://gfy.com/showthread.php?t=877468)

eMonk 12-23-2008 06:16 PM

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?

psili 12-23-2008 06:18 PM

<?php
echo array_rand(file('../path/to/text.txt'));
?>

shit.
i suck.
thus a bump for you.

NickSunshine 12-23-2008 06:20 PM

PHP Code:

<?
$a=file('filename.txt');
$b=array_rand($a);
print($a[$b]);
?>


eMonk 12-23-2008 06:25 PM

Quote:

Originally Posted by NickSunshine (Post 15238925)
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?

NickSunshine 12-23-2008 06:35 PM

Quote:

Originally Posted by eMonk (Post 15238946)
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.

eMonk 12-23-2008 06:42 PM

after adding print_r($a); it displays the thumb link but the error still remains... any ideas?

NickSunshine 12-23-2008 06:50 PM

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.

psili 12-23-2008 06:54 PM

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.

eMonk 12-23-2008 07:10 PM

Quote:

Originally Posted by psili (Post 15239073)
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?

fris 12-23-2008 07:20 PM

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;

?>


NickSunshine 12-23-2008 07:40 PM

Quote:

Originally Posted by eMonk (Post 15239119)
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>");
}

?>


eMonk 12-23-2008 07:40 PM

Quote:

Originally Posted by fris (Post 15239147)
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 12-23-2008 07:45 PM

Quote:

Originally Posted by NickSunshine (Post 15239190)
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?

psili 12-23-2008 07:45 PM

Quote:

Originally Posted by eMonk (Post 15239194)
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);

NickSunshine 12-23-2008 07:54 PM

Quote:

Originally Posted by eMonk (Post 15239207)
what should the txt file look like?

samantha thumb.jpg text here?

samantha
jenny
debbie
susan
christy
...
...
julia

eMonk 12-23-2008 07:54 PM

Quote:

Originally Posted by psili (Post 15239211)
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! :thumbsup

I just hope this phrases fast when 50 or so entries are added to the txt file...

NickSunshine 12-23-2008 07:56 PM

correction: use shuffle($a); not array_shuffle($a);

i invented a function i guess :D

psili 12-23-2008 08:07 PM

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.

eMonk 12-23-2008 08:25 PM

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.

NickSunshine 12-23-2008 08:27 PM

Quote:

Originally Posted by eMonk (Post 15239318)
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. :1orglaugh

Bro Media - BANNED FOR LIFE 12-23-2008 08:28 PM

Easy peasy.

Voodoo 12-23-2008 08:29 PM

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!

eMonk 12-23-2008 08:33 PM

Quote:

Originally Posted by Voodoo (Post 15239336)
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.. :1orglaugh

psili 12-23-2008 08:51 PM

Quote:

Originally Posted by eMonk (Post 15239318)
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. :-)

NickSunshine 12-23-2008 09:14 PM

Quote:

Originally Posted by psili (Post 15239265)
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 :1orglaugh

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.

psili 12-23-2008 09:30 PM

Quote:

Originally Posted by NickSunshine (Post 15239450)
monkey work :1orglaugh

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?

Bro Media - BANNED FOR LIFE 12-23-2008 09:32 PM

Quote:

Originally Posted by psili (Post 15239522)
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];
?>


eMonk 12-23-2008 10:00 PM

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! :thumbsup

pornbling 12-24-2008 02:43 AM

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

quantum-x 12-24-2008 03:01 AM

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.


All times are GMT -7. The time now is 05:56 AM.

Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc