1

I added pagination to a module, in a mod.filename.php

Below is my code, it outputs the pagination (after it lists all the reviews) if you pass yes into the template tag parameter as seen below. After you click the first link for pagination, it appends the number to the end of the URL (ex: domain.com/news/1), then if you click another pagination link, it appends another number to the URL, (ex: domain.com/news/1/1). I know why this happens, because I am fetching the current URI.

I know I can pass segments into a parameter to append in the mod file, but I am looking for the cleanest, easiest (on the user) way possible.

{exp:module_name:reviews entry_id="{entry_id}" entry_type="electronics" pagination="yes"}
  // template tags
{/exp:module_name:reviews}

public function __construct()
{
    $this->EE =& get_instance();
    $this->EE->load->library('pagination');
}

private function paginate($entry_id, $entry_type)
{
    $p_config['base_url'] = $this->EE->functions->fetch_current_uri();
    $where = array(
        'entry_id'      =>  $entry_id,
        'entry_type'    =>  $entry_type,
    );

    $p_config['total_rows'] = $this->EE->db->where($where)->count_all_results('bmi_review');
    $p_config['per_page'] = 1; 

    $this->EE->pagination->initialize($p_config);
    $vars['pagination'] = $this->EE->pagination->create_links();

    return $vars['pagination'];
}

public function reviews()
{
    $entry_id = $this->EE->TMPL->fetch_param('entry_id', false);
    $entry_type = $this->EE->TMPL->fetch_param('entry_type', false);
    $pagination = $this->EE->TMPL->fetch_param('pagination', false);

    $reviews = $this->_get_data("reviews");

    $output = '';
    $count = 0;

    if(count($reviews) === 0)
        return false;

    foreach($reviews as $review)
    {
        $count++;

        $vars = array(
            'review'            =>  $review->review,
            'review_author'     =>  $review->author,
            'review_date'       =>  $review->date,
            'review_count'      =>  $count,
            'review_total'      =>  count($reviews),
        );

        $output .= $this->EE->TMPL->parse_variables_row($this->EE->TMPL->tagdata, $vars);
    }
        if($pagination == "yes")
            $output .= $this->paginate($entry_id, $entry_type);

    return $output;
}
1

Try this code:

$basepath = $this->EE->functions->create_url($this->EE->uri->uri_string);
$query_string = ($this->EE->uri->page_query_string != '') ? $this->EE->uri->page_query_string : $this->EE->uri->query_string;

if (preg_match("#^P(\d+)|/P(\d+)#", $query_string, $match))
{
    $start = (isset($match[2])) ? $match[2] : $match[1];
    $basepath = reduce_double_slashes(str_replace($match[0], '', $basepath));
}

$config['base_url']     = $basepath;

It will strip 'P1' segment off your URL.

2
  • It does not include P within the URL, I checked the documentation and do not see a config option for setting P or page, so creates a url: domain.com/news/page/1, ... /page/2 Is there a config option for that? If so, I can append 'page' to the URL and with your preg_match, I can strip whatever after the page segment of the url – Brad Oct 1 '13 at 19:42
  • $config['prefix'] = 'P'; sets the "P" prefix – Yuri Salimovskiy Oct 1 '13 at 19:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.