当前位置:首页> PHP教程> PHP代码
关键字
文章内容
在线竞拍系统的PHP实现框架(二)
 
 
修改时间:[2012/03/04 23:06]    阅读次数:[1419]    发表者:[起缘]
 
上回说完了商品的列表显示方法。下面来谈谈显示商品详细信息的方法,这与我们在论坛中常用的显示帖子的方法大同小异。

********显示详细信息的文件***********
detail.php

<?php
include "config.inc.php";

//显示商品详细信息
//用全局变量来做,可以方便地取到多个字段的值。
//有id,名字,价格,介绍,当前价,单位,图片,结束时间。
function ReadDetail(){
global $WARE_TABLE;
global $id,$name,$price,$description,$cprice,$unit,$image,$endtime;

$SQL="select id,name,description,price,unit,endtime,reply,curprice,photo from $WARE_TABLE where id='$id'";
$result=mysql_query($SQL) or die(mysql_error());

//下面这一句,也可以用list(...)来做,但我*惯这样分开,看起来是麻烦点,不过清楚些,特别是有要处理的字段时,这样就很有必要了。

$row=mysql_fetch_array($result);
$name=$row[name];
$price=$row[price];
$description=$row[description];
$unit=$row[unit];
$cprice=$row[curprice];
$endtime=date("Y-m-j",$row[endtime]);
if($row[photo]==1) $image=$row[id].".jpg";
else $image="";
}


//读最新10个买家信息
function ReadBuyer(){
global $BID_TABLE;
global $id,$buyer,$buyprice,$date,$num;

$SQL="select id,buyer,email,price from $BID_TABLE where parentid='$id' order by id desc";
$result=mysql_query($SQL) or die(mysql_error());
$k=mysql_num_rows($result);

$num=($k>10)? 10:$k; //判断一下,是不是多于10个。

for($i=0;$i<$num;$i++){
$row=mysql_fetch_array($result);
$buyer[]=$row[buyer];
$buyprice[]=$row[price];
$date[]=date("Y-m-j H:i:s",$row[id]);
}
}


//显示10个买家信息。
function ShowBuyer(){
global $buyer,$buyprice,$date,$num;

for($i=0;$i<$num;$i++){
echo "<tr><td width=25%>".$date[$i]."</td>";
echo "<td width=40%>".$buyer[$i]."</td>";
echo "<td width=35%>".$buyprice[$i]."</td></tr>";
}
}

ReadDetail();
ReadBuyer();

?>

  完成了上面的处理,就可以html中用<? echo $name?>或<? echo $price;?>等来取到要显示的信息了,这些我就不详述了。
  就是这句,要根据是否有图片分别显示文字或图片。
<? if($image=="") echo "没有照片"; else echo "<img src=photo/$image>"; ?>



  在显示商品详细资料时,也应让用户可以投标,所以此时要对用户下的新价做出一个判断,我比较懒,就直接用deamweaver来做这个表单的校验了,只把其中的英文说明改成中文,不过由于在判断新出价的方面有点特殊,所以对dw生成的函数做了一点点改动。

<script language="JavaScript">
<!--
function MM_findObj(n, d) { //v3.0
var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); return x;
}

function MM_validateForm() { //v3.0
var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
if (val) { nm=val.name; if ((val=val.value)!="") {
if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
if (p<1 || p==(val.length-1)) errors+='- '+nm+' 必须是正确的email地址.n';
} else if (test!='R') { num = parseFloat(val);
if (val!=''+num) errors+='- '+nm+' 必须是数值.n';
if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
min=test.substring(8,p); max=test.substring(p+1);
//**********改动部分*********
var k=(num-min)/max;
//判断要大于当前价,并且是当前价+加价单位的整数倍
if (num<min || k != Math.ceil(k) ) errors+='- '+nm+' 必须是大于 '+min+' 并且加价必须为 '+max+'的整数倍.n';
} } } //*******结束改动部分********
else if (test.charAt(0) == 'R') errors += '- '+nm+' 要填完整.n'; }
} if (errors) alert('发现错误如下:n'+errors);
document.MM_returnValue = (errors == '');
}
//-->
</script>

下面的事就简单了,还要有什么呢?显示一下已结束竞标的商品吧。这个功能的实现与显示未结束的商品方法一样,我就不罗嗦了。唯一的区别在于一是SetCondition()条件的不同和在显示结束竞价的商品详细资料时,没有用户出价的表单。


另外,我再给出添加新商品的方法吧。

<?php
include "config.inc.php";
include "tview.class.php";

function AddRecord(){
global $view;
//以下变量都是从上一个页面的表单中得到的。
global $name,$price,$unit,$year,$month,$day,$photofile,$intro;

$t=time();
$c=nl2br($intro);
$et=mktime(24,0,0,$month,$day,$year);

//判断一下是否有图上传了。
$ph=( $photofile!="none" and !empty($photofile) )? '1':'0';

//调用成员函数add()参数就是sql语句中的values()中的字串
$view->Add("'$t','$name','$c','$price','$unit','$et','0','$price','$ph'");

//如果有图片,拷到指定目录下。
if($ph=='1')
copy($photofile,"photo/$t.jpg");

}

if( $Submit){
$view= new TViewPage($WARE_TABLE,20);
AddRecord();
header("Location:manager.htm");
}

?>

  好了,罗罗嗦嗦写了两篇,不知道可不可以又加个几百分哦。:)

  有什么问题真心地请大家提出来讨论,我可是把程序拿出来了,您见到什么不妥的不当的,可千万不要吝啬,记得告诉我一声。

  我的email : ycshowtop@21cn.com

  另外,如果你想要这个程序比较完整的代码,也来个email,我寄给您。