jasonc Posted April 18, 2006 Share Posted April 18, 2006 I have a large file with a list of songs, i wish to chose at random 5 of these to be shown on my web page.the problem i have is reading this file to get the info from it.i have the php script that randomise 5 numbers all i need is to know how to extract the lets saythe 5, 17, 26, 55, 105 and er 300 th lines from a text (tracks.txt) file.I known in the past that scripts can write a file but how can i read a line from one?also been told be a friend that when i do this i also need to be sure that the file can be read many time at the same time.I was going to use my database but decided against that as i have had problems in the past with them.also adding the data to it would take ages too.thank you in advance for your help. Link to comment https://forums.phpfreaks.com/topic/7780-read-random-line-from-a-text-file/ Share on other sites More sharing options...
ypirc Posted April 18, 2006 Share Posted April 18, 2006 This is what I came up with. There are a several solutions.[code]*** Came up with a different solution than before...<?$file = file('/etc/passwd');shuffle($file);for($i=0;$i<5;$i++) printf("%s\n", trim($file[$i]));?>[/code] Link to comment https://forums.phpfreaks.com/topic/7780-read-random-line-from-a-text-file/#findComment-28372 Share on other sites More sharing options...
Barand Posted April 18, 2006 Share Posted April 18, 2006 [!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]I was going to use my database ... adding the data to it would take ages too.[/quote]why, if you already have the data in a text file?Try[code]$songs = file('songlist.txt');$rand_titles = array_rand($songs, 5);foreach ($rand_titles as $k) { echo $songs[$k], '<br />';}[/code] Link to comment https://forums.phpfreaks.com/topic/7780-read-random-line-from-a-text-file/#findComment-28376 Share on other sites More sharing options...
jasonc Posted April 19, 2006 Author Share Posted April 19, 2006 thank you very much for your help, this worked a treat. i now have a new command to play around with and get to know.J[!--quoteo(post=366218:date=Apr 18 2006, 11:22 PM:name=Barand)--][div class=\'quotetop\']QUOTE(Barand @ Apr 18 2006, 11:22 PM) [snapback]366218[/snapback][/div][div class=\'quotemain\'][!--quotec--]why, if you already have the data in a text file?Try[code]$songs = file('songlist.txt');$rand_titles = array_rand($songs, 5);foreach ($rand_titles as $k) { echo $songs[$k], '<br />';}[/code][/quote] Link to comment https://forums.phpfreaks.com/topic/7780-read-random-line-from-a-text-file/#findComment-28512 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.