Sing along if you know it!
We are going to assume you have a table named 'images' and has a field named 'filename' which holds the filename of the image.
We build our sql statement. You can change up the where clause to match your needs.
We create a new array named 'filenames' to store all the filenames from the database. This will keep us from needing to make multiple calls to our database, saving on server resources.
We loop through our sql result and pull each returned filename and push it into our 'filenames' array. If you have filenames 'dog.jpg', 'cat.jpg', 'bird.jpg' in your table, you will end up with an array like
$filenames = array ('dog.jpg', 'cat.jpg', 'bird.jpg');
But I digress.
We print out our <script> tag. Notice the '\n' new line at the end of the string. This will print a newline in our source code which will be great for debugging our source. Remember that you can add escape characters to format your html source code. If you didn't add these characters you would get one giant string of php generated source code.
We run a loop that is the length of the size of our filenames. We create a new Image() object for each image in our database. If you have 3 files in your database you'll get the result
image0 = new Image(); image1 = new Image(); image2 = new Image();
We put out another newline character. Again, we loop through the filename array and touch each element in the array. We then print out the source location for each image. Your result would be
image0.src = 'images/dog.jpg'; image1.src = 'images/cat.jpg'; image2.src = 'images/bird.jpg';