简体   繁体   English

PHP通过分页对结果进行排序

[英]PHP sort results with paging

I really need help about sorting and paging. 我真的需要有关排序和分页的帮助。

I read on some other posts that I need session but I don't know how it's working. 我读了一些其他需要会议的帖子,但不知道它是如何工作的。

This is my code for sorting and moving to next and previous page. 这是我的代码,用于排序并移至下一页和上一页。

<?php 

            $sql = "SELECT * FROM movies";

            if (isset($_GET['sort']) == 'year')
            {
                $sql .= " ORDER BY year";
            }
            elseif (isset($_GET['sort']) == 'IMDBrating')
            {
                $sql .= " ORDER BY IMDBrating";
            }
            elseif (isset($_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 ORDER BY year DESC 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);
            }

        ?>

我建议使用像datatables这样的插件来对数据进行排序和过滤。

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

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