文章詳情頁
在線競拍系統的PHP實現框架(一)
瀏覽:100日期:2023-12-23 16:37:22
前面我給了一個分頁顯示mysql記錄的類,卻沒給出使用的例子,現在,我整理了我剛寫的一個在線競拍系統框架程序,來說明這個類的使用方法,而且也就在線競拍的實現方法與大家一起來討論一下。 首先聲明,我不是高手,也不是行家,只是一個fans,所以這個程序肯定有不少漏洞,但我之所以敢拿出來,是因為我很希望能自由地與大家分享PHP帶給我們的快樂。(其實是想多加點分好弄個支持mysql的空間^_^) 我覺得競拍系統與一般的供求信息發布系統相比,最大的不同有兩點,一點是出價者開的新價要及時地反映在商品的價格上,另一點是有時間的限制,在競標結束后,就要停止出價。并且給出最后中標者。 其它的我還沒想到呢,有行家給點介紹吧。 所以,我想把一個供求信息發布系統做成一個競拍系統應是不困難的事吧。 下面先把新版的TViewPage類和數據庫結構給出來吧。 <?php /********************************************* TViewPage v 1.2 分頁顯示Mysql數據庫記錄的類 作者:sharetop E-mail:[email protected] 時間: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 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 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>
標簽:
PHP
排行榜
