简体   繁体   中英

Why is my pagination PHP code not working?

I'm working on a piece of code that does pagination for different sorting systems. It works for likes and outputs correctly, but does not work for the sorting system of tag.

 $sorting = $_GET["sorting"];

$per_page = 10;
$pages = $count_total->num_rows;
$total_pages = ceil($pages / $per_page);

if($sorting == "likes") {
      $count_total = $db2->query("SELECT * FROM likes WHERE user='$user'");
    }

if($sorting == "tag") {
     $tag_name = $_GET["tag_name"];
     $count_total = $db2->query("SELECT * FROM movie_tags WHERE tag_id='$tag_name'");
    }

$pages = $count_total->num_rows;
    $total_pages = ceil($pages / $per_page);

 $start = (($page - 1) * $per_page);



for($number=1;$number<=$total_pages;$number++) {
           if($page == $number) {
             echo '<div class="complete_page">'.$number.'</div>';
           } else {
             $sorting = $_GET["sorting"];
            echo '<a href="?page='.$number.'&sorting='.$sorting.'"> <div class="number_page">'.$number.'</div></a>';

           }
         }
         }

This is an example of how I'm using pagination:

$movie = $db2->query("SELECT * FROM movies ORDER BY likes DESC LIMIT $start, $per_page");

NOTE: When I echo $pages , both sorting systems generate values. In fact, tag has a value of 11. Why is this value not creating the pagination system. I know this is not a problem with the pagination system because it is working for the sorting system of likes.

try changing you query to:

$movie = $db2->query("SELECT * 
                       FROM movies 
                       ORDER BY likes DESC 
                       LIMIT $per_page 
                       OFFSET $start");

This should set the amount of lines to $per_page Starting from $start.

You should try to remove the 2 single quotes around $tag_name in your SQL query. I mean:

$count_total = $db2->query("SELECT * FROM movie_tags WHERE tag_id=".$tag_name);

Since your tag value is 11, i believe your database column named tag_id is an integer. And with those 2 single quotes, you are comparing it to a string/varchar '11' .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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