您好我有以下代码对我的数据进行分页,我也有一些复选框可以调整显示的结果。我遇到的问题是,当我在1页上有所有内容时它工作正常,但当结果分页时,分页将不会显示在底部,它只显示14个结果,就是这样。这是我的代码
if(isset($_POST['criteria']) && !empty($_POST['criteria'])){
foreach($_POST['criteria'] as $key=>$value){
// Runs mysql_real_escape_string() on every value encountered.
$clean_criteria = array_map('mysql_real_escape_string', $_REQUEST['criteria']);
//get the function
include ($_SERVER['DOCUMENT_ROOT'] .'/script/pagination-functions.php');
$page = (int) (!isset($_GET["page"]) ? 1 : $_GET["page"]);
$limit = 14;
$startpoint = ($page * $limit) - $limit;
// Convert the array into a string.
$criteria = implode("','", $clean_criteria);
if(!$query=mysql_query("SELECT * FROM mytable WHERE exercise IN ('$criteria') ORDER BY long ASC LIMIT {$startpoint} , {$limit}"))
{
echo "Cannot parse query";
}
elseif(mysql_num_rows($query) == 0) {
echo "No records found";
}
while($row = mysql_fetch_assoc($query)) {
echo "".$row['title'] ."<br>
".$row['description'] ."";
}
echo "<div class=\"new-pagination\">";
echo pagination($statement,$limit,$page);
echo "</div>";
}
}
这是我的分页脚本
function pagination($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 = "1";
$page = ($page == 0 ? 1 : $page);
$start = ($page - 1) * $per_page;
$firstPage = 1;
$prev = ($page == 1)?1:$page - 1;
$prev = $page - 1;
$next = $page + 1;
$lastpage = ceil($total/$per_page);
$lpm1 = $lastpage - 1;
$pagination = "";
if($lastpage > 1)
{
$pagination .= "<ul class=\"pagination1\">";
if ($page == 1)
{
$pagination.= "<li><a class=\"inactive\">First</a></li>";
$pagination.= "<li><a class=\"inactive\">Prev</a></li>";
}
else
{
$pagination.= "<li><a href=\"{$url}&page=$firstPage\">First</a></li>";
$pagination.= "<li><a href=\"{$url}&page=$prev\">Prev</a></li>";
}
$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=\"inactive\">Next</a></li>";
$pagination.= "<li><a class=\"inactive\">Last</a></li>";
}
$pagination.= "</ul>\n";
}
return $pagination;
}
我还需要存储所选复选框的数据,以便为下一页保留结果。如果有人能给我任何帮助或指出我正确的方向,我会非常感激。
答案 0 :(得分:0)
提供代码已完成,$statement
变量未定义,因此pagination
函数中的SQL语法无效,因为$query
为空。
另请注意,在最后一个for
循环的第三个表达式中没有运算符。所以代码甚至都没有编译。
您是否在PHP中启用了警告?可能不是,否则你会得到一些,他们想引导你去解决。请启用它们并分享。你会变得更好。