![]() |
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> |
<?php
echo array_rand(file('../path/to/text.txt')); ?> shit. i suck. thus a bump for you. |
PHP Code:
|
Quote:
Error: Warning: array_rand() [function.array-rand]: First argument has to be an array. Do I need anything special in my text file? |
Quote:
is the path correct? try adding a print_r($a); after the file function to see whats in there. |
after adding print_r($a); it displays the thumb link but the error still remains... any ideas?
|
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. |
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. |
Quote:
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? |
Code:
<? |
Quote:
for some reason i thought you only wanted one line... PHP Code:
|
Quote:
displays one thumb at a time. how can we display them ALL side-by-side randomly? |
Quote:
what should the txt file look like? samantha thumb.jpg text here? |
Quote:
$deck = file('test.txt'); shuffle($deck); echo implode(' ',$deck); |
Quote:
jenny debbie susan christy ... ... julia |
Quote:
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... |
correction: use shuffle($a); not array_shuffle($a);
i invented a function i guess :D |
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. |
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.
|
Quote:
sometimes you just gotta loop. :1orglaugh |
Easy peasy.
|
Code:
<?php include("myrandomtextfile.txt"); ?> |
Quote:
lol voodoo.. :1orglaugh |
Quote:
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. :-) |
Quote:
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. |
Quote:
Regardless. I like my crab cioppino served with warm garlic bread. Huh? |
Quote:
PHP Code:
|
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 |
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]; ... |
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:
Code:
<a href="../models/silvia/" class="tip thumb"><img src="../models/silvia/thumb.jpg" width="88" height="88"><span>Silvia</span></a> |
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