I have a little script that reads in the contents of a directory, and I'm boning up on cookies, and what I'd like to do is modify this script so that it presents all the files in a directory (in this case image files) with a thumbnail version in a table-presented radio-button array (one image & button per cell), which saves the results to a cookie.
The effect intended here is that an image is chosen, submit is clicked and the result is used as a background image ("wallpaper") each time the site is loaded (ergo the cookie) until changed by a user.
The script is intended to be dynamic, in that it reads the contents of the directory each time it's loaded, rather than maintaining a fixed index file so that images may be added to or removed from the directory without requiring changes to the script or any secondary support files, but still allows different users to have different backgrounds (again, the task of the cookies).
The cookie is simple enough as it only needs to store the filename from the chosen index entry for the new wallpaper which can be retrieved by any page and set during pageload.
Based on my experience with looping in other languages, I'm assuming I need to start my
For example, this idea works OK (on paper at least) if there's only a handful images to choose from. But what if there were thousands or more? I'd end up with all the images loaded onto one huge form with outrageous pageload times, and that's just bad presentation. Can I paginate dynamically if the count reaches a preset number, say, 50? Or can I allow the user to choose the pagination limit within a given set of options, like 25,50,75,100 (and optionally store that in the cookie too)?
The effect intended here is that an image is chosen, submit is clicked and the result is used as a background image ("wallpaper") each time the site is loaded (ergo the cookie) until changed by a user.
The script is intended to be dynamic, in that it reads the contents of the directory each time it's loaded, rather than maintaining a fixed index file so that images may be added to or removed from the directory without requiring changes to the script or any secondary support files, but still allows different users to have different backgrounds (again, the task of the cookies).
Code:
<?php $path = "F:/development/apache2/sandbox-root/wallpapers/"; $narray=array(); $dir_handle = @opendir($path) or die("Unable to open $path."); echo "Please choose one of the following backgrounds to use:\n"; $i=0; while($file = readdir($dir_handle)) { if($file != '.' && $file != '..') { $narray[$i]=$file; echo "<a target='_blank' href='$path$narray[$i]'>$narray[$i]</a>"; $i++; } } //closing the directory closedir($dir_handle); ?>
Based on my experience with looping in other languages, I'm assuming I need to start my
<TABLE>
before the loop, and add my <TR>
, <TD>
, <IMG SRC>
and <INPUT>
tags inside the loop (likely with ECHO "";
or ECHO <<<
, and close the table and input after the loop ends. Because I'm still just a PHP Padawan, my concern is if there's a better way to do this as this seems a bit convoluted.For example, this idea works OK (on paper at least) if there's only a handful images to choose from. But what if there were thousands or more? I'd end up with all the images loaded onto one huge form with outrageous pageload times, and that's just bad presentation. Can I paginate dynamically if the count reaches a preset number, say, 50? Or can I allow the user to choose the pagination limit within a given set of options, like 25,50,75,100 (and optionally store that in the cookie too)?
Comment