在线竞拍系统的PHP实现框架(一)

前面我给了一个分页显示mysql记录的类,却没给出使用的例子,现在,我整理了我刚写的一个在线竞拍系统框架程序,来说明这个类的使用方法,而且也就在线竞拍的实现方法与大家一起来讨论一下。

首先声明,我不是高手,也不是行家,只是一个fans,所以这个程序肯定有不少漏洞,但我之所以敢拿出来,是因为我很希望能自由地与大家分享PHP带给我们的快乐。(其实是想多加点分好弄个支持mysql的空间^_^)


我觉得竞拍系统与一般的供求信息发布系统相比,最大的不同有两点,一点是出价者开的新价要及时地反映在商品的价格上,另一点是有时间的限制,在竞标结束后,就要停止出价。并且给出最后中标者。

其它的我还没想到呢,有行家给点介绍吧。

所以,我想把一个供求信息发布系统做成一个竞拍系统应是不困难的事吧。

下面先把新版的TViewPage类和数据库结构给出来吧。



本类没有提供连接数据库的功能,所以需在外部打开相应的数据库。
本类也没有提供显示记录的功能,只是分页读取记录至 Result二维数组中。
需在外部自定义数据显示格式。
***********************************************/
class TViewPage {

 var 
$Table;    //表名
 
var $MaxLine;    //每页显示行数
 
 
var $Offset;    //记录偏移量
 
var $Total;    //记录总数
 
var $Number;    //本页读取的记录数
 
var $Result;    //读出的结果
 
 
var $TPages;    //总页数
 
var $CPages;    //当前页数
 
 
var $Condition;    //显示条件 如:where id='$id' order by id desc
 
var $PageQuery;    //分页显示要传递的参数
 
 
 //******构造函数*************
 //参数:表名、最大行数、偏移量
 
 
function TViewPage($TB,$ML){
  global 
$offset;
  
  
$this->Table=$TB;
  
$this->MaxLine=$ML;
  if(isset(
$offset)) $this->Offset=$offset;
  else 
$this->Offset=0;
  
$this->Condition="";
 }
 
//********设置显示条件*********
//如:where id='$id' order by id desc 
//要求是字串,符合SQL语法(本字串将加在SQL语句后)
 
 
function SetCondition($s){
  
$this->Condition=$s;
 }

//******设置传递参数************
// key参数名 value参数值
// 如:setpagequery("id",$id);如有多个参数要传递,可多次调用本函数。
 
 
function SetPageQuery($key,$value){
  
$tmp[key]=$key$tmp[value]=$value;
  
$this->PageQuery[]=$tmp;
 }
 
 
//********读取记录***************
 // 主要工作函数,根据所给的条件从表中读取相应的记录
 // 返回值是一个二维数组,Result[记录号][字段名]
 
 
function ReadList() {
   
$SQL="SELECT Count(*) AS total FROM ".$this->Table." ".$this->Condition;
   
   
$result=mysql_query($SQL) or die(mysql_error());
   
$row=mysql_fetch_Array($result);
   
$this->Total=$row[total];
   
   if(
$this->Total>0) {            //根据条件 Condition
    
$SQL="SELECT *  FROM ".$this->Table." ".$this->Condition.
         
" LIMIT ".$this->Offset." , ".$this->MaxLine;

    
$result=mysql_query($SQL) or die(mysql_error());
    
$this->Number=mysql_num_rows($result);
    
    
$i=0;
    while(
$row=mysql_fetch_Array($result)){
      
$this->Result[$i]=$row;
      
$i++;
    }
   }
   return 
$this->Result;
 }
 
 
//*******加入新记录**********
 //$str为加入的值,如 "'$id','$name','$class'"等
 
 
function Add($str){
 
  
$SQL="INSERT INTO ".$this->Table." VALUES(".$str.")";
  
mysql_query($SQL) or die(mysql_error());
 
 }
 
 
//*********删除记录**********
 //先调用SetCondition()来确定条件。
 
 
function Delete(){
  
$SQL="DELETE FROM ".$this->Table." ".$this->Condition;
  
mysql_query($SQL) or die(mysql_error());
 }
 
 
//********修改记录************
 //$field 字段名 $value新值
 //如要修改多个字段可重复调用来函数。
 
 
function Modify($field,$value){
  
$SQL="UPDATE FROM ".$this->Table." SET ".$field."=".$value." ".$this->Condition;
  
mysql_query($SQL) or die(mysql_error());
 }
 
 
//**********显示页数*************
 //显示当前页及总页数
  
 
function ThePage() {
   
$this->TPages=ceil($this->Total/$this->MaxLine);
   
$this->CPages=$this->Offset/$this->MaxLine+1;
   echo 
"第".$this->CPages."页/共".$this->TPages."页";
 }

 
//**********显示翻页按钮*************
 //此函数要在ThePage()函数之后调用!!!
 //显示首页、下页、上页、未页,并加上要传递的参数
 
 
function Page() {
   
$first=0;
   
$next=$this->Offset+$this->MaxLine;
   
$prev=$this->Offset-$this->MaxLine;
   
$last=($this->TPages-1)*$this->MaxLine;

   
$k=count($this->PageQuery);
   
$strQuery="";    //生成一个要传递参数字串
   
for($i=0;$i<$k;$i++){
     
$strQuery.="&".$this->PageQuery[$i][key]."=".$this->PageQuery[$i][value];
   }

   if(
$this->Offset>=$this->MaxLine)
     echo 
"<A href=$PHP_SELF?offset=".$first.$strQuery.">首页</A>|";
   if(
$prev>=0
     echo 
"<A href=$PHP_SELF?offset=".$prev.$strQuery.">上一页</A>|";
   if(
$next<$this->Total)
     echo 
"<A href=$PHP_SELF?offset=".$next.$strQuery.">下一页</A>|";
   if(
$this->TPages!=&& $this->CPages<$this->TPages)
     echo 
"<A href=$PHP_SELF?offset=".$last.$strQuery.">末页</A>";
 } 
 
//******end class
}

?>

//************************
ebid.sql文件(我是用phpmyadmin导出的)

# phpMyAdmin MySQL-Dump
# http://www.htmlwizard.net/phpMyAdmin/
#
# Host: localhost Database : ebid

# --------------------------------------------------------
# Table structure for table 'reply'
# id,商品id,出价人,出价人的email,出价。

CREATE TABLE reply (
   id varchar(16) NOT NULL,
   parentid varchar(16) NOT NULL,
   buyer varchar(12) NOT NULL,
   email varchar(32) NOT NULL,
   price float(10,2) DEFAULT '0.00' NOT NULL,
   PRIMARY KEY (id, price)
);


# --------------------------------------------------------
# Table structure for table 'shop'
# id,商品名,介绍,原始价,加价单位,结束时间,竞标数,当前价,是否有照片

CREATE TABLE shop (
   id varchar(16) NOT NULL,
   name varchar(50) NOT NULL,
   description text,
   price float(10,2) DEFAULT '0.00' NOT NULL,
   unit tinyint(2) unsigned NOT NULL,
   endtime varchar(16) DEFAULT '0000-00-00 00:00' NOT NULL,
   reply int(4) unsigned NOT NULL,
   curprice float(10,2) DEFAULT '0.00' NOT NULL,
   photo tinyint(1) unsigned NOT NULL,
   PRIMARY KEY (id),
   KEY kreply (reply)
);

配置文件如下:
//**************
//config.inc.php

<?php

$HOST
="localhost";    //主机名
$DATABASE="ebid";    //数据库名
$WARE_TABLE="shop";    //商品表
$BID_TABLE="reply";    //回应表
$USER="root";        //用户
$PASSWD="9999";        //密码

$PAGE_MAX_LINE=20;    //每页显示行数

//打开数据库
$LinkID=mysql_connect($HOST,$USER,$PASSWD);
mysql_select_db($DATABASE,$LinkID) or die(mysql_error());

?>

以下是显示商品及TOP10商品的函数
//*****************
//
<?php
include "config.inc.php";
include 
"tview.class.php";    //类文件

//*****显示商品列表********
function PrintList(){
 global 
$view;
 
 
$ct=time();
 
 
//设置条件的句子!要满足SQL语法哦。只显示没有结束竞标的商品
 
$view->SetCondition("where endtime>'$ct' order by id desc");    
 
 
 
//调用成员函数来读记录
 //结果$result[记录号][字段名] 是二维数组。
 
$result=$view->ReadList();
   
 if(
$view->Number==0) {echo "<tr><td colspan=4>&nbsp</td></tr>"; return;}

 for(
$i=0;$i<$view->Number;$i++){
   if(
ceil($i/2)*2==$i$bgc="#ffffff";
   else 
$bgc="#f3f3f3";
   echo 
"<tr bgcolor=$bgc><td width=60% >";
   echo 
"<a href="javascript:showdetail('detail.php?id=".$result[$i][id]."')">".$result[$i][name]."</a>";
   echo 
"</td><td width=15% >";
   echo 
date("Y-m-j 24:00:00",$result[$i][endtime]);
   echo 
"</td><td width=15% align=right>¥";
   echo 
$result[$i][curprice];
   echo 
"</td><td width=10% align=right>";
   echo 
$result[$i][reply];
   echo 
"</td></tr>";
 }
}

//*********显示最热的10条记录**********
function ListTopHot(){
 global 
$view;
 
 
//同样先设置条件
 
$view->SetCondition("order by reply desc");
 
//读记录
 
$result=$view->ReadList();
 
 
$k=(count($result)>10)? '10':(count($result));
 
 for(
$i=0;$i<$k;$i++){
  echo 
"<tr><td>";
  echo 
"<a href="javascript:showdetail('detail.php?id=".$result[$i][id]."')">".$result[$i][name]."</a>";
  echo 
"</td></tr>";
 }
 
}

//*********显示最新10条记录***********
function ListTopNew(){
 global 
$view;
 
 
$view->SetCondition("order by id desc");
 
$result=$view->ReadList();
 
 
$k=(count($result)>10)? '10':(count($result));
 
 for(
$i=0;$i<$k;$i++){
  echo 
"<tr><td>";
  echo 
"<a href="javascript:showdetail('detail.php?id=".$result[$i][id]."')">".$result[$i][name]."</a>";
  echo 
"</td></tr>";
 }
}


//**********结束函数定义,主程序体*************
//构造这个viewpage类,给出商品表及每页显示行数

$view=new TViewPage($WARE_TABLE,$PAGE_MAX_LINE);

?>

下面给出用到的一个js函数吧,很简单,就是打开一个新窗口:
<script>
function showdetail(str){
 window.open(str,"newwin","top=20,left=20,width=600,height=400,
location=no,toolbar=no,status=no,resizable=no,scrollbars=yes"); 
}
</script>