Hi, I currently have a form to upload an image to a directory and another page that randomly displays the pictures. However, right now the PHP code I have displays them randomy from the directory. I want it to randomly receive it from one of the SQL fields that are in one of the tables. When someone uploads an image i currently save the link of where the image is to a table. My current PHP code for the random image is:
<?php
function random_image($dir,$w,$h) {
if(file_exists($dir) AND !ereg("..",$dir) AND !ereg(".ht",$dir)) {
$d = dir("$dir");
$i = 0;
while (false !== ($entry = $d->read())) {
if(eregi(".jpg|.jpeg|.gif|.png",$entry)) {
$array[$i] = $entry;
$i++;
}
}
$d->close();
$i = $i -1;
$i = rand(0,$i);
if($array[$i] != "") {
echo "<img src=\"$dir$array[$i]\"";
if(isset($w) AND is_numeric($w)) {
echo " width=\"$w\"";
}
if(isset($h) AND is_numeric($h)) {
echo " height=\"$h\"";
}
echo ">";
} else {
echo "Error: Failed to select a random image (does the dir contain any .jpg/.png/gif's?).";
}
} else {
echo "Error: Directory $dir doesn't seem to exist.";
}
}
?>
How can I modify this code to access a database and receive the link from one of the fields so that I may next implement a rating system. Any help is greatly appreciated as I am still new to the language. Thank you.