Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Aug 2011
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Question Need help outputting image array via radio buttons

    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).

    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);
    ?>
    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 <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)?
    Last edited by EvilSupahFly; 08-21-2011 at 06:11 AM. Reason: Clarity

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    This is non-recursive right?
    If so, I'd just use a glob. Being that you are not using a database for this, pagination will still require the entire directory stored in order to know where to start from. Since these are resources, we cannot tell them where to start, and this is why glob is easiest. You can still use a scan directory technique if you want, but you must create the array.
    PHP Code:
    <?php
    $iPerPage 
    25// Can be collected from a client, but must be validated it it comes from a user
    $iPage = isset($_GET['page']) ? (int)$_GET['page'] : 0;
    $sPath '/path/to/images/*.jpg'// I assumed a jpeg image.  You can change or use braces to add more types {jpg,png}
    // Refer to the glob page for how to add that: http://php.ca/manual/en/function.glob.php

    $aImages glob($sPath);
    sort($aImages);
    $iStart $iPage $iPerPage;
    $iTotal count($aImages);

    ?>
    <form method="post" action="<?php echo $_SERVER['SCRIPT_NAME'];?>">
        <table><!-- Tables are pretty old school.  Check with the Client developers for converting to floating divs -->
    <?php
    for ($i $iStart$i $iTotal; ++$i)
    {
        
    printf('<tr><td><input type="radio" name="btnBackground" value="%s" /></td><td><img src="%s" alt="" /></td></tr>'urlencode($aImages[$i]), $aImages[$i]);
    }
    ?>
            <tr>
                <td>
    <?php 
    if ($iPage 0)
    {
        
    printf('<a href="%s">Previous %d</a>', ($_SERVER['SCRIPT_NAME'] . '?page=' . ($iPage 1)), $iPerPage);
    }
    ?>
                </td>
                <td>
    <?php
    if ($iTotal < ($iPerPage $iPage))
    {
        
    printf('<a href="%s">Next %d</a>', ($_SERVER['SCRIPT_NAME'] . '?page=' . ($iPage 1)), $iPerPage);
    }
    ?>
                </td>
            </tr>
        <table>
        <input type="submit" value="Change Background" />
    </form>

  • Users who have thanked Fou-Lu for this post:

    EvilSupahFly (08-21-2011)

  • #3
    New Coder
    Join Date
    Aug 2011
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Smile

    That looks much better! Thanks! I can't wait to see how this turns out!


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •