没有课程源码,请问怎么练习?

来源:2-1 优秀分页赏析

慕运维2907241

2015-09-15 13:07

没有课程源码,请问怎么练习,可以提供下吗,谢谢

写回答 关注

2回答

  • 魔鬼达人
    2015-09-22 17:23:03

    我写的,你可以参考一下

    <!DOCTYPE html >

    <head>

    <meta http-equiv="content-type" content="text/html;charset=utf-8">

    </head>

    <style >

    body{

    font-size:12px;

    FONT-FAMILY:Microsoft YaHei;

    width:100%;

    }

    div.page{

    text-align:center;

    }

    div.content{

    height:300px;

    }

    div.page a{

    border:#aaaadd 1px solid;

    text-decoration:none;

    padding:2px 5px;

    margin:3px;

    }

    div.page span.current{

    border:#000099 1px solid;

    background-color:#000099;

    padding:2px 6px;

    margin:3px;

    color:#fff;

    font-weight:bold;

    }

    div.page span.disable{

    border:#eee 1px solid;

    padding:2px 5px;

    margin:3px;

    color:#ddd;

    }

    div.page form{

    display:inline;

    }

    </style>

    <body>

    <?php

    //1传入页码

    $page = $_GET['p'];

    $pagesize = 10;

    //2根据页码取出数据

    $host= "localhost";

    $username = "root";

    $password = "";

    $db = "test";

    $showpage = 5;

    $conn = mysql_connect($host,$username,$password);

    if(!$conn){

        echo "fail";

        exit;

    }

    mysql_select_db($db);

    mysql_query("set names utf8");

    $sql = "SELECT * FROM page LIMIT ".($page-1)*$pagesize .",$pagesize";

    $result = mysql_query($sql);

    echo "<div class='content'>";

    echo "<table border=1 cellspacing=0 width=30% align=center>";

    echo "<tr><td>ID</td><td>NAME</td></tr>";

    while($row = mysql_fetch_assoc($result)){

      echo "<tr>";

      echo "<td>{$row['id']}</td>";

      echo "<td>{$row['name']}</td>";

      echo "</tr>";

    }

    echo "</table>";

    echo "</div>";

    mysql_free_result($result);

    $total_sql = "select count(*) from page";

    $total_result = mysql_fetch_array(mysql_query($total_sql));

    $total = $total_result[0];

    $total_pages = ceil($total/$pagesize);


    mysql_close($conn);

    //3显示数据+分页条

    $page_banner = "<div class='page'>";

    $pageoffset = ($showpage-1)/2;

    $start = 1;

    $end = $total_pages;


    if($page > 1){

        $page_banner.= "<a href='".$_SERVER['PHP_SELF']."?p=1'>首页</a>";

        $page_banner.= "<a href='".$_SERVER['PHP_SELF']."?p=".($page-1)."' ><上一页</a>";

    }else{

        $page_banner.="<span class='disable'>首页</span>";

        $page_banner.="<span class='disable'><上一页</span>";

    }


    if($total_pages > $showpage){

        if($page > $pageoffset+1){

            $page_banner.="...";

        }

        if($page > $pageoffset){

            $start = $page - $pageoffset;

            $end = $total_pages > $page+$pageoffset?$page+$pageoffset:$total_pages;

        }else{

            $start = 1;

            $end = $total_pages > $showpage ? $showpage:$total_pages;

        }

        if($page + $pageoffset > $total_pages){

            $start = $start - ($page+$pageoffset-$end);

        }

    }

    for($i=$start;$i<=$end;$i++){

        if($page==$i){

            $page_banner.="<span class='current'>{$i}</span>";

        }else{

        $page_banner.= "<a href='".$_SERVER['PHP_SELF']."?p=".$i."' >$i</a>";

        }

    }


    if($total_pages > $showpage && $total_pages >$page+$pageoffset){

        $page_banner.="...";

    }


    if($page < $total_pages){

        $page_banner.= "<a href='".$_SERVER['PHP_SELF']."?p=".($page+1)."' >下一页></a>";

        $page_banner.= "<a href='".$_SERVER['PHP_SELF']."?p=".($total_pages)."'>尾页</a>";

    }else{

        $page_banner.="<span class='disable'>下一页></span>";

        $page_banner.="<span class='disable'>尾页</span>";

      }

    $page_banner.="共{$total_pages}页";

    $page_banner.="<form action = 'index.php' method='get'>";

    $page_banner.="到第<input type='text' size='2' name='p' value='$page'>页";

    $page_banner.="<input type='submit' value='确定'>";

    $page_banner.="</form></div>";

    echo $page_banner;

    ?>

    </body>

    </html>


  • pardon110
    2015-09-15 13:30:06

    看懂原理,自己开始写。

PHP+MySQL分页原理实现

引导大家完成一个PHP+MySQL分页功能,由简入繁的剖析原理

41180 学习 · 175 问题

查看课程

相似问题