smarty+php+ajax 簡單無重新整理分頁

來源:互聯網
上載者:User

標籤:無重新整理分頁

    簡介

    分頁,無非就是從資料庫中獲得我們想查詢的資料,再加以處理即可!

    ① 確定資料總數($count)

    ② 每頁顯示資料條數($pageSize)

    ③ 分多少頁($pageCount)

    ④ 上一頁($pagePrev)

    ⑤ 下一頁($pageNext)

    ⑥ 判斷越界問題

    ⑦ 位移量($offset)

    ⑧ sql語句($sql = "select * from goods limit $offset,$pageSize";)

    簡單歸簡單,我們還得考慮實際的應用。例如:如果你正在馬鈴薯網看《火影忍者》,下面一個評論吸引了你,你點擊“下一頁”後整個頁面都重新整理,我勒個去,《火影忍者》也刷沒了,只能再從頭開始看,這樣的情況是不是令你十分厭惡。再想想,如果當你點擊“下一頁”時,只有評論的部分重新整理,你的視頻根本沒有受到影響,那是不是很完美呢!

    想要無重新整理,第一個想到Ajax;前台都是HTML+JS,後台php+smarty組合,那我們就直接進入主題:

    

    檔案結構

    650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/4C/47/wKioL1Q6h9yRGlm9AABJFHoHYjg697.jpg" title="檔案結構.png" alt="wKioL1Q6h9yRGlm9AABJFHoHYjg697.jpg" />

    ① Smarty模板檔案 官網地址:http://www.smarty.net/

    ② /templates     自訂的檔案夾  page.htpl模板檔案,用於存放分頁資料及連結

    ③ page.html      前台顯示頁面   js觸發

    ④ page.php       資料庫處理   smarty資料處理

    ⑤ public.js      Ajax對象的封裝

    

    程式流程

    ① 前端page.html傳遞page參數(不傳遞後台頁面也會預設賦值)

        (前端主要的作用:顯示、發出Ajax請求)

    ② php頁面資料庫操作,$sql分頁語句查詢出分頁所需的資料

    ③ 載入smarty模板,將參數assign傳遞到page.htpl模板頁

    ④ 模板頁匯入資料、遍曆資料  將上、下頁加入超連結及js事件

    ⑤ smarty fetch()方法讀模數板頁資料 賦值給變數  變數再響應給Ajax用戶端

    ⑥ 前台接收


    代碼

    page.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"         "http://www.w3.org/TR/html4/loose.dtd"><html>    <head>        <meta http-equiv="Content-Type" content="text/html; charset=gb2312">        <title>無重新整理分頁</title>        <script language="javascript" src="public.js"></script>        <script language="javascript">        function display(page){            $.get(‘page.php‘,‘page=‘+page,function(msg){                $(‘content‘).innerHTML = msg;            });        }        window.onload = function(){            display(1);        };        </script>    </head>        <body>        <div id="content"></div>    </body></html>

    public.js

