简体   繁体   English

分页数据库查询问题

[英]Pagination Database Query Woes

I have been working at this all day and still at 8pm have had no luck. 我整天都在工作,直到晚上8点仍然没有运气。 I was wondering if you guys can give me some advice on fixing it. 我想知道你们是否可以给我一些修复建议。 I am building an Upload - gif sharing system for University. 我正在为大学建立一个Upload-gif共享系统。

Here is my code anyway - 无论如何这是我的代码-

 <?php
 ini_set('display_errors',1); 
 error_reporting(E_ALL);

 mysql_connect("localhost","root","root") or die("Top Query");
 mysql_select_db("UPLOAD") or die(mysql_error());   

 $count_query = mysql_query("SELECT NULL FROM details");
 $count = mysql_num_rows($count_query); 

 //pagination 

 if(isset($_GET['page'])){
$page = preg_replace("#[^0-9]#","",$_GET['page']);  
 }else{
$page= 1;
 }
 $perPage = 5;
 $lastPage = ceil($count / $perPage);

if($page < 1){
$page = 1;
}else if($page > $lastPage){
$page = $lastPage;
}
$limit = "LIMIT " .($page -1) * $perPage . ", $perPage";

//Query and gifs
$query = mysql_query("SELECT * FROM details ORDER BY date_added DESC") or die("2nd Query"); 
//Puts it into an array 


$pagination="";
if($lastPage != 1){

if($page != $lastPage){
    $next = $page + 1;
    $pagination.='<a href="index.php?page='.$next.'">More</a>';
}

if($page != 1){
    $prev = $page - 1;
    $pagination.='<a href="index.php?page='.$prev.'">Back</a>';
}
}
?>

And then the output in the html - 然后在html中输出-

<?php 
    while($info = mysql_fetch_array($query)){ 
        $shortlink = "<a href=uploads/".$info['photo'].">".$info['photo']."                   </a>" ;

    //Outputs the image and other data
    echo "<article class='upload-post'>" . "<div class='crop'>";
    echo "<a href=uploads/".$info['photo'].">";
    echo "<img class='scale-with-grid' src=uploads/".$info['photo'] .">"."</a>"; 
    echo "</div>";
    echo "".$info['name'] . "<br/>"; 
    echo "Reaction ".$info['reaction'] ."<br/>";
    echo "In " .$info['category'] ." <br/>";
    echo "On " .$info['date_added'] ." <br/>";
    echo "Link: $shortlink";
    echo "</article>";
 }


?>

 <?php echo $pagination;?>

I can toggle between pages but its not limiting the number of posts displayed on the page. 我可以在页面之间切换,但是它不限制页面上显示的帖子数量。 Id really appreciate the help as the deadline isn't too far away. 我非常感谢您的帮助,因为最后期限离我们不太远。 Thanks a bunch in advance! 提前谢谢一堆!

您永远不会在查询中附加$limit

Try the follwing as a replacement... 尝试以下替代品...

... ...

//Query and gifs
$query = mysql_query("SELECT * FROM details ORDER BY date_added DESC ".$limit) or die("2nd Query"); 
//Puts it into an array

... ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM