2016-06-15 78 views
-1

我觉得我问了很多问题。PHP分页不工作,我不知道为什么

不管怎样,我正在写一些数据库条目分页,它看起来声音给我,但是当我点击链接它只是显示前10个职位,并没有别的。

整个事情就在这里:

 <?php 
      $post_limit = 10; 

      $conn = new mysqli($servername, $username, $password, $dbname); 
      if ($conn->connect_error) { 
       die("failed to connect: " . $conn->connect_error); 
      } 

      $sql = "SELECT count(id) FROM $tablename"; 
      $result = mysqli_query($conn, $sql); 

      if (!$result) { 
       echo "you fucked up"; 
      } else { 
       echo $row["id"]; 
      } 

      $row = mysqli_fetch_array($result, MYSQL_NUM); 
      $post_count = $row[0]; 

      if(isset($_GET['page'])) { 
       $page = $_GET['page'] + 1; 
       $offset = $post_limit * $page; 
      } else { 
       $page = 0; 
       $offset = 0; 
      } 

      $post_left = $post_count - ($page * $post_limit); 

      $sql = "SELECT id, upvotes, downvotes, name, title, message, date, time FROM posts ORDER BY date DESC, time DESC LIMIT $offset, $post_limit"; 
      $result = $conn->query($sql); 

      if ($result->num_rows > 0) { 
       while($row = $result->fetch_assoc()) { 
        echo "<br><div id='messageBar'>"; 
        echo " &#10070; </b>"; 
        echo "Posted by <b>"; 
        echo htmlspecialchars($row["name"]); 
        echo "</b> on "; 
        echo $row["date"]; 
        echo " at "; 
        echo $row["time"]; 
        if (!empty($row['title'])) { 
         echo " - <b>"; 
         echo htmlspecialchars($row["title"]); 
         echo "</b>"; 
        } 
        echo "<span style='float: right'>#"; 
        echo $row["id"]; 
        echo "</span>"; 
        echo "</div><div id='messageContent'>"; 
        echo htmlspecialchars($row["message"]); 
        echo "<br><br><span id='commentLink'><a class='commentLink' href='thread.php?id=$row[id]'>view thread&nbsp;</a></span>"; 
        echo "<br></div><br><hr>"; 
       } 
      } else { 
       echo "<br>"; 
       echo "<center><i>it's dusty in here</i></center>"; 
       echo "<br>"; 
      } 

      if ($page > 0) { 
       $last = $page - 2; 
       echo "<a href='$_PHP_SELF?page = $last'>previous page</a> | "; 
       echo "<a href='$_PHP_SELF?page = $page'>next page</a>"; 
      } else if ($page == 0) { 
       echo "<a href='$_PHP_SELF?page = $page'>next page</a>"; 
      } else if ($post_left < $post_limit) { 
       $last = $page - 2; 
       echo "<a href='$_PHP_SELF?page = $last'>previous page</a>"; 
      } 

      $conn->close(); 
     ?> 

的链接,下一个页面出现在底部,但点击它把你带到你已经在用相同的最近10个帖子的页面。

我想学习PHP的,因为我去,所以我感谢所有帮助。谢谢!

+0

正确的语法是'$ _GET [ '页']','不是$ _GET { '页面'}'。 – aynber

+0

谢谢,修正了这种语法,但问题仍然存在。 – Treedot

+0

这是我经常提醒我编程时很糟糕 – Treedot

回答

0

而不是使用$_GET请使用$_SESSION管理计数器。 每次你进入这个页面,你都会增加计数器。

所以,你必须做这个页面的顶部:

if(isset($_SESSION['counter'])) { 
      $page = $_SESSION['counter'] + 1; 
      $offset = $post_limit * $page; 
     } else { 
      $page = 0; 
      $offset = 0; 
      $_SESSION['counter']=0; 
     } 

试试这个。 顺便说一句,有Datatables哪些做你正在寻找。

P.S.如果您没有在代码中的其他位置开始会话,请将session_start();放在第一页(例如,login.php)中,否则请放在此页面上。

+0

刚刚试过这个,没有任何东西看起来改变了。 – Treedot

0

固定。问题在这里:

if ($page > 0) { 
      $last = $page - 2; 
      echo "<a href='$_PHP_SELF?page = $last'>previous page</a> | "; 
      echo "<a href='$_PHP_SELF?page = $page'>next page</a>"; 
     } else if ($page == 0) { 
      echo "<a href='$_PHP_SELF?page = $page'>next page</a>"; 
     } else if ($post_left < $post_limit) { 
      $last = $page - 2; 
      echo "<a href='$_PHP_SELF?page = $last'>previous page</a>"; 
     } 

我连接到的URL不应该在=符号周围有空格。它仍然是越野车,但它的工作原理。

相关问题