(function(){    //1、用於得到一個DOM元素    //定義了一個$函數 範圍有局部    var $ = function(id){        return document.getElementById(id);    };        //2、用於得到一個Ajax對象    //將$看作函數對象,init為屬性,值為函數體    $.init = function(){        try{return new XMLHttpRequest()}catch(e){}         try{return new ActiveXObject(‘Microsoft.XMLHTTP‘)}catch(e){}        alert(‘請更改新瀏覽器!‘);    };    //用於發送Ajax get請求
    $.get = function(url,data,callback,type){        var xhr = $.init();        if (data != null){//傳遞參數、只發出請求            url = url+‘?‘+data;        }        xhr.open(‘get‘,url);        xhr.setRequestHeader(‘If-Modified-Since‘,‘0‘);//解決get緩衝問題        xhr.onreadystatechange = function(){            if (xhr.readyState == 4 && xhr.status == 200){                //當沒有指定傳實值型別時,預設為字串                if (type == null){                    type = ‘text‘;                }                //判斷語句指定三種接收形式                if (type == ‘text‘){                    callback(xhr.responseText);                }                if (type == ‘xml‘){                    callback(xhr.responseXML);                }                if (type == ‘json‘){                    callback(eval("("+xhr.responseText+")"));                }            }    };    xhr.send(null);
    };        //增大其範圍全域變數  window方法的$屬性  賦值為$  閉包寫法        window.$ = $;})();

    page.php

<?php    mysql_connect(‘localhost‘,‘root‘,‘111111‘);    mysql_select_db(‘shop‘);    mysql_query(‘set names gb2312‘);    //為查詢結果增加新欄位 num    $sql = "select * from sw_goods";    $result = mysql_query($sql);    $count = mysql_num_rows($result);//獲得總行數,與mysql_num_rows()類似    $page = isset($_GET[‘page‘])?$_GET[‘page‘]:1;//擷取當前頁碼,預設1    $pageSize = 5;//頁尺寸  每頁顯示多少條資料    $pageCount = ceil($count/$pageSize);//計算總頁面    $pagePrev = $page - 1;//上一頁頁碼    $pageNext = $page + 1;//下一頁頁碼    if ($pagePrev < 1) $pagePrev = 1;  //判斷頁碼越界    if ($pageNext > $pageCount) $pageNext = $pageCount;    if ($page < 1) $page = 1;   //判斷當前頁頁碼越界    if ($page > $pageCount) $page = $pageCount;
    $offset = ($page -1)*$pageSize; //位移量        //相對於當前頁來講的    $sql = "select * from sw_goods limit $offset,$pageSize"; //order by id asc 預設/desc    $result = mysql_query($sql);//查詢那一頁的結果集    $num = mysql_num_rows($result);    $data = array();    for ($i=0;$i<$num;$i++){    //遍曆五次,每次獲得一個數組array(‘good_id‘=>‘‘,‘goods_name‘=>‘‘,‘goods_price‘=>‘‘)    //形成一個二維數組    $data[] = mysql_fetch_assoc($result);    }    mysql_close();    //***************************************************************************    include(‘Smarty/Smarty.class.php‘);    $smarty = new Smarty();    $smarty -> assign(‘data‘,$data);    $smarty -> assign(‘count‘,$count);    $smarty -> assign(‘pageCount‘,$pageCount);    $smarty -> assign(‘page‘,$page);    $smarty -> assign(‘pagePrev‘,$pagePrev);    $smarty -> assign(‘pageNext‘,$pageNext);    $smarty -> assign(‘pageCount‘,$pageCount);    $str = $smarty -> fetch(‘page.htpl‘);//擷取模板裡面的資料,賦值給變數,再傳遞給Ajax
    對象    header("content-type:text/html;charset=gb2312");    echo $str;

    page.htpl

<!--htpl是當作模板來用的副檔名--><!--分頁模板--> <style type="text/css">        *{            margin:0px;            padding:0px;        }        body{            text-align:center;        }        table{            width:650px;            margin:0px auto;            margin-top:20px;        }        tr{            background-color:#ffffff;            height:30px;            font-size:12px;        }
</style><table cellspacing="1" cellpadding="4" bgcolor="#336699">    <tr>        <td>序號</td>        <td>商品名稱</td>        <td>商品價格</td>        <td>商品數量</td>    </tr>    {foreach from = $data item = ‘value‘}    <tr>        <td>{counter}</td>        <td>{$value[‘goods_name‘]}</td>        <td>{$value[‘goods_price‘]}</td>        <td>{$value[‘goods_number‘]}</td>    </tr>    {/foreach}
    <tr>            <td colspan = ‘4‘>        共{$count}條資料        共{$pageCount}頁        當前第{$page}頁        <a href="#" onclick="display(1)">首頁</a>        <a href="#" onclick="display({$pagePrev})">上一頁</a>        <a href="#" onclick="display({$pageNext})">下一頁</a>        <a href="#" onclick="display({$pageCount})">末頁</a>        </td>    </tr></table>

    最後的分頁效果:

    650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/4C/46/wKiom1Q6jaCD0Jh3AAEoKd5TsRE696.jpg" title="分頁圖.png" alt="wKiom1Q6jaCD0Jh3AAEoKd5TsRE696.jpg" />

本文出自 “做一隻蝸牛真好” 部落格,請務必保留此出處http://smili.blog.51cto.com/8919945/1563049

smarty+php+ajax 簡單無重新整理分頁

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.