eMonk Posted December 24, 2008 Share Posted December 24, 2008 I want to display thumb links randomly from a text file in php. here's a sample link: <a href="../names/samantha/" class="tip thumb"><img src="../names/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? I tried the following but get the error, "Warning: array_rand() [function.array-rand]: First argument has to be an array". <?php $a=file('filename.txt'); $b=array_rand($a); print($a[$b]); ?> Link to comment https://forums.phpfreaks.com/topic/138262-solved-displaying-random-lines-from-a-text-file/ Share on other sites More sharing options...
Jabop Posted December 24, 2008 Share Posted December 24, 2008 <?php $image_list = file_get_contents('file.txt'); $image_list = explode("\n", $image_list); echo $image_list[mt_rand(0, count($image_list))]; ?> Link to comment https://forums.phpfreaks.com/topic/138262-solved-displaying-random-lines-from-a-text-file/#findComment-722882 Share on other sites More sharing options...
eMonk Posted December 24, 2008 Author Share Posted December 24, 2008 <?php $image_list = file_get_contents('file.txt'); $image_list = explode("\n", $image_list); echo array_rand($image_list); ?> Warning: file_get_contents(txt) [function.file-get-contents]: failed to open stream: No such file or directory. ...but the file is there... any ideas? Link to comment https://forums.phpfreaks.com/topic/138262-solved-displaying-random-lines-from-a-text-file/#findComment-722886 Share on other sites More sharing options...
Jabop Posted December 24, 2008 Share Posted December 24, 2008 Warning: file_get_contents(txt) [function.file-get-contents]: failed to open stream: No such file or directory. ...but the file is there... any ideas? Check my code again, I edited it. Make sure you're passing the right file. Link to comment https://forums.phpfreaks.com/topic/138262-solved-displaying-random-lines-from-a-text-file/#findComment-722888 Share on other sites More sharing options...
eMonk Posted December 24, 2008 Author Share Posted December 24, 2008 no errors now but it's only showing 1/3 lines in the text file. i have repeated the a href link in first post in the txt file 3 times. the display output should show 3 thumb links... Link to comment https://forums.phpfreaks.com/topic/138262-solved-displaying-random-lines-from-a-text-file/#findComment-722891 Share on other sites More sharing options...
Jabop Posted December 24, 2008 Share Posted December 24, 2008 That wouldn't work for what you need - hold on. Link to comment https://forums.phpfreaks.com/topic/138262-solved-displaying-random-lines-from-a-text-file/#findComment-722893 Share on other sites More sharing options...
JasonLewis Posted December 24, 2008 Share Posted December 24, 2008 Try this... $contents = file("file.txt"); echo $contents[rand(0,count($contents))]; Link to comment https://forums.phpfreaks.com/topic/138262-solved-displaying-random-lines-from-a-text-file/#findComment-722895 Share on other sites More sharing options...
eMonk Posted December 24, 2008 Author Share Posted December 24, 2008 thanks.. i tried my initial php code and used... a b c d e f g in the txt file and it worked. but again it only displayed one letter at a time. i want them all displayed side-by-side randomly. Link to comment https://forums.phpfreaks.com/topic/138262-solved-displaying-random-lines-from-a-text-file/#findComment-722896 Share on other sites More sharing options...
eMonk Posted December 24, 2008 Author Share Posted December 24, 2008 Try this... $contents = file("file.txt"); echo $contents[rand(0,count($contents))]; displays one letter at a time. how can we display them ALL side-by-side randomly? Link to comment https://forums.phpfreaks.com/topic/138262-solved-displaying-random-lines-from-a-text-file/#findComment-722899 Share on other sites More sharing options...
eMonk Posted December 24, 2008 Author Share Posted December 24, 2008 one more thing i noticed... sometimes it doesn't show any letters at all... i don't have any blank lines in the txt file either... Link to comment https://forums.phpfreaks.com/topic/138262-solved-displaying-random-lines-from-a-text-file/#findComment-722901 Share on other sites More sharing options...
Jabop Posted December 24, 2008 Share Posted December 24, 2008 eMonk <?php $array=shuffle($array); foreach($array as $key=>$value) { echo $value; } ?> Link to comment https://forums.phpfreaks.com/topic/138262-solved-displaying-random-lines-from-a-text-file/#findComment-722902 Share on other sites More sharing options...
eMonk Posted December 24, 2008 Author Share Posted December 24, 2008 shouldn't file.txt be included in that code? Link to comment https://forums.phpfreaks.com/topic/138262-solved-displaying-random-lines-from-a-text-file/#findComment-722905 Share on other sites More sharing options...
eMonk Posted December 24, 2008 Author Share Posted December 24, 2008 found an easier way... $deck = file('test.txt'); shuffle($deck); echo implode(' ',$deck); Link to comment https://forums.phpfreaks.com/topic/138262-solved-displaying-random-lines-from-a-text-file/#findComment-722922 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.