Janik Posted January 7, 2009 Share Posted January 7, 2009 hello everyone here is some code i got to display random items from a txt file. [pre]<?php // load the file that contain the ads $adfile = "ads.txt"; $ads = array(); // one line per ad $fh = fopen($adfile, "r"); while(!feof($fh)) { $line = fgets($fh, 10240); $line = trim($line); if($line != "") { $ads[] = $line; } } // randomly pick an ad $num = count($ads); $idx = rand(0, $num-1); echo $ads[$idx]; ?>[/pre] Could someone tell me how i could make it show more than 1 item at a time. Like 10 links from the ads.txt file would be great. Thanks you all in advance Link to comment https://forums.phpfreaks.com/topic/139917-solved-need-some-help-to-make-work-right/ Share on other sites More sharing options...
dennismonsewicz Posted January 7, 2009 Share Posted January 7, 2009 try this change: $ads[] = $line; to: $ads[] .= $line; Link to comment https://forums.phpfreaks.com/topic/139917-solved-need-some-help-to-make-work-right/#findComment-732010 Share on other sites More sharing options...
Brian W Posted January 7, 2009 Share Posted January 7, 2009 put code into code tags please when the code is more than one line (or even then if you'd like) [ code ]code goes here[ /code ] remove the spaces in between [] on the tags: code goes here <?php // load the file that contain the ads $adfile = "ads.txt"; $ads = array(); // one line per ad $fh = fopen($adfile, "r"); while(!feof($fh)) { $line = fgets($fh, 10240); $line = trim($line); if($line != "") { $ads[] = $line; } } // randomly pick an ad $i=0; while($i < 10){ $num = count($ads); $idx = rand(0, $num-1); echo $ads[$idx]; $i++; } ?> Try that, should do it 10 times. Link to comment https://forums.phpfreaks.com/topic/139917-solved-need-some-help-to-make-work-right/#findComment-732015 Share on other sites More sharing options...
premiso Posted January 7, 2009 Share Posted January 7, 2009 try this change: $ads[] = $line; to: $ads[] .= $line; mmm not really... <?php // load the file that contain the ads $adfile = "ads.txt"; $ads = array(); // one line per ad $fh = fopen($adfile, "r"); while(!feof($fh)) { $line = fgets($fh, 10240); $line = trim($line); if($line != "") { $ads[] = $line; } } // randomly pick an ad $num = count($ads); $idx = array(); while (count($idx) != 10) { $rand = rand(0, $num-1); if (!in_array($rand, $idx)) $idx[] = $rand; } foreach ($idx as $id) echo $ads[$id] . "<br />"; ?> Something like that would work, this assumes you have more than 10 items in that list, and with the random, the scripts execute time will vary. EDIT: The difference between this one and Brians, is that the one I posted should guarantee 10 unique ads, Brian's may not. But yea, whichever one better suits your needs. Link to comment https://forums.phpfreaks.com/topic/139917-solved-need-some-help-to-make-work-right/#findComment-732019 Share on other sites More sharing options...
dennismonsewicz Posted January 7, 2009 Share Posted January 7, 2009 sorry, still learning somethings Link to comment https://forums.phpfreaks.com/topic/139917-solved-need-some-help-to-make-work-right/#findComment-732022 Share on other sites More sharing options...
Janik Posted January 7, 2009 Author Share Posted January 7, 2009 Thank you all very much for so fast help! I try it now and let you know, thank you. Edit : premiso version works great, thank you all again for fast and great help! Link to comment https://forums.phpfreaks.com/topic/139917-solved-need-some-help-to-make-work-right/#findComment-732027 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.