使用分页时如何保持排序顺序?

时间:2015-10-20 13:01:02

标签: javascript php mysqli

我点击下一页时无法确定如何保持排序顺序。在该页面上,产品没有按选定的选项排序,这就是问题所在。当点击选项Naziv时,只有首页产品由Naziv排序,当进入下一页时,产品不会被Naziv排序,它们应该是。

我试过: page=$next&order=$order。但这不能正常工作。

有没有人有一些建议如何解决这个问题。



<html>
<body>
<div>
<div id="container">
    <div>         
        <select name="filter" onchange="window.location.href = 'laptop?order=' + this.value">
            <option>Poredaj po: </option>
            <option value="naziv">Naziv</option>
            <option value="cijenaasc">Cijena Manja-Veća</option>
            <option value="cijenadesc">Cijena Veća-Manja</option>
         </select>

        <form id="Forma" method='post' action="usporedi">
             <?php
                 // 5. record shown amount
                 $per_page = 10;
                 // 7. current page
                 if (!isset($_GET['page'])) {
                      $page = 1;
                 } else {
                      $page = $_GET['page'];
                 }

                 // 4. mysql start possition
                 if ($page <= 1) {
                     $start = 0;
                 } else {
                     $start = $page * $per_page - $per_page;
                 }

                 // 2. main sql query
                 $query = "SELECT *, FORMAT(cijena,2,'de_DE') as cijena FROM artikli WHERE kategorija='laptop'";
                 $order = isset($_GET['order']) ? $_GET['order'] : '';
                 if ($order) {
                     switch ($order) {
                         case 'cijenaasc':
                             $order = 'cijena';
                             $query = "SELECT *, FORMAT(cijena,2,'de_DE') as cijena FROM artikli WHERE kategorija='laptop' ORDER BY CAST(cijena AS DECIMAL(8,2)) ASC ";
                             break;

                         case 'naziv':
                             $order = 'naziv';
                             $query = "SELECT *, FORMAT(cijena,2,'de_DE') as cijena FROM artikli WHERE kategorija='laptop' ORDER BY naziv ASC ";
                             break;

                         case 'cijenadesc':
                             $order = 'cijena';
                             $query = "SELECT *, FORMAT(cijena,2,'de_DE') as cijena FROM artikli WHERE kategorija='laptop' ORDER BY CAST(cijena AS DECIMAL(8,2)) DESC ";
                             break;
                         }
                    }

                    // 7. How much records are in database
                    $num_rows = mysqli_num_rows(mysqli_query($con, $query));

                    // 7. How much pages are at all
                    $num_pages = ceil($num_rows / $per_page);

                    // 6. Appends limit for shown records
                    $query .= " LIMIT $start, $per_page";

                    // 3. Show all records
                    $result = mysqli_query($con, $query);
                    while ($row = mysqli_fetch_array($result)) {
                        $id = $row ['id'];

                        print
                                "<div id='proizvod'></br><a style='text-decoration:none; color:black;' class='two' href='proizvod.php?id=$id' >" . $row["naziv"] . "" .
                                "<p><img src=" . $row["slika"] . " width='200px' height='200px' style='border-radius: 15px;'></p>" .
                                "<p style='font-size:20px'><b> Cijena za gotovinu: " . $row["cijena"] . " KN </b></p>" .
                                "<pre id='pre1'>" . $row["opis"] . "</pre>" .
                                "</a></div>";
                    }
                    ?>
        </form>
    </div>     
    <br/>
    <div id="pagination">
        <?php
            // 8. Prev numbers, next links
            $prev = $page - 1;
            $next = $page + 1;

            // prev
            if ($prev > 0) {
                echo "<a style='text-decoration:none; color: blue;' href='?page=$prev&order=$order'><b><</b></a> ";
            }
            //numbers
            $number = 1;
            for ($number; $number <= $num_pages; $number +=1) {
                if ($page == $number) {
                    echo " <b> $number </b> ";
                } else {
                    echo " <a style='text-decoration:none; color: blue;' href='?page=$number&order=$order'>$number</a> ";
                }
            }

            // next
            if ($page < ceil($num_rows / $per_page)) {
                echo " <a  style='text-decoration:none; color: blue;'  href='?page=$next&order=$order'><b>></b></a> ";
            }
        ?>
    </div>
</div>
</body>
</html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

在分页页变量

时保持排序顺序

$ order = isset($ _ GET ['order'])? $ _GET ['order']:'';

必须在“div id ='pagination'”中。

 <html>
        <body>

            <div id="container">

                <?php

                $per_page = 10;

                if (!isset($_GET['page'])) {
                    $page = 1;
                } else {
                    $page = $_GET['page'];
                }


                if ($page <= 1) {
                    $start = 0;
                } else {
                    $start = $page * $per_page - $per_page;
                }

                $query = "SELECT *, FORMAT(cijena,2,'de_DE') as cijena FROM artikli WHERE kategorija='smartphone' ORDER BY id ASC ";
                // 7. How much records are in database
                $num_rows = mysqli_num_rows(mysqli_query($con, $query));

                // 7. How much pages are at all
                $num_pages = ceil($num_rows / $per_page);
                ?>

                <div id="gumb"> 
                    <div id="usporedivanje">
                        <input type='submit' form='Forma' name='usporedi' disabled='disabled' id='usporedi' value='Usporedi' onmouseover="" style="cursor: pointer;" />

                    <div id='sortiranje'>
                        <select id='filter' style="font-size:20px; height:50px; border: 5px solid #099CDB; border-radius: 10px; border-color: #099bcd;" name="filter" onchange="window.location.href = 'smartphone?order=' + this.value">
                            <option>Poredaj po: </option>
                            <option value="naziv">Naziv</option>
                            <option value="cijenaasc">Cijena Manja-Veća</option>
                            <option value="cijenadesc">Cijena Veća-Manja</option>
                        </select>
                    </div>
                    </div>

                    <br/>

                    <form id="Forma" method='post'>
                        <?php

                        $order = isset($_GET['order']) ? $_GET['order'] : '';
                        if ($order) {
                            switch ($order) {
                                case 'cijenaasc':
                                    $order = 'cijena';
                                    $query = "SELECT *, FORMAT(cijena,2,'de_DE') as cijena FROM artikli WHERE kategorija='smartphone' ORDER BY CAST(cijena AS DECIMAL(8,2)) ASC ";
                                    break;

                                case 'naziv':
                                    $order = 'naziv';
                                    $query = "SELECT *, FORMAT(cijena,2,'de_DE') as cijena FROM artikli WHERE kategorija='smartphone' ORDER BY naziv ASC ";
                                    break;

                                case 'cijenadesc':
                                    $order = 'cijena';
                                    $query = "SELECT *, FORMAT(cijena,2,'de_DE') as cijena FROM artikli WHERE kategorija='smartphone' ORDER BY CAST(cijena AS DECIMAL(8,2)) DESC ";
                                    break;
                            }
                        }

                        $query .= " LIMIT $start, $per_page";

                        $result = mysqli_query($con, $query);
                        while ($row = mysqli_fetch_array($result)) {

                            $id = $row ['id'];

                            print
                                    "<div id='proizvod'></br><a style='text-decoration:none; color:black; font-size:20px;' class='two' href='proizvod.php?id=$id' ><b>" . $row["naziv"] . "</b>" .
                                    "<p><img src=" . $row["slika"] . " width='200px' height='200px' style='border-radius: 15px;'></p>" .
                                    "<p style='font-size:20px'><b> Cijena za gotovinu: " . $row["cijena"] . " KN </b></p>" .
                                    "<pre id='pre1'>" . $row["opis"] . "</pre>" .
                                    "</a></div>";
                        }
                        ?>
                    </form>

                <div id="pagination">
                    <?php

                    $prev = $page - 1;
                    $next = $page + 1;

                    $order = isset($_GET['order']) ? $_GET['order'] : '';


                    if ($prev > 0) {
                        echo "<a style='text-decoration:none; font-size:30px; color: #099BCD;' href='?page=$prev&order='$order'><b><</b></a> ";

                    }

                    $number = 1;
                    for ($number; $number <= $num_pages; $number +=1) {
                        if ($page == $number) {
                            echo " <b style='font-size:30px; color: #099BCD;'> $number </b> ";
                        } else {
                            echo " <a style='text-decoration:none; font-size:30px; color: #099BCD;' href='?page=$number&order=$order'>$number</a> ";
                        }
                    }

                    if ($page < ceil($num_rows / $per_page)) {
                        echo " <a  style='text-decoration:none; font-size:30px; color: #099BCD;'  href='?page=$next&order=$order'><b>></b></a> ";
                    }
                    ?>

                </div>

            </div>

    <?php include "footer.php"; ?>
        </body>
    </html>