Nevertheless, it has been decided to keep this tutorial available until such time as it has been either updated or re-written. In the meantime, if you wish to take the tutorial, then it is recommended that you change the mysql-extension code to use mysqli or PDO. (If you do this successfully then perhaps you might consider submitting a revised tutorial, contributing to dream.in.code and earning some Kudos!)
I figured I'd take a break from my marathon coding session and write a quick tutorial. Being behind schedule sucks.
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';
This post has been edited by andrewsw: 30 January 2015 - 12:25 PM