`自己周末做了一个在线拍卖行,虽然没有很好看的界面,但是上架商品,竞价,展示商品,展示热门商品等已经都有了,自己现在把里面用到的东西一点点写出来为以后复习用。
      首先就是一个php的分页显示的类:

<!--myDataGridClass.php:数据分页显示类------------------------------------>
<?php
/********************************************* 
类名: myDataGridClass
功能:分页显示MySQL数据库中的数据 
***********************************************/ 
class myDataGridClass{ 
    //属性
    var $sql;                          //所要显示数据的SQL查询语句 
    var $max_line;             //每页显示最多行数 

    var $begin_record;         //所要显示记录的首行序号
    var $total_records;            //记录总数 
    var $current_records;      //本页读取的记录数 
    var $result;                       //读出的结果 

    var $total_pages;          //总页数  
    var $current_page;         //当前页数

    var $arr_page_query;       //数组,包含分页显示需要传递的参数 

    //方法
/********************************************* 
构造函数:__construct()
输入参数:           
        $pmax_line:每页显示最多行数    
***********************************************/ 
function myDataGridClass($pmax_line)
{ 
    $this->max_line=$pmax_line; 
    $this->begin_record=0;
} 

/********************************************* 
构造函数:__destruct()
输入参数:           
***********************************************/ 
function __destruct()
{

}

/********************************************* 
get函数:__get()
***********************************************/ 
function __get($property_name)
{  
     if(isset($this->$property_name)) 
     { 
            return($this->$property_name); 
     } 
     else 
     { 
            return(NULL); 
     } 
}

/********************************************* 
set函数:__set()
***********************************************/ 
function __set($property_name, $value) 
{ 
     $this->$property_name = $value; 
} 

/********************************************* 
函数名:read_data
功能: 根据SQL查询语句从表中读取相应的记录
返回值:属性二维数组result[记录号][字段名]
***********************************************/ 
function read_data()
{ 
    $psql=$this->sql;
    if(isset($_GET['begin_record']))
    $this->begin_record=$_GET["begin_record"];
    else
        $this->begin_record=0;
    //查询数据,数据库链接等信息应在类调用的外部实现
    $result=mysql_query($psql) or die(mysql_error()); 
    $this->total_records=mysql_num_rows($result); 

    //利用LIMIT关键字获取本页所要显示的记录
    if($this->total_records>0) 
    {
        $psql=$psql.  " LIMIT ".$this->begin_record." , ".$this->max_line; 

        $result=mysql_query($psql) or die(mysql_error()); 
        $this->current_records=mysql_num_rows($result); 

        //将查询结果放在result数组中
        $i=0; 
        while($row=mysql_fetch_Array($result))
        { 
            $this->result[$i]=$row; 
            $i++; 
        } 
    }
}


/********************************************* 
函数名:navigate()
功能: 显示首页、下页、上页、未页
***********************************************/ 
function navigate() 
{
    //获取总页数、当前页信息
    $this->total_pages=ceil($this->total_records/$this->max_line); 
    $this->current_page=$this->begin_record/$this->max_line+1; 
    echo "<div align=center>";
    echo "<font color = red size ='4'>第".$this->current_page."页/共".$this->total_pages."页</font>"; 
    echo "    ";

    //获取将要导航到的分页的初始记录号
    $first=0; 
    $next=$this->begin_record+$this->max_line; 
    $prev=$this->begin_record-$this->max_line; 
    $last=($this->total_pages-1)*$this->max_line; 

    //生成导航链接
    if($this->begin_record>=$this->max_line) 
        echo "<A href=".$_SERVER['PHP_SELF']."?begin_record=".$first.">首页</A>|"; 
    if($prev>=0) 
        echo "<A href=".$_SERVER['PHP_SELF']."?begin_record=".$prev.">上一页</A>|"; 
    if($next<$this->total_records) 
        echo "<A href=".$_SERVER['PHP_SELF']."?begin_record=".$next.">下一页</A>|"; 
    if($this->total_pages!=0 && $this->current_page<$this->total_pages) 
        echo "<A href=".$_SERVER['PHP_SELF']."?begin_record=".$last.">末页</A>";       
    echo "</div>";
} 

} 
?>

当然还可能有些小问题,但是里面自己加上去的是这几行,实际上感觉还蛮关键的,php一般的话不用这种方式好像变量并不能直接传到到新的界面上去:

 if(isset($_GET['begin_record']))
    $this->begin_record=$_GET["begin_record"];
    else
        $this->begin_record=0;

display 函数就是调用这个类:

<!--display_goods.php:显示商品页面---------------------------->
<?php 
//包含分页显示类
include "myDataGridClass.php";
//global $view;

/******************************************
函数名:    display_goods()
功能:     显示所有商品
******************************************/
function display_goods(){ 
    //全局分页显示对象
    global $view;
    $DBHOST="localhost";
    $DBUSER="root";
    $DBPWD="";
    $DBNAME="auction";
    //连接数据库
    //include "sys_conf.inc";
    $link_id=mysql_connect($DBHOST,$DBUSER,$DBPWD);            
    mysql_select_db($DBNAME); 

    //只显示没有结束竞标的商品 
    $time=time(); 
    $view->__set("sql","select * from goods where endtime>'$time' order by gid desc");     
    //读数据 
    $view->read_data();

    //如果数据为空,则返回
    if($view->current_records==0) 
    {
        echo "<tr><td colbegin_record=4> </td></tr>"; 
        return;
    }        

    //数据不为空,显示数据
    echo "<table width='80%' border='0' align='center'>";
    echo "<tr bgcolor='green'>"; 
    echo "<td>商品名</td>"; 
    echo "<td>结束时间</td>"; 
    echo "<td>当前最高价格</td>"; 
    echo "<td>出价人数</td>";
    echo "</tr>"; 
    for($i=0;$i<$view->current_records;$i++)
    { 
        if(ceil($i/2)*2==$i) 
            $bgc="white"; 
        else 
            $bgc="yellow"; 

        echo "<tr bgcolor=$bgc><td>"; 
        echo "<a href=detail.php?gid=".$view->result[$i]["gid"].">".$view->result[$i]["name"].$view->result[$i]["gid"]."</a>";
        echo "</td><td>"; 
        echo $view->result[$i]["endtime"]; 
        echo "</td><td>¥"; 
        echo $view->result[$i]["current_price"]; 
        echo "</td><td>"; 
        echo $view->result[$i]["reply_num"]; 
        echo "</td></tr>"; 
    } 
    echo "</table>";

    //关闭数据库
    mysql_close($link_id);
} 

//*********显示最热的10条记录********** 
function displsy_top10()
{ 
    //全局分页显示对象
    global $view;
    $DBHOST="localhost";
    $DBUSER="root";
    $DBPWD="";
    $DBNAME="auction";
    //每页显示记录数
    $PAGE_MAX_LINE=5;
    //连接数据库
    //include "sys_conf.inc";
    $link_id=mysql_connect($DBHOST,$DBUSER,$DBPWD);          
    mysql_select_db($DBNAME); 

    //显示出价者最多的10个商品
    $view->__set("sql","select *  from goods order by reply_num desc");    
    //读数据 
    $view->read_data();

    //如果数据为空,则返回
    if($view->current_records==0) 
    {
        echo "<tr><td colbegin_record=4> </td></tr>"; 
        return;
    }        

    //数据不为空,显示数据
    echo "<table width='80%' border='0' align='center'>";
    echo "<tr bgcolor='green'>"; 
    echo "<td>商品名</td>"; 
    echo "<td>结束时间</td>"; 
    echo "<td>当前最高价格</td>"; 
    echo "<td>出价人数</td>";
    echo "</tr>"; 
    for($i=0;$i<$view->current_records;$i++)
    { 
        if(ceil($i/2)*2==$i) 
            $bgc="white"; 
        else 
            $bgc="yellow"; 

        echo "<tr bgcolor=$bgc><td>"; 
        echo "<a href=detail.php?gid=".$view->result[$i]["gid"].">".$view->result[$i]["name"]."</a>"; 
        echo "</td><td>"; 
        echo $view->result[$i]["endtime"]; 
        echo "</td><td>¥"; 
        echo $view->result[$i]["current_price"]; 
        echo "</td><td>"; 
        echo $view->result[$i]["reply_num"]; 
        echo "</td></tr>"; 
    } 
    echo "</table>";

    //关闭数据库
    mysql_close($link_id); 
} 

//*********显示最新10条记录*********** 
function displsy_latest10()
{ 
    //全局分页显示对象
    global $view;
    $DBHOST="localhost";
    $DBUSER="root";
    $DBPWD="";
   $DBNAME="auction";
    //$PAGE_MAX_LINE=5;

    //连接数据库
    //require_once("sys_conf.inc");         //系统配置文件,包含数据库配置信息
    $link_id=mysql_connect($DBHOST,$DBUSER,$DBPWD);          
    mysql_select_db($DBNAME); 

    //显示最新的10个商品
    $view->__set("sql","select * from goods order by gid desc");   
    //读数据 
    $view->read_data();

    //如果数据为空,则返回
    if($view->current_records==0) 
    {
        echo "<tr><td colbegin_record=4> </td></tr>"; 
        return;
    }        

    //数据不为空,显示数据
    echo "<table width='80%' border='0' align='center'>";
    echo "<tr bgcolor='green'>"; 
    echo "<td>商品名</td>"; 
    echo "<td>结束时间</td>"; 
    echo "<td>当前最高价格</td>"; 
    echo "<td>出价人数</td>";
    echo "</tr>"; 
    for($i=0;$i<$view->current_records;$i++)
    { 
        if(ceil($i/2)*2==$i) 
            $bgc="white"; 
        else 
            $bgc="yellow"; 

        echo "<tr bgcolor=$bgc><td>"; 
        echo "<a href=detail.php?gid=".$view->result[$i]["gid"].">".$view->result[$i]["name"]."</a>";
        echo "</td><td>"; 
        echo $view->result[$i]["endtime"]; 
        echo "</td><td>¥"; 
        echo $view->result[$i]["current_price"]; 
        echo "</td><td>"; 
        echo $view->result[$i]["reply_num"]; 
        echo "</td></tr>"; 
    } 
    echo "</table>";

    //关闭数据库
    mysql_close($link_id); 
} 

    //主程序
    include("head.html");
    //include "sys_conf.inc";
    //实例化myDataGridClass对象
$PAGE_MAX_LINE=5;

$view=new myDataGridClass($PAGE_MAX_LINE);

    //初始化所要显示的具体记录
    if(isset($begin_record))
        $view->__set("begin_record",$begin_record);
    else
        $view->__set("begin_record",0);

    //显示商品列表
    if(!isset($task) || $task=="")
    {
        display_goods();
        $view->navigate();
    }
    else if($_GET['task']=="topten")
    {
        displsy_top10();
    }
    else if($_GET['task']=="latestten")
    {
        displsy_latest10();     
    }   
?> 

当然还有head头部:

<br>
<div align=center>
<a href=login.php>|登录|</a>
<a href=add_goods.php>|添加商品|</a>
<a href=display_goods.php>|商品列表|</a>
<a href=display_goods.php?task=topten>|HOT商品|</a>
<a href=display_goods.php?task=latestten>|最新商品|</a>
<a href=about:blank>|退出系统|</a>
</div>
<br>

当然注意include那个地方,自己实际上不知道为什么include的不管用(下面还是会说变量未定义),然后自己只能都重新写一遍了…希望有热心的网友可以帮忙解答一下~

posted on 2015-04-05 08:22  虽然如此  阅读(264)  评论(0编辑  收藏  举报