Jump to content

save key+value in array in order to remember current page per user


gjschouten

Recommended Posts

Hey all..

 

I'm trying to pick up PHP by reading ebooks and follow tutorials. I am now busy with creating a thumbnailgallery from uploaded pics per user.

I want to let the visitor scroll through a row of thumbnails by clicking next or previous.

There are 3 different rows of thumbnails.

Here's how it looks like:

 

<<previous (row1 of 6thumbnails) next>>

 

<<previous (row2 of 6thumbnails) next>>

 

<<previous (row3.....                  )  next>>

 

when page loads mysql fetches rows of information for the filenames...then there is a foreach row loop that does first a do ..while loop to build up the thumbnailrows...and then creates the navigation links previous  and next who send the "page"+1 or "page"-1 to _GET array so that the thumbnail 7 until 12 will be shown.

 

The problem i run into is that i want the main code page to remember at which "page" of scrolling through the thumnails he is. Now it is that when a user scrolls one row of thumbnails and stops at the 3rd "page" of it and then clicks on a different row to go to "page" 2 of that row that all the rows jump together back to "page" 1.

 

I thought there may be a solution in storing the number of row and the number of page into an array but can't seem to find a solution....

 

Has anybody got experience in this?

Help would be very welcome i'm totally stuck and cant read about it in tutorials/ebooks....

greets from holland..

 

GJ

you have 3 different sets of 6 thumbnails. and you want each one to be able to scroll separately.

 

you should either:

a. set 3 different page= in the url (like page1=x&pag2=y&page3=z)

 

or

b. use cookies to set the page. you can set an array with the 3 pages in one cookie.

 

if you use cookies then to go up and down you can do a ?page=x&value=(1 or -1).

Hey there wolf..

 

Thanks for your reply. The best option is using the cookie then i guess..because i have plans for a lot more thumbnailrows then these 3. Do you maybe have a good link (ebook/tutorial/etc.) where i can read specific info about this matter? i.e. storing array in cookie iot remember internal pointer of pagenr.???

 

I ask this because cookies are new to me..ebooks so far didn't discuss them.

 

thx..gj

http://php.net/manual/en/features.cookies.php - this has a lot of information on cookies.

 

You want to look at setcookie() and $_COOKIE

 

it is simple once you get it down. if you need any help just let me know.

<?php
class Cookie
{
    // Reserved session keys
    private static $_reserved = array();

    // Static class cannot be initialized
    private function __construct() {}
   
    // Alias for delete() function
    public static function del($key)
    {
        self::delete($key);
    }
   
    // Delete a cookie
    public static function delete($key)
    {
        // Change string representation array to key/value array
        $key = self::_scrubKey($key);
       
        // Make sure the cookie exists
        if (self::exists($key))
        {           
            // Check for key array
            if (is_array($key))
            {
                // Grab key/value pair
                list ($k, $v) = each($key);
               
                // Set string representation
                $key = $k . '[' . $v . ']';
               
                // Set expiration time to -1hr (will cause browser deletion)
                setcookie($key, false, time() - 3600);
               
                // Unset the cookie
                unset($_COOKIE[$k][$v]);
            }
           
            // Check for cookie array
            else if (is_array($_COOKIE[$key]))
            {
                foreach ($_COOKIE[$key] as $k => $v)
                {
                    // Set string representation
                    $cookie = $key . '[' . $k . ']';
                   
                    // Set expiration time to -1hr (will cause browser deletion)
                    setcookie($cookie, false, time() - 3600);
                   
                    // Unset the cookie
                    unset($_COOKIE[$key][$k]);
                }
            }
           
            // Unset single cookie
            else
            {
                // Set expiration time to -1hr (will cause browser deletion)
                setcookie($key, false, time() - 3600);
               
                // Unset key
                unset($_COOKIE[$key]);
            }
        }
    }
   
    // See if a cookie key exists
    public static function exists($key)
    {
        // Change string representation array to key/value array
        $key = self::_scrubKey($key);
       
        // Check for array
        if (is_array($key))
        {
            // Grab key/value pair
            list ($k, $v) = each($key);
           
            // Check for key/value pair and return
            if (isset($_COOKIE[$k][$v])) return true;
        }
       
        // If key exists, return true
        else if (isset($_COOKIE[$key])) return true;
       
        // Key does not exist
        return false;
    }
   
    // Get cookie information
    public static function get($key)
    {
        // Change string representation array to key/value array
        $key = self::_scrubKey($key);
       
        // Check for array
        if (is_array($key))
        {
            // Grab key/value pair
            list ($k, $v) = each($key);
           
            // Check for key/value pair and return
            if (isset($_COOKIE[$k][$v])) return $_COOKIE[$k][$v];
        }

        // Return single key if it's set
        else if (isset($_COOKIE[$key])) return $_COOKIE[$key];
           
        // Otherwise return null
        else return null;
    }
   
    // Return the cookie array
    public static function contents()
    {
        return $_COOKIE;
    }
   
    // Set cookie information
    public static function set(
        $key,
        $value,
        $expire = 0,            /* Default expire time (session, 1 week = 604800) */
        $path = '',             /* Default path */
        $domain = '',           /* Default domain */
        $secure = false,        /* Does this cookie need a secure HTTPS connection? */
        $httponly = true        /* Can non-HTTP services access this cookie (IE: javascript)? */
    ){       
        // Make sure they aren't trying to set a reserved word
        if (!in_array($key, self::$_reserved))
        {       
            // If $key is in array format, change it to string representation
            $key = self::_scrubKey($key, true);
               
            // Store the cookie
            setcookie($key, $value, $expire, $path, $domain, $secure, $httponly);   
        }
           
        // Otherwise, throw an error
        else Error::warning('Could not set key -- it is reserved.', __CLASS__);
    }
   
    // Converts strings to arrays (or vice versa if toString = true)
    private static function _scrubKey($key, $toString = false)
    {
        // Converting from array to string
        if ($toString)
        {
            // If $key is in array format, change it to string representation
            if (is_array($key))
            {
                // Grab key/value pair
                list ($k, $v) = each($key);
               
                // Set string representation
                $key = $k . '[' . $v . ']';
            }
        }
       
        // Converting from string to array
        else if (!is_array($key))
        {
            // is this a string representation of an array?
            if (preg_match('/([\w\d]+)\[([\w\d]+)\]$/i', $key, $matches))
            {
                // Store as key/value pair
                $key = array($matches[1] => $matches[2]);
            }
        }
       
        // Return key
        return $key;
    }
}
?>

@@Example usage:

<?php
// Basic usage
Cookie::set('testcookie', 'test');
print(Cookie::get('testcookie'));

// You can set 'array' cookies in two different ways:
Cookie::set('array[one]', 'item one');
Cookie::set(array('array' => 'two'), 'item two');

// Likewise, you can also get 'array' cookies in two different ways:
print(Cookie::get('array[one]'));
print(Cookie::get(array('array' => 'one')));

// Or you can grab the whole array at once:
print_r(Cookie::get('array'));

// Deleting cookies is done in the same way:
Cookie::del('array[one]');
Cookie::del(array('array' => 'two'));

// Delete the entire array:
Cookie::del('array');

// Print contents of $_COOKIE (refresh for this)
print '<pre>';
print_r(Cookie::contents());
print '<pre>';
?>

 

You may find this helpful.

 

All credit to the creator. Kyle Florence

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.