PHP / MySql结合排序和分页

时间:2022-11-03 21:18:07

I have some issues in understanding how to combine sort and paging, when I go to another page it's not sorted like first one.
First part of code is sorting and another part is paging!
This is my code:

我在理解如何组合排序和分页时遇到一些问题,当我转到另一个页面时,它没有像第一个那样排序。代码的第一部分是排序,另一部分是分页!这是我的代码:

<?php 
        $sql = '';

            if(isset($_GET['sort'])){
                if ($_GET['sort'] == 'year')
                {
                    $sql = " ORDER BY year";
                }
                elseif ($_GET['sort'] == 'IMDBrating')
                {
                    $sql = " ORDER BY IMDBrating";
                }
                elseif ($_GET['sort'] == 'userrating')
                {
                    $sql = " ORDER BY userrating";
                } 
            }

        ?>

            <th><a href="http://localhost/New folder (3)/index.php?sort=year">Year</a></th>
            <th><a href="http://localhost/New folder (3)/index.php?sort=IMDBrating">IMDB rating</a></th>
            <th><a href="http://localhost/New folder (3)/index.php?sort=userrating">user rating</a></th>

        <?php
            $pagesize = 5;

            $recordstart = (int)(isset($_GET['recordstart'])) ? $_GET['recordstart'] : 0;

            $sql01 = "SELECT * FROM movies ".$sql." LIMIT $recordstart, $pagesize";
            $records=mysql_query($sql01);

            $result = mysql_query("SELECT count(id) FROM movies");
            $totalrows = mysql_fetch_row($result);

            while ($movies=mysql_fetch_array($records)){
                echo '<div class="movie_box"><p><div class="news_img"><div class="cover"><img src="'.$movies['cover'].'" width = "183px" height = "271px"/></div><br><button class="trailer_button" type="button">Trailer</button></div><strong><p class="h3"><div class="content">'.$movies['name'].'</p></strong>'.$movies['plot'].'<br><br><strong>Žanr</strong>:'.$movies['genre'].'<br><strong>IMDB ocjena</strong>:'.$movies['IMDBrating'].'<br><strong>Director</strong>:'.$movies['director'].'<br><strong>Glumci</strong>:'.$movies['Starring'].'<br><strong>Ocjena korisnika</strong>:</div><br><div class="trailer">'.$movies['trailer'].'</div><div class="dark"></div></p></div>';
            }

            if ($recordstart > 0){
                $prev = $recordstart - $pagesize;
                $url = $_SERVER['PHP_SELF'].'?recordstart='.$prev;
                printf('<a id="prev" href="%s"><</a>',$url);
            }

            if ($totalrows > ($recordstart + $pagesize)){
                $next = $recordstart + $pagesize;
                $url = $_SERVER['PHP_SELF'].'?recordstart='.$next;
                printf('<a id="next" href="%s">></a>',$url);
            }

        ?> 

1 个解决方案

#1


your paging links must contain all the $_GET parameters you already using. so...

您的分页链接必须包含您已使用的所有$ _GET参数。所以...

$url = $_SERVER['PHP_SELF'].'?recordstart='.$prev;

$ url = $ _SERVER ['PHP_SELF']。'?recordstart ='。$ prev;

should change to

应该改为

$url = $_SERVER['PHP_SELF'].'?recordstart='.$prev.'&sort='.$_GET['sort'];

$ url = $ _SERVER ['PHP_SELF']。'?recordstart ='。$ prev。'&sort ='。$ _ GET ['sort'];

and

$url = $_SERVER['PHP_SELF'].'?recordstart='.$next;

$ url = $ _SERVER ['PHP_SELF']。'?recordstart ='。$ next;

should change to

应该改为

$url = $_SERVER['PHP_SELF'].'?recordstart='.$next.'&sort='.$_GET['sort'];

$ url = $ _SERVER ['PHP_SELF']。'?recordstart ='。$ next。'&sort ='。$ _ GET ['sort'];

#1


your paging links must contain all the $_GET parameters you already using. so...

您的分页链接必须包含您已使用的所有$ _GET参数。所以...

$url = $_SERVER['PHP_SELF'].'?recordstart='.$prev;

$ url = $ _SERVER ['PHP_SELF']。'?recordstart ='。$ prev;

should change to

应该改为

$url = $_SERVER['PHP_SELF'].'?recordstart='.$prev.'&sort='.$_GET['sort'];

$ url = $ _SERVER ['PHP_SELF']。'?recordstart ='。$ prev。'&sort ='。$ _ GET ['sort'];

and

$url = $_SERVER['PHP_SELF'].'?recordstart='.$next;

$ url = $ _SERVER ['PHP_SELF']。'?recordstart ='。$ next;

should change to

应该改为

$url = $_SERVER['PHP_SELF'].'?recordstart='.$next.'&sort='.$_GET['sort'];

$ url = $ _SERVER ['PHP_SELF']。'?recordstart ='。$ next。'&sort ='。$ _ GET ['sort'];