我是PHP的初学者。 我想从表到主页面显示我的mysql数据库内容作为数据库表中项目数的分页(有限制)。我的代码显示一个具有精确分页的格式,并在第一页中绘制项目(如:http://www.swaminarayantravel.com/category.php?sc=Trekking),但问题是,当我尝试从下面的分页转到第2页时,它会转到错误页面。 我的代码是:
<?php
//get the function
include_once ('function.php');
include_once ('includes/connect.php');
$page = (int) (!isset($_GET["page"]) ? 1 : $_GET["page"]);
$limit = 4;
$startpoint = ($page * $limit) - $limit;
//to make pagination
$sql = "category where category.category='$selcat'";
?>
<div class="records round">
<ul class="homelist">
<?php
//show records
$query = mysql_query("SELECT * FROM {$sql} LIMIT {$startpoint} , {$limit}");
echo "<table>
<tr></tr>";
while ($row = mysql_fetch_assoc($query)) { ?>
<?php echo' <li > <img src="images/subcat/'.$row['file'] .'" height="100" width="100" alt="'.$row['subcategory'] .'"/>
<a class="title">';
$subcategory= $row["subcategory"];
echo "$subcategory";
echo'</a>
<p align="justify">';
$detail= $row["subcat_detail"];
echo "$detail";
echo ' </p> '; ?>
<?php } ?>
</table></div>
<?php echo pagination($sql,$limit,$page); ?>
和function.php页面包含以下代码:
<?php
功能分页($ query,$ per_page = 10,$ page = 1,$ url ='?'){
$ query =“SELECT COUNT(*)as num
FROM {$ query}”;
$ 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;
}
&GT;
答案 0 :(得分:1)
您的代码很完美,但您忘记在请求网址中添加“sc = Trekking”。 因此,在您的代码中,“$ selcat”可能变为空,并且sql查询不返回任何内容。
请试试这个: http://www.swaminarayantravel.com/category.php?sc=Trekking&page=2
也许你应该修改“分页”功能。
您的网站很精彩。 我记得我在尼泊尔的旅行。