PHP-分頁模式-指定排列

edited 二月 2016 in PHP新手區
我是新手-邊看書邊學
這個範例是我從書上提供的程式進行修改.
我的目標是可以讓使用者點自己喜歡的排列在顯示出來
我設計一個"依價錢排列"按鈕
但是現在卡在我點"依價錢排列"後的當下可以很順利的由高至低排列
可是我換到下一頁後又跳還原本的預設值排列
我試過很多方法如判斷式..等都搞不定
可以麻煩大大幫忙指教
我該怎麼下手
附上原始碼
謝謝
<html>
    <head>
        <meta charset="UTF-8">
        <title>分頁練習</title>
    </head>
    <body>
        <h1 align="center">價目表</h1>
        <?php
        require_once ("connSQL.php");
        $conn = connsql();
        $sql = "SELECT category AS '零組件種類', brand AS '品牌', specification AS '規格', price AS '價格', date AS '報價日期' FROM Price";
        $result = check($conn, "pro", $sql);
        if (mysqli_connect_errno()) {
            echo "NG" . mysqli_connect_errno();
        } else {
            echo "連線成功" . "<hr>";
        }
        $sql1 = "SELECT * FROM `price`";
        $result1 = check($conn, "pro", $sql1);

        if (mysqli_num_rows($result1) >= 1) {
            echo 'DATABASS OK';
        } else {
            echo 'DATABASS NG';
        }
        if (isset($_POST["action"]) && $_POST["action"] = "add") {
            //此為寫入SQL中
            $sql = "SELECT category AS '零組件種類', brand AS '品牌', specification AS '規格', price AS '價格', date AS '報價日期' FROM Price ORDER BY `price` DESC";
            $result1 = check($conn, "pro", $sql);
            $session['AA'] = 0;
            if (mysqli_connect_errno()) {
                echo "NG" . mysqli_connect_errno();
            } else {
                // echo "連線成功" . "<hr>";
            }
            //關閉資料庫
            //mysqli_close($conn);
        }
        //分頁計算
        if (isset($_GET["page"]))
            $page = $_GET["page"];
        else
            $page = 1;
        $records_per_page = 5;
        $total_pages = ceil(mysqli_num_rows($result) / $records_per_page);       //計算本頁第一筆記錄的序號	
        $started_record = $records_per_page * ($page - 1);       //將記錄指標移至本頁第一筆記錄的序號	
        mysqli_data_seek($result, $started_record);
        //顯示標題
        echo "<table border='1' align='center' width='800'>";
        echo "<tr align='center'>";

        for ($i = 0; $i < mysqli_num_fields($result); $i++) //f取欄位數
            echo "<td>" . mysqli_fetch_field_direct($result, $i)->name . "</td>"; //取得欄位資訊

        echo "</tr>";
        //顯示記錄
        $j = 1;


        while ($row = mysqli_fetch_row($result) and $j <= $records_per_page) { //取得紀錄 資訊
            echo "<tr align='center'>";

            for ($i = 0; $i < mysqli_num_fields($result); $i++)
                echo "<td>$row[$i]</td>";
            $j++;
            echo "</tr>";
        }

        echo "</table>";

        //顯示分頁數字
        $URL = "record.php?page=";
        echo "<p align='center'>";
        if ($page > 1)
            echo "<a href='$URL" . ($page - 1) . "'>上一頁</a> ";

        for ($i = 1; $i <= $total_pages; $i++) {
            if ($i == $page)
                echo "$i ";
            else
                echo "<a href='$URL$i'>$i</a> ";
        }

        if ($page < $total_pages)
            echo "<a href='$URL" . ($page + 1) . "'>下一頁</a> ";
        echo "</p>";
        ?>
        <Form method ="POST" align="center" action="<?php $URL ?>" name="myForm">
            <input name="action" type="hidden" value="add">
            <input type="submit" data-inline="true" value="降逆排列">
            <br>
        </form>
    </body>
</html>

