I have a query by different attributes. When one query is started and we are for example on page 7, and a new attribute is chosen, the query still shows the 7 page of new results. And it should show page 1 as the new attribute is chosen. Could you help me with that?

<?php

class Pagination 
{


public $current_page;

public $per_page;

public $total_count;

public $links_on_page = 10;

public $page_links = 15;

public $current_page_string = '';


public function __construct($page=1, $per_page=10, $total_count=0){
        $this->current_page = ($page > 0) ? (int)$page : 1;
        $this->per_page = abs((int)$per_page);
        $this->total_count = abs((int)$total_count);
    }



    public function offset() {
        return ($this->current_page - 1) * $this->per_page;
    }



public function total_pages() {
        return ceil($this->total_count/$this->per_page);
    }



public function previous_page() {
        return $this->current_page - 1;
    }



public function next_page() {
        return $this->current_page + 1;
    }



public function has_previous_page() {
        return $this->previous_page() >= 1 ? true : false;
    }



public function has_next_page() {
        return $this->next_page() <= $this->total_pages() ? true : false;
    }



public function remaining_links($dim = 0){
        return $this->page_links = $this->page_links - $dim;
    }



    public function render_pagination_bar($self = '', $addcond = '')
{
    $cond='';
        if($addcond)
{
            foreach($addcond as $k=>$v)
{
                $cond.="&pattr_{$k}=$v";

}

        }



$return = '';


if($this->total_pages() > 1) {
            if($this->has_previous_page()) 

{ 
                $return .= a( href("$self&page=".$this->previous_page().$cond) , "« Poprzednia", null, 'link', null, null, null, true);

}



            if($this->total_pages() > 10):
                //print first


if($this->current_page > 6):
                    if($this->current_page == 1) {

$this->current_page_string = "{$self}&page=1";
                        $return .= span(1, null, 'selectedpage page_num');

                    } else {
                        $return .= a( href("{$self}&page=1{$cond}") , 1, null, 'page_num', null, null, null, true);
                    }
                    //append dots


                if($this->current_page > 7)
                    $return .= " ... ";
                endif;

                //print current and 3 pages bck and 5 pages fwd
                $startcondition = ($this->current_page > 5) ? ($this->current_page - 5) : 1;
                $endcondition = ($this->current_page < ($this->total_pages() - 6)) ? ($this->current_page + 5) : $this->total_pages();


                for($i = $startcondition; $i <= $endcondition; $i++) {
                    if($i == $this->current_page) {
                        $this->current_page_string = "{$self}&page={$i}{$cond}";
                        $return .= span($i, null, 'selectedpage page_num');
                    } else {
                        $return .= a( href("{$self}&page={$i}{$cond}") , $i, null, 'page_num', null, null, null, true);
                    }
                }

                //print last

if($this->current_page < ($this->total_pages() - 6)):
                    //append dots
                    $return .= " ... ";

                    if($this->current_page == $this->total_pages()) {
                        $this->current_page_string = "{$self}&page=".$this->total_pages();
                        $return .= span($this->total_pages(), null, 'selectedpage page_num');
                    } else {
                        $return .= a( href("{$self}&page=".$this->total_pages().$cond) , $this->total_pages(), null, 'page_num', null, null, null, true);
                    }
                endif;
            else:
                for($i=1; $i <= $this->total_pages(); $i++) {
                    if($i == $this->current_page) {
                        $this->current_page_string = "{$self}&page={$i}{$cond}";
                        $return .= span($i, null, 'selectedpage page_num');
                    } else {
                        $return .= a( href("{$self}&page={$i}{$cond}") , $i, null, 'page_num', null, null, null, true);
                    }
                }
            endif;


            if($this->has_next_page()) { 
                $return .= a( href("$self&page=".$this->next_page().$cond) , "» Następna", null, 'link', null, null, null, true);
            }
        }
        return (!empty($return)) ? div($return, "pagination", "textcenter") : null;
    }
}
?>

