简体   繁体   中英

pagination not working with where condition

table name - news

I create pagination code

here I am passing city and state from Navigation bar. And on basis of that i Tried to show Row with pagination pages.

It works but on 2,3,4,5...N pages it shows error.

Simply Pagination Not fetching data with where condition. plz suggest proper solution.

when page loads it gets url like

localhost/hindi/city.php?state=19&city=16

and display pagination rows below.

But when i click on page2 it show url like

localhost/hindi/city.php?page=2

and shows

error

Undefined index: city

Undefined index: state

Navigation code

<li><a href='city.php?state=20&city=18'>Bhopal</a></li>
  <li><a href='city.php?state=20&city=19'>Indore</a></li>

and other php code with pagination code

<?php 
require_once('includes/config.php');
$Admin = new admins;
$baseString = "detail.php";
$cit = $_REQUEST['city'];
$sta = $_REQUEST['state'];
?>

<?php           
$page = (int) (!isset($_GET["page"]) ? 1 : $_GET["page"]);
$limit = 1;
$startpoint = ($page * $limit) - $limit;
$statement = "news";
?>
<?php
mysql_set_charset('utf8');
$sql="select id,story,headline,photo from {$statement} where state_id = '$sta' and city_id = '$cit' order by id desc LIMIT {$startpoint} , {$limit}";
$query=mysql_query($sql);
?>

//code to display pages

<?php echo $Admin->pagination($statement,$limit,$page); ?>

pagination function

function pagination($query, $per_page = 10,$page = 1, $url = '?')
        {        
        $query = "SELECT COUNT(*) as `num` FROM {$query}";
        //print_r($query);
        //exit;
        $row = mysql_fetch_array(mysql_query($query));
        $total = $row['num'];
        $adjacents = "2"; 

        $page = ($page == 0 ? 1 : $page);  
        $start = ($page - 1) * $per_page;                               

        $prev = $page - 1;                          
        $next = $page + 1;
        $lastpage = ceil($total/$per_page);
        $lpm1 = $lastpage - 1;

        $pagination = "";
        if($lastpage > 1)
        {   
            $pagination .= "<ul class='pagination'>";
                    $pagination .= "<li class='details'>Page $page of $lastpage</li>";
            if ($lastpage < 7 + ($adjacents * 2))
            {   
                for ($counter = 1; $counter <= $lastpage; $counter++)
                {
                    if ($counter == $page)
                        $pagination.= "<li><a class='current'>$counter</a></li>";
                    else
                        $pagination.= "<li><a href='{$url}page=$counter'>$counter</a></li>";                    
                }
            }
            elseif($lastpage > 5 + ($adjacents * 2))
            {
                if($page < 1 + ($adjacents * 2))        
                {
                    for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
                    {
                        if ($counter == $page)
                            $pagination.= "<li><a class='current'>$counter</a></li>";
                        else
                            $pagination.= "<li><a href='{$url}page=$counter'>$counter</a></li>";                    
                    }
                    $pagination.= "<li class='dot'>...</li>";
                    $pagination.= "<li><a href='{$url}page=$lpm1'>$lpm1</a></li>";
                    $pagination.= "<li><a href='{$url}page=$lastpage'>$lastpage</a></li>";      
                }
                elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
                {
                    $pagination.= "<li><a href='{$url}page=1'>1</a></li>";
                    $pagination.= "<li><a href='{$url}page=2'>2</a></li>";
                    $pagination.= "<li class='dot'>...</li>";
                    for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
                    {
                        if ($counter == $page)
                            $pagination.= "<li><a class='current'>$counter</a></li>";
                        else
                            $pagination.= "<li><a href='{$url}page=$counter'>$counter</a></li>";                    
                    }
                    $pagination.= "<li class='dot'>..</li>";
                    $pagination.= "<li><a href='{$url}page=$lpm1'>$lpm1</a></li>";
                    $pagination.= "<li><a href='{$url}page=$lastpage'>$lastpage</a></li>";      
                }
                else
                {
                    $pagination.= "<li><a href='{$url}page=1'>1</a></li>";
                    $pagination.= "<li><a href='{$url}page=2'>2</a></li>";
                    $pagination.= "<li class='dot'>..</li>";
                    for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
                    {
                        if ($counter == $page)
                            $pagination.= "<li><a class='current'>$counter</a></li>";
                        else
                            $pagination.= "<li><a href='{$url}page=$counter'>$counter</a></li>";                    
                    }
                }
            }

            if ($page < $counter - 1){ 
                $pagination.= "<li><a href='{$url}page=$next'>Next</a></li>";
                $pagination.= "<li><a href='{$url}page=$lastpage'>Last</a></li>";
            }else{
                $pagination.= "<li><a class='current'>Next</a></li>";
                $pagination.= "<li><a class='current'>Last</a></li>";
            }
            $pagination.= "</ul>\n";        
        }


        return $pagination;
    }

}
?>

In url localhost/hindi/city.php?page=2 there is no city variable so $_REQUEST['city'] is not define.
It's the same thing for state variable.

Try this:

if there are no page then 
let
page=1
limit=1
now startpoint=(1*1)-1=0 //correct

if you use 

page=2
then
SP=(2*1)-1=1 // one is start point and one is limit ?

i think its correct 

show me error message.. PLZ
in method pagination($statement,$limit,$page) 
you pass only 3 variable but in method required constant 4 variable

for optionaly variable use 

example

function($var=null,$var2=null){
/*....your program....*/
}
now you can call
function();
function($arg1);
function($arg1,$arg2);

You have two options

  1. You need to pass full url (with all parameters except $page) in your Pagination method

    pagination($statement,$limit,$page,'city.php?state=20&city=18'); ?>

Then add $page parameter in your pagination method. It could be a bit tricky to determine whether to use "&" or not based on your current parameter.

  1. You can add a new parameter in your pagination method to identify the current url parameters. You can then use them to generate full urls for pagination.

    function pagination($query, $per_page = 10,$page = 1, $url = '?', $parameters=array()){}

and pass the current url parameters ($city, $state) as an array when calling the method.

how are you?

You have to pass city and state params to the pagination function.

function pagination($query, $per_page = 10, $page = 1, $params = array()) {
    ...
    $params['page'] = $page; // or $counter or $nextPage, ...
    ...
    $url = http_build_query($params);
    $html = '<a href="' . $url . '">XXX</a>';
}

Then, you call it passing the $_REQUEST (or another variable if you are generating params from other side);

$Admin->pagination($statement,$limit, $page, $_REQUEST);

Regards.

undefiend index are not a programming error
if comes because you not pass any request city in url
for ignore you can use error_reporting(0);

or use

if(isset($_REQUEST['city'])){
$city=$_REQUEST['city'];
}
else{
//....
//$city="default value";
}

if(isset($_REQUEST['state'])){
$state=$_REQUEST['state'];
}
else{
//default operation
}

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