[英]pagination not working with where condition
表格名稱-新聞
我創建分頁代碼
在這里,我從導航欄傳遞城市和州。 在此基礎上,我嘗試顯示帶有分頁的Row。
它可以工作,但是在2,3,4,5 ... N頁上顯示錯誤。
簡單分頁不使用where條件獲取數據。 請提出適當的解決方案。
當頁面加載時,它的URL像
本地主機/印地文/ city.php?狀態= 19&城市= 16
並在下面顯示分頁行。
但是當我點擊第2頁時,它會顯示網址
本地主機/印地文/ city.php?頁= 2
並顯示
錯誤
未定義索引:城市
未定義索引:狀態
導航代碼
<li><a href='city.php?state=20&city=18'>Bhopal</a></li>
<li><a href='city.php?state=20&city=19'>Indore</a></li>
和其他帶有分頁代碼的php代碼
<?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);
?>
//代碼以顯示頁面
<?php echo $Admin->pagination($statement,$limit,$page); ?>
分頁功能
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;
}
}
?>
在網址localhost/hindi/city.php?page=2
,沒有city
變量,因此未定義$_REQUEST['city']
。
state
變量也是一樣。
嘗試這個:
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);
你有兩個選擇
您需要在分頁方法中傳遞完整的url(除$ page以外的所有參數)
分頁($聲明,$限制,$頁, '?city.php狀態= 20&城市= 18'); ?>然后在分頁方法中添加$ page參數。 根據您當前的參數確定是否使用“&”可能有些棘手。
您可以在分頁方法中添加新參數以標識當前的url參數。 然后,您可以使用它們來生成用於分頁的完整URL。
函數分頁($ query,$ per_page = 10,$ page = 1,$ url ='?',$ parameters = array()){}
並在調用該方法時將當前的url參數($ city,$ state)作為數組傳遞。
你好嗎?
您必須將城市和州參數傳遞給分頁功能。
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>';
}
然后,通過$ _REQUEST調用它(如果從另一側生成參數,則調用另一個變量);
$Admin->pagination($statement,$limit, $page, $_REQUEST);
問候。
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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.