Forum Moderators: coopster

Message Too Old, No Replies

need help with random

pull data of a file and print it randomly

         

jj2000

6:37 am on Mar 7, 2007 (gmt 0)

10+ Year Member



I need help to make a part of a php code works,
your help is appreciated,

a script will be called, and then one line of data from a csv file will be printed ( echo command), the code works fine, but the thing is every time the code is called the first line of data is printed out, I want to change that to random, because the csv file contains lots of data (one on every line seperaged by %%)that has to be printed, the way it works now, it only prints same data each time,( first line of data).

I have tried the shuffle function as well, but i could not make it work, I just replace the this line ( $data = explode('%%', $data); )
with this one ($data = shuffle(explode('%%', $data));)
but stoped to print,
anyone willing to help?
Here is the code :

$count = $oldcount = $totalcount = $tally = 0;
$data = file_get_contents("data.csv");
$data = explode('%%', $data);
$di = 0;

$printme .= "\n" . $data[$di++];
if($di>=count($data))$di=0;
$tally = 0;
}
}
$totalcount += $count;
}
}
echo $printme;
}
?>

thanks.

jj2000

6:54 am on Mar 7, 2007 (gmt 0)

10+ Year Member



nevermind,
I got it working,

coopster

5:37 am on Mar 8, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Glad you got it sorted, jj2000.

shuffle() [php.net] returns a boolean value, not one of the "shuffled" index values.

bool shuffle ( array &$array )

I'm guessing you realized that and found that you needed to grab an index value after shuffling the array. Again, glad you got is sorted, I just wanted to post a possible answer here for future readers!