Recommended Answers

All 5 Replies

Are you appending the query string to the form used to submit the new query? Can you show how you apply the pagination class in your results script?

This code is painful to look at. I'll give it a shot at helping.

I can't figure out what the problem is (due to DaniWeb's word wrap). Sorry. :/

Thank you for your intentions to help. A colleague come up with a solution in JS:

var re = new RegExp("&page=(\d*)","g");
            loc = loc.replace(re, '');

The problem is that with some filtering one after another it cuts out the &page= and everything is messed up. Another colleauge suggested that the best way to cut out "&page=XX" but the &page fragment is occuring in different places of the link. I paste the JS file here. I hope someone got the solution. Thank you.

function changeSort(){
    $(function(){
        $("#sortselector").change(function(){
            var loc = location.href;
            loc = loc.replace(/&orderby=\w+/, '');
            loc = loc.replace(/&orderdir=\w+/, '');
            location.href = loc + "&orderby=" + $(this).val() + "&orderdir=" + $("#sortorderselector").val();
        });
        $("#sortorderselector").change(function(){
            var loc = location.href;
            loc = loc.replace(/&orderby=\w+/, '');
            loc = loc.replace(/&orderdir=\w+/, '');
            location.href = loc + "&orderby=" + $("#sortselector").val() + "&orderdir=" + $(this).val();
        });
    });
}
function priceFilter(){
    $(function(){
        $(".price_input").keyup(function(){
            var inputValue = $(this).val();
            $(this).val(inputValue.replace(/\,/i, '.'));
        });

        $("#pricefilter").click(function(){
            var 
                price_from = $("#price_from").val(),
                price_to = $("#price_to").val(),
                loc = location.href,
                repf = new RegExp("&price_from=[^&]*","g"),
                rept = new RegExp("&price_to=[^&]*","g");
            loc = loc.replace(repf, '');
            loc = loc.replace(rept, '');

            if(price_from.length)
                loc = loc + "&price_from=" + price_from;
            if(price_to.length)
                loc = loc + "&price_to=" + price_to;
            location.href = loc;
        });
    });

}
function changeAdvSel(){
    $("#categoryid").change(function(){
        var loc = location.href;
        loc = loc.replace(/\?q=categoryproducts\/view&categoryid=\d+/, '');
        location.href = loc + "?q=categoryproducts/view&categoryid=" + $(this).val();
    });

    $(".advselector").change(function(){
        var attrselId = $(this).attr('id');
        var loc = location.href;
        var re = new RegExp("&" + attrselId + "=[^&]*","g");
        loc = loc.replace(re, '');
        location.href = loc + "&" + attrselId + "=" + $(this).val();
    });

}

function changeAdvSelPage(){
    $(function(){
        $(".advselector").multiselect({
            selectedList: 2,
            selectedText: "Wybrano: #",
            classes: "advsel"
        });

        $("#advselectorfilter").click(function(){
            var filterstring = '';
            var loc = location.href;

            $(".advselector").each(function(){
                var selectid = $(this).attr('id');
                var selectvals = $(this).val();
                if($(selectvals).length > 0){
                    var selectvalsString = selectvals.join(':::');
                    filterstring += '&' + selectid + '=' + selectvalsString;
                }
                var re = new RegExp("&" + selectid + "=[^&]*","g");
                loc = loc.replace(re, '');
            });
            var re = new RegExp("&page=(\d*)","g");
            loc = loc.replace(re, '');
            location.href = loc + filterstring;
        });

    });

}

Are you trying to remove the page argument through javascript because you've previously added it through javascript?

I repeat: can you show us how you implement the pagination? Better: can you show us how you create these links? Perhaps by appending the query string to the form action? Example:

<form action="index.php<?php echo isset($_SERVER['QUERY_STRING']) ? '?'.$_SERVER['QUERY_STRING']:''; ?>">
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.