評論

  • 你可以考慮把 排序的資料 帶成GET
    或者 $URL給值 得時候,判斷 是否有 add 然後一併帶著
    //顯示分頁數字
    $URL = \"record.php?page=\";
    if( $_GET['action']!='' ){ $URL = \"record.php?action=add&page=\"; }
    ...
    ...
    
    
    記得下面的 from 的 method 要改成 GET
  • edited 二月 2016
    jk60504 wrote: »
    你可以考慮把 排序的資料 帶成GET
    或者 $URL給值 得時候,判斷 是否有 add 然後一併帶著
    //顯示分頁數字
    $URL = \"record.php?page=\";
    if( $_GET['action']!='' ){ $URL = \"record.php?action=add&page=\"; }
    ...
    ...
    
    
    記得下面的 from 的 method 要改成 GET



    大大我改成這樣
     if (isset($_GET["action"]) && $_GET["action"] = "add") {
                //此為寫入SQL中
                $sql = "SELECT category AS '零組件種類', brand AS '品牌', specification AS '規格', price AS '價格'"
                        . ", date AS '報價日期' FROM Price ORDER BY `price` DESC";
                $result = check($conn, "pro", $sql);
                if (mysqli_connect_errno()) {
                    echo "NG" . mysqli_connect_errno();
                } else {
                    //echo "連線成功" . "<hr>";
                }
            } 
    
    ...
    ...
           $result = check($conn, "pro", $sql);
            //顯示分頁數字
            $URL = "record.php?action={$_GET['action']}&page=";
            echo "<p align='center'>";
            if ($page > 1)
                echo "<a href='$URL" . ($page - 1) . "'>上一頁</a> ";
            for ($i = 1; $i <= $total_pages; $i++) {
                if ($i == $page)
                    echo "$i ";
                else
                    echo "<a href='$URL$i'>$i</a> ";
            }
            if ($page < $total_pages)
                echo "<a href='$URL" . ($page + 1) . "'>下一頁</a> ";
            echo "</p>";
            ?>
            <Form method ="GET" align="center" action="<?php $URL ?>" name="myForm">
                <input name="action" type="hidden" value="add">
                <input type="submit" data-inline="true" value="降逆排列">
                <br>
            </form>
    
    

    這樣改的話
    第一次載入出現
    Notice: Undefined index: action in C:\xampp\htdocs\listem\text\record.php on line 73
    我按下一頁後就直接幫我降逆排列了
    變成我設的按鈕是多餘的-.-
    阿......快瘋啦.....


    我原本是想法是網頁第一次載入就用MYSQL預設值排列
    使用者想改變在點按鈕改變 有點像論壇的 最新發表排列 還是最後回覆排列

    看似容易可是就是不知道卡在哪裡
  • Notice 只是提示性警告 可以忽略,如果你在意的話
    就是 先判斷 isset( $_GET )
    有 就如何如何,沒有的話在如何如何
        if ( isset($_GET["action"]) ) {
            
            if( $_GET["action"]=="add" ){
                //此為寫入SQL中
                $sql = "SELECT category AS '零組件種類', brand AS '品牌', specification AS '規格', price AS '價格'"
                    . ", date AS '報價日期' FROM Price ORDER BY `price` DESC";
                $result = check($conn, "pro", $sql);
                if (mysqli_connect_errno()) {
                    echo "NG" . mysqli_connect_errno();
                } else {
                    //echo "連線成功" . "<hr>";
                }
            }
        } 
    

    另外 你判斷 ='add' 打錯了,所以你按下一頁帶入了 $_GET 雖然是空值,
    但是該數值存在,所以會直接變成 降逆排列
  • edited 二月 2016
    jk60504 wrote: »
    Notice 只是提示性警告 可以忽略,如果你在意的話
    就是 先判斷 isset( $_GET )
    有 就如何如何,沒有的話在如何如何
        if ( isset($_GET["action"]) ) {
            
            if( $_GET["action"]=="add" ){
                //此為寫入SQL中
                $sql = "SELECT category AS '零組件種類', brand AS '品牌', specification AS '規格', price AS '價格'"
                    . ", date AS '報價日期' FROM Price ORDER BY `price` DESC";
                $result = check($conn, "pro", $sql);
                if (mysqli_connect_errno()) {
                    echo "NG" . mysqli_connect_errno();
                } else {
                    //echo "連線成功" . "<hr>";
                }
            }
        } 
    

    另外 你判斷 ='add' 打錯了,所以你按下一頁帶入了 $_GET 雖然是空值,
    但是該數值存在,所以會直接變成 降逆排列
    謝謝大大的大力幫忙..我搞定了
    可以隨心所欲的排列了
Sign In or Register to comment.