Reputation: 138
What I want to do is display three random images from a database.
I want to use 'SELECT * FROM banner_images ORDER BY RAND() LIMIT 3'
, and at the end of the script, have three vars with the path (DB column) of the random images.
My Database Structure:
id name path
1 Banner-101 Banner-101.png
2 Banner-102 Banner-102.png
3 Banner-103 Banner-103.png
4 Banner-104 Banner-104.png
5 Banner-105 Banner-105.png
So for example after the script runs these are the vars
$path1 = 'Banner-103.png';
$path2 = 'Banner-105.png';
$path2 = 'Banner-101.png';
or something like that.
Does anyone know how I can do this?
Upvotes: 0
Views: 175
Reputation: 8280
Loop the rows and add to an array:
$array = array();
$i = 0;
//do your query
//fetch assoc rows
//loop them
while($row){
$array[$i] = $row['path']; //change banner to field name
$i++;
}
If you need those vairable names just assign them after:
$path1 = $array[0];
$path2 = $array[1];
$path2 = $array[2];
Now you can just echo those 3 variable names like you wanted.
Upvotes: 2