表名 - 新闻
我创建分页代码
这里我从导航栏传递城市和州。 在此基础上我试图用分页页面显示Row。
它有效,但在2,3,4,5 ... N页面上显示错误。
简单分页不在具有条件的地方获取数据。 请提出正确的解决方案。
页面加载时会收到类似
的网址本地主机/印地文/ 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;
}
}
?>
答案 0 :(得分:1)
在网址localhost/hindi/city.php?page=2
中没有city
变量,因此$_REQUEST['city']
未定义。
state
变量也是如此。
答案 1 :(得分:0)
试试这个:
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
答案 2 :(得分:0)
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);
答案 3 :(得分:0)
您有两个选择
您需要在分页方法中传递完整的网址(包含$ page以外的所有参数)
分页($声明,$限制,$页, '?city.php状态= 20安培;城市= 18'); ?&GT;然后在您的分页方法中添加$ page参数。确定是否使用“&amp;”可能有点棘手或不基于您当前的参数。
您可以在分页方法中添加新参数,以识别当前的url参数。然后,您可以使用它们生成分页的完整URL。
函数分页($ query,$ per_page = 10,$ page = 1,$ url ='?',$ parameters = array()){}
并在调用方法时将当前url参数($ city,$ state)作为数组传递。
答案 4 :(得分:0)
你必须将城市和州的参数传递给分页功能。
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(或另一个变量,如果你从另一边生成params);
$Admin->pagination($statement,$limit, $page, $_REQUEST);
问候。
答案 5 :(得分:0)
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
}