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

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

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

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

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

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

<?php

/*********************************************

tviewpage v 1.2

分页显示mysql数据库记录的类

作者:sharetop

e-mail:ycshowtop@21cn.com

时间:2000-8-31

[2000-9-6] 1.2

修正了readlist()的一个bug,将验证offset放入类中。

增加add() delete() modify()三个基本操作函数。

  本类没有提供连接数据库的功能,所以需在外部打开相应的数据库。

  本类也没有提供显示记录的功能,只是分页读取记录至 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!=0 && $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> </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>