Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Passing $_GET parameters in pagination url
  • As the title indicated, I am trying to have $_GET parameters included in the pagination url. so far what i get is something like :
    seg1/seg2/?param1=x¶m2=y¶m3=z/page_number/ it would be more elegant to have something like seg1/seg2/page_number/?param1=x¶m2=y¶m3=z FYI : param 1 , param 2 & param 3 are filtering parameters for the query so they are critical . I appreciate any help thank you
  • Can't be done without extending the pagination class. It expects the 'root' URL for your page, and just sticks the rest on to it, as you have discovered. I store filtering and sort order in the session. It keeps the URL's clean, and also allows you to remember the users selection when he later revisits the page. If you want to add support for GET variables, make the modifcations and pull request.
  • Thank you for your quick reply!, I will make the mods and make a change request.
  • I have emailed you guys my modified pagination class to email@fuelphp.com
    here it is for others to use pending it makes it to the core :
    class Pagination { /**
    * @var integer The current page
    */
    public static $current_page = null; /**
    * @var integer The offset that the current page starts at
    */
    public static $offset = 0; /**
    * @var integer The number of items per page
    */
    public static $per_page = 10; /**
    * @var integer The number of total pages
    */
    public static $total_pages = 0; /**
    * @var array The HTML for the display
    */
    public static $template = array(
    'wrapper_start' => '<div class="pagination"> ',
    'wrapper_end' => ' </div>',
    'page_start' => '<span class="page-links"> ',
    'page_end' => ' </span>',
    'previous_start' => '<span class="previous"> ',
    'previous_end' => ' </span>',
    'previous_mark' => '« ',
    'next_start' => '<span class="next"> ',
    'next_end' => ' </span>',
    'next_mark' => ' »',
    'active_start' => '<span class="active"> ',
    'active_end' => ' </span>',
    ); /**
    * @var integer The total number of items
    */
    protected static $total_items = 0; /**
    * @var integer The total number of links to show
    */
    protected static $num_links = 5; /**
    * @var integer The URI segment containg page number
    */
    protected static $uri_segment = 3; /**
    * @var mixed The pagination URL
    */
    protected static $pagination_url; /**
    * Init
    *
    * Loads in the config and sets the variables
    *
    * @access public
    * @return void
    */
    public static function _init()
    {
    $config = \Config::get('pagination', array()); static::set_config($config);
    } //

    /**
    * Set Config
    *
    * Sets the configuration for pagination
    *
    * @access public
    * @param array $config The configuration array
    * @return void
    */
    public static function set_config(array $config)
    { foreach ($config as $key => $value)
    {
    if ($key == 'template')
    {
    static::$template = array_merge(static::$template, $config);
    continue;
    } static::${$key} = $value;
    } static::initialize();
    } //

    /**
    * Prepares vars for creating links
    *
    * @access public
    * @return array The pagination variables
    */
    protected static function initialize()
    {
    static::$total_pages = ceil(static::$total_items / static::$per_page) ?: 1; static::$current_page = (int) \URI::segment(static::$uri_segment); if (static::$current_page > static::$total_pages)
    {
    static::$current_page = static::$total_pages;
    }
    elseif (static::$current_page < 1)
    {
    static::$current_page = 1;
    } // The current page must be zero based so that the offset for page 1 is 0.
    static::$offset = (static::$current_page - 1) * static::$per_page;
    } //

    /**
    * Creates the pagination links
    *
    * @access public
    * @return mixed The pagination links
    */
    public static function create_links()
    {
    if (static::$total_pages == 1)
    {
    return '';
    } \Lang::load('pagination', true); $pagination = static::$template;
    $pagination .= static::prev_link(\Lang::line('pagination.previous'));
    $pagination .= static::page_links();
    $pagination .= static::next_link(\Lang::line('pagination.next'));
    $pagination .= static::$template; return $pagination;
    } //

    /**
    * Pagination Page Number links
    *
    * @access public
    * @return mixed Markup for page number links
    */
    public static function page_links()
    {
    if (static::$total_pages == 1)
    {
    return '';
    } $pagination = ''; // Let's get the starting page number, this is determined using num_links
    $start = ((static::$current_page - static::$num_links) > 0) ? static::$current_page - (static::$num_links - 1) : 1; // Let's get the ending page number
    $end = ((static::$current_page + static::$num_links) < static::$total_pages) ? static::$current_page + static::$num_links : static::$total_pages; for($i = $start; $i <= $end; $i++)
    {
    if (static::$current_page == $i)
    {
    $pagination .= static::$template.$i.static::$template;
    }
    else
    {
    $url = ($i == 1) ? '' : '/'.$i;
    $pagination .= self:: create_url($i, $url);
    }
    } return static::$template.$pagination.static::$template;
    } //

    /**
    * Pagination "Next" link
    *
    * @access public
    * @param string $value The text displayed in link
    * @return mixed The next link
    */
    public static function next_link($value)
    {
    if (static::$total_pages == 1)
    {
    return '';
    } if (static::$current_page == static::$total_pages)
    {
    return $value.static::$template;
    }
    else
    {
    $next_page = static::$current_page + 1; return self:: create_url($value.static::$template, $next_page);
    }
    } //

    /**
    * Pagination "Previous" link
    *
    * @access public
    * @param string $value The text displayed in link
    * @return mixed The previous link
    */
    public static function prev_link($value)
    { if (static::$total_pages == 1)
    {
    return '';
    } if (static::$current_page == 1)
    {
    return static::$template.$value;
    }
    else
    {
    $previous_page = static::$current_page - 1;
    $previous_page = ($previous_page == 1) ? '' : '/'.$previous_page; return self:: create_url(static::$template.$value, $previous_page);
    }
    } public static function create_url($url_label,$page_number) { $get = null; if(count($_GET)>0) { $get='?'; foreach ($_GET as $array_key=>$array_value){ $get .= $array_key.'='.$array_value; if($array_value != end($_GET)) { $get .= '&';}
    }
    } return \Html::anchor(rtrim(static::$pagination_url, '/').$page_number.$get, $url_label); }
    }
  • I'm afraid it's not going to work like that. Fork the github fuel/core repository, make the 1.1/develop branch current, apply your modifications, and send a pull request. That allows us to quickly verify your changes, and merge them when we approve.
  • Thank for clarifying. will do :)
  • As an alternative to storing page state in a session, you could use the new HTML 5 local storage option that most browsers support now. Just a quick thought :).

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion