I want to select all entries in a MySQL table, and randomly display a number of them.
Here is my code:
include 'connectdb.php';
$query = "SELECT * FROM places WHERE box = 1";
$result = mysql_query($query);
$total = mysql_affected_rows();
$total = $total - 1;
$wanted = 2; // the number of entries i want to display
if( $wanted > $total ) {
die("Not enough places yet!");
}
$toshow = array();
for( $i = 0; $i < $wanted ; $i++ ) {
$temp = rand( 0 , $total );
while( in_array( $temp , $toshow ) ) {
$temp = rand( 0 , $total );
}
$toshow[$i] = $temp;
}
foreach( $toshow as $add ){
$row = // function needed here //
$name = $row['name'];
$telephone = $row['telno'];
//get rest of row and include a view to show the entry
}
I also tried using:
mysql_result( $result , $add );
but that does not seem to help.
The MySQL connection works, the for loop returns an array of numbers that exist only once so that I don’t show the same post all the time, and I’ve added the die because if we need more posts than we have number, it ges stuck in an infinite loop.
Any ideas on the function I need / suggestions to solve the problem? / things I’ve done completely wrong?
Just select with a random order and you already have what you ask for: