December 16, 201213 yr Hello, have been running a Facebook competition and was going to draw the names from a hat. Turns out a few more people than expected entered so I wish to just use a button on my web page to display 3 random users from my database. I have the fields of full name & email. Thanks, Craig
December 16, 201213 yr Fetch all the names inside one array using mySQL and PHP, shuffle them in a random order and display the first three. Léon
December 17, 201213 yr Fetch all the names inside one array using mySQL and PHP, shuffle them in a random order and display the first three. Léon This.
December 17, 201213 yr Author Thanks for the help Managed to get it to do roughly what i wanted. I could only get it to display 1 result though. How do i choose the amount it shows? Heres what i have: $result = mysql_query('SELECT * FROM email') or exit(mysql_error()); while ($row = mysql_fetch_assoc($result)) {$array[] = $row;} shuffle($array); print_r($array[3]); I thought the [3] would show 3 results? or does it show the 3rd result?
December 17, 201213 yr Thanks for the help Managed to get it to do roughly what i wanted. I could only get it to display 1 result though. How do i choose the amount it shows? Heres what i have: $result = mysql_query('SELECT * FROM email') or exit(mysql_error()); while ($row = mysql_fetch_assoc($result)) {$array[] = $row;} shuffle($array); print_r($array[3]); I thought the [3] would show 3 results? or does it show the 3rd result? It shows the 4rd result. Arrays are zero based. [0] -> First result [1] -> Second result [2] -> Third result Hope it helps, Léon Edited December 17, 201213 yr by Leonvv
December 17, 201213 yr Ah gotcha, so what about printing out 3 results? echo $array[0]; echo $array[1]; echo $array[2]; Or: print_r($array); to see the all the elements in the array. Léon
December 17, 201213 yr Author echo $array[0]; echo $array[1]; echo $array[2]; Or: print_r($array); to see the all the elements in the array. Léon When someone explains the answer it always seems to simple... yet until that point im puzzled! Thanks for all your help, much appreciated
Create an account or sign in to comment