Page 1 of 1

pagination problems - left in the lurch by my tech guy

Posted: Sat May 30, 2009 9:39 pm
by pchowitt
Hi, my tech guy left my project on short notice. Until I find someone to replace him (and money to do so) he has also left me some bugs to fix. My knowledge of php is nil but happy to learn. We have a paginator on the site that does not store/cache your page number which means that if a member of our site is on page 3 of 9 say in the div and click a result and then wish to go back they do not go back to page 3 of 9 using the back function on their browser (they always return to page 1!).

Code is below: all help appreciated. ps: I assume this is being done using the below file...it was labelled pagination.php on our server. Could it be a javascript issue? an example can be found at the bottom of the page on http://www.tailcast.com/art.html

Thanks

Code: Select all

 * Build the pagination data
     *
     * @param integer $nr_results The number of displayed items
     * @param array $params (optional) The parameters variable contains 2 things:
     *          - pagination: page, show, sort, order
     *          - sort_filter: array(....) - the default sort value will be the first element of this array
     * @param string $query_hash (optional) Query hash, ie: #comments. Remember to add the # too
     * @param array $private_params (optional) Private params, not passed in the pagination links
     * @return array
     */
    public function init($nr_results, $params=null, $query_hash=null, $private_params=null)
    {
        // set the pagination elements: page, show
        if (empty($params['pagination']['page']))
        {
            $params['pagination']['page'] = 1;
        }
        
        if (empty($params['pagination']['show']))
        {
            $params['pagination']['show'] = DEFAULT_ITEMS_PER_PAGE;
        }
 
        
        // set the pagination elements: sort
        if ($params['pagination']['sort'] && array_search($params['pagination']['sort'], (array)$params['sort_filter']) === false)
        {
            unset($params['pagination']['sort']);
        }
        
        if (empty($params['pagination']['sort']))
        {
            $params['pagination']['sort'] = $params['sort_filter'][0];
        }
        
        unset($params['sort_filter']);
        
        
        // set the pagination elements: order
        if ($params['pagination']['order'] != 'asc' && $params['pagination']['order'] != 'desc')
        {
            $params['pagination']['order'] = 'asc';
        }
        
        
        // set the "sort" and "limit" parameter values
        $params['sort'] = $params['pagination']['sort'].' '.$params['pagination']['order'];
        $params['limit'] = ($params['pagination']['page'] - 1) * $params['pagination']['show'].', '.$params['pagination']['show'];
    
 
        // count the number of pages
        $params['pagination']['nr_results'] = $nr_results;
        $params['pagination']['nr_pages'] = ceil($params['pagination']['nr_results'] / $params['pagination']['show']);
 
        
        // build the pages and return the entire pagination data
        return $this->buildPages($params, $query_hash, $private_params);
    }
    
    
    /**
     * Build the pages
     *
     * @param array $params Pagination parameters
     * @param string $query_hash (optional) Query hash, ie: #comments. Remember to add the # too
     * @param array $private_params (optional) Private params, not passed in the pagination links
     * @return array
     */
    protected function buildPages($params, $query_hash, $private_params)
    {
        // check for extra pagination params
        foreach ((array)$params['pagination'] as $key => $var)
        {
            if ($key != 'page' && $key != 'sort' && $key != 'order' && $key != 'show' && $key != 'nr_results' && $key != 'nr_pages')
            {
                $extra_vars[$key] = $var;
            }
        }        
        
        
        // build the base_url and sort_link
        $url =& Singleton::initController('url');
        
        $base_url = $url->buildGetUrl(array('page'), $extra_vars);
        $params['pagination']['sort_link'] = $url->buildGetUrl(array('page','sort','order'), $extra_vars);
        
        
        // set the private parameters
        foreach ((array)$private_params as $key => $value)
        {
            $params['pagination'][$key] = $value;
        }
        
        
        // if we don't have more then one page, return the results
        if ($params['pagination']['nr_pages'] <= 1)
        {
            return $params;
        }
          
        
        // build the first page
        if ($params['pagination']['page'] > 1)
        {
            $params['pagination']['first_page'] = $base_url.'page=1'.$query_hash;
        }
 
        // built the last page
        if ($params['pagination']['page'] < $params['pagination']['nr_pages'])
        {
            $params['pagination']['last_page'] = $base_url.'page='.$params['pagination']['nr_pages'].$query_hash;
        }
 
        // build the previous page
        if ($params['pagination']['page'] > 1)
        {
            $params['pagination']['previous_page'] = $base_url.'page='.($params['pagination']['page']-1).$query_hash;
        }
 
        // build the next page

Re: pagination problems - left in the lurch by my tech guy

Posted: Sun May 31, 2009 5:37 pm
by kcormier
This one is interested. For those who haven't looked at the page, the problem is that the pagination is done with javascript, and then when the user clicks anything it opens the page. Then when hitting the back button, the javascript defaults back to page 1. Anyone know of a way to store the javascript variable such that it isn't reset or a way of recovering it?

My suggestion is to store the page # in a cookie. The other option would be to store it in the db but that sounds like a lot of overhead to me!

-Kevin

Re: pagination problems - left in the lurch by my tech guy

Posted: Sun May 31, 2009 6:31 pm
by pchowitt
Thanks very much Kevin. Is there simple code I can input in the javascript file to store a page cookie?

Re: pagination problems - left in the lurch by my tech guy

Posted: Sun May 31, 2009 6:41 pm
by mikemike
GET variable and just print it into the javascript?

(note I haven't had time to read the code or thoroughly go through the website)

Re: pagination problems - left in the lurch by my tech guy

Posted: Sun May 31, 2009 7:32 pm
by pchowitt
These guys have found a way of doing it in ajax and javascript: http://www.dynamicdrive.com/dynamicinde ... /index.htm

have no idea how to make that work with my script...:(