简体   繁体   中英

Codeigniter pagination ESCAPE PAGE NUMBER on URI

I got this search form. On my search form theres pagination function , calling data per pages. If its the original query ,I have no problem moving from one page to another but the problem lies when I do a search based on criterias. When I move from the first page to the second page, the second page directly load the original query result ,without the search criterias.

The idea of my search form is when there is $param , it contains the search conditions which would be exploded to be the search parameter.

index.php/admin/popup/history/1dps-2A-3ADMIN9

But When I click page 2, the uri becomes this:

index.php/admin/popup/history/ispager/2

If I added the $param as the $param2 , IT WORKS!

index.php/admin/popup/history/ispager/2/1dps-2A-3ADMIN9

Question: How can I escape the page number? Something that goes like:

<?php $this->mylib->pager_page(site_url("admin/popup/history/ispager/?/$param"), "table_list", "preloader"); ?>

so whatever page number, it still works

index.php/admin/popup/history/ispager/3/1dps-2A-3ADMIN9

history.php

<?php
//INIT
$pager=(isset($pager)) ? $pager : "1";

?>
<div id="table_list">
<?php $this->load->view("admin/history-list"); ?>
</div>

history-list.php

$arr=(isset($param)) ? explode("-", $param) : array();
    //var_dump($arr);
$search="";$search2="";$search3=""

 for($i=0;$i<count($arr);$i++){
$z = substr($arr[$i],0,1);
$y= substr($arr[$i],1);

    switch($z){
    case "1":
        $search1 =  "AND b.user_name LIKE'".$y."%'"; break;
    case "2":
        $search2 =  "AND c.admin_name LIKE'".$y."%'"; break;
    case "3": 
        $search3 = "AND ".$ltable."_bank LIKE'".$y."%'"; break;

    default:
    break;
    }
}
$s="SELECT * FROM
    ($table a INNER JOIN user b ON (a.".$ltable."_user_id=b.user_id))
    LEFT JOIN admin c ON (a.".$ltable."_admin_id=c.admin_id)
    WHERE a.".$ltable."_id<>'' $search1 $search2 $search3 ORDER BY a.".$ltable."_id DESC";

$this->mylib->pager_init($s, 1, $pager);
$q=$this->db->query($this->mylib->pager["dataQuery"]);


<table content>

On the bottom of the search form page:

Page: <?php $this->mylib->pager_page(site_url("admin/popup/history/ispager"), "table_list", "preloader"); ?>

Library: mylib.php

 public function pager_page($page="", $obj="", $loader=""){
        print "\n";
        if(($this->pager["page"]>6) && $this->pager["pagenum"]>6){
            print "<span class=\"pager\"><a href=\"$page/1\">&lt;&lt;</a></span>\n";
        }
        for($pageId=1; $pageId<=$this->pager["pagenum"]; $pageId++){
            if((($this->pager["pagenum"]-5)>0) && ($pageId>($this->pager["page"]-6)) && ($pageId<($this->pager["page"]+6)) || $this->pager["pagenum"]<6){
                if($pageId != $this->pager["page"]){
                    print "<span class=\"pager\"><a href=\"$page/$pageId\">$pageId</a></span>\n";
                }else{
                    print "<span class=\"pager_sel\">$pageId</span>\n";
                }
            }
        }
        if(($this->pager["page"]!=$this->pager["pagenum"]) && $this->pager["pagenum"]>6){
            print "<span class=\"pager\"><a href=\"$page/".((int)$this->pager["pagenum"])."\">&gt;&gt;</a></span>\n";
        }
        print "\n";
    }

In Codeigniter you can get segments using $this->uri->segment(n); // n=1 for controller, n=2 for method, etc

This provides you to retrieve information from your URI strings

consider this example http://example.com/index.php/controller/action/1stsegment/2ndsegment

it will return

$this->uri->segment(1); // controller
$this->uri->segment(2); // action
$this->uri->segment(3); // 1stsegment
$this->uri->segment(4); // 2ndsegment

I did a little revise on the pager_page function, adding an extra parameter for delivering param and it works.

history.php

$paramABC= $param;
if ($param =="ispager"){
$paramABC= $param2;
$arr=explode("-",$param2);
}

history-list.php

Page: <?php $this->mylib->pager_page(site_url("admin/popup/history/ispager"), "table_list", "preloader",$paramABC); ?>


mylib.php
    public function pager_page($page="", $obj="", $loader="",$param2=""){
        print "\n";
        if(($this->pager["page"]>6) && $this->pager["pagenum"]>6){
            print "<span class=\"pager\"><a href=\"$page/1/$param2\">&lt;&lt;</a></span>\n";
        }
        for($pageId=1; $pageId<=$this->pager["pagenum"]; $pageId++){
            if((($this->pager["pagenum"]-5)>0) && ($pageId>($this->pager["page"]-6)) && ($pageId<($this->pager["page"]+6)) || $this->pager["pagenum"]<6){
                if($pageId != $this->pager["page"]){
                    print "<span class=\"pager\"><a href=\"$page/$pageId/$param2\">$pageId</a></span>\n";
                }else{
                    print "<span class=\"pager_sel\">$pageId</span>\n";
                }
            }
        }
        if(($this->pager["page"]!=$this->pager["pagenum"]) && $this->pager["pagenum"]>6){
            print "<span class=\"pager\"><a href=\"$page/".((int)$this->pager["pagenum"])."/$param2\">&gt;&gt;</a></span>\n";
        }
        print "\n";
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM