Home > Article > Backend Development > PHP implementation framework of online bidding system (1)_PHP tutorial
Earlier, I gave a class for paging mysql records, but did not give an example of its use. Now, I have compiled an online bidding system framework program I just wrote to illustrate the use of this class, and it is also for online bidding. Let’s discuss the implementation method with everyone.
First of all, let me state that I am not a master or an expert, I am just a fan, so this program must have many loopholes, but the reason why I dare to bring it out is because I really hope to freely share PHP with everyone. Bring us happiness. (Actually, I want to add more points to get a space that supports mysql^_^)
I think there are two biggest differences between the auction system and the general supply and demand information release system. One is The new price offered by the bidder must be reflected in the price of the product in a timely manner. Another point is that there is a time limit. After the bidding ends, bidding must stop. And the final winning bidder will be announced.
I haven’t thought of the others yet, maybe an expert can give me some introduction.
Therefore, I think it should not be difficult to turn a supply and demand information release system into an auction system.
Let’s first show the new version of TViewPage class and database structure.
<?php
/*********************************************
TViewPage v 1.2
Class for paging display of Mysql database records
Author: sharetop
E-mail:ycshowtop@21cn.com
Time: 2000-8-31
[ 2000-9-6] 1.2
Fixed a bug in readlist() and put the verification offset into the class.
Add three basic operation functions add() delete() modify().
This class does not provide the function of connecting to the database, so the corresponding database needs to be opened externally.
This class does not provide the function of displaying records. It only reads records into the Result two-dimensional array in pages.
The data display format needs to be customized externally.
***********************************************/
class TViewPage {
var $Table; //Table name
var $MaxLine; //Every Number of rows displayed on the page
var $Offset; //Record offset
var $Total; //Total number of records
var $Number; //Number of records read on this page
var $Result; //Read results
var $TPages; //Total number of pages
var $CPages; //Current number of pages
var $Condition; //Display Conditions such as: where id='$id' order by id desc
var $PageQuery; //Page display parameters to be passed
//******Constructor***** ********
//Parameters: table name, maximum number of rows, offset
function TViewPage($TB,$ML){
global $offset;
$this->Table=$TB;
$this->MaxLine=$ML;
if(isset($offset)) $this->Offset=$offset;
else $this->Offset=0;
$this->Condition="";
}
//********Set display conditions *********
//For example: where id='$id' order by id desc
//The requirement is a string, conforming to SQL syntax (this string will be added after the SQL statement )
function SetCondition($s){
$this->Condition=$s;
}
//******Set the passing parameters** **********
// key parameter name value parameter value
// For example: setpagequery("id",$id); If there are multiple parameters to be passed, it can be called multiple times This function.
function SetPageQuery($key,$value){
$tmp[key]=$key; $tmp[value]=$value;
$this->PageQuery[]=$ tmp;
}
//********Read record****************
//Main working function, according to The given conditions read the corresponding records from the table
// The return value is a two-dimensional array, Result[record number][field name]
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) { //according to ConditionCondition
$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;
}
//************Add new record**********
//$str is the added value, such as "'$id','$name','$class'", etc.
function Add($str){
$SQL=" INSERT INTO ".$this->Table." VALUES(".$str.")";
mysql_query($SQL) or die(mysql_error());
}
//**********Delete record************
//First call SetCondition() to determine the conditions.
function Delete(){
$SQL="DELETE FROM ".$this->Table." ".$this->Condition;
mysql_query($SQL) or die( mysql_error());
}
//************Modify record************
//$field field name$value New value
//If you want to modify multiple fields, you can call the function repeatedly.
function Modify($field,$value){
$SQL="UPDATE FROM ".$this->Table." SET ".$field."=".$value." " .$this->Condition;
mysql_query($SQL) or die(mysql_error());
}
//**********Display Page count**********
//Display the current page and total page count
function ThePage() {
$this->TPages=ceil( $this->Total/$this->MaxLine);
$this->CPages=$this->Offset/$this->MaxLine+1;
echo "th".$ this->CPages."Pages/Total".$this->TPages."Pages";
}
//************Display page button* ************
//This function should be called after the ThePage() function! ! !
//Display the homepage, next page, previous page, and next page, and add the parameters to be passed
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=""; //Generate a parameter string to pass
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.">Homepage>|";
if($prev>=0)
echo "<A href=$PHP_SELF?offset=".$prev.$strQuery." >Previous page</A>|";
if($next<$this->Total)
echo "<A href=$PHP_SELF?offset=".$next.$strQuery." >Next page</A>|";
if($this->TPages!=0 && $this->CPages<$this->TPages)
echo "<A href= $PHP_SELF?offset=".$last.$strQuery.">Last page</A>";
}
//******end class
}
?>
//************************
ebid.sql file ( I exported it using phpmyadmin)
# phpMyAdmin MySQL-Dump
# http://www.htmlwizard.net/phpMyAdmin/
#
# Host: localhost Database: ebid
#------------------------------------------------ -----------
# Table structure for table 'reply'
# id, product id, bidder, bidder's email, bid.
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, product name, introduction, original price, markup unit, end time, number of bids, current price, whether there are photos
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)
);
The configuration file is as follows:
//**************
//config.inc.php
<?php
$HOST="localhost"; //Host name
$DATABASE="ebid"; //Database name
$WARE_TABLE="shop"; //Product table
$BID_TABLE="reply"; //Response table
$USER="root"; //User
$PASSWD="9999"; //Password
$PAGE_MAX_LINE=20; //Number of lines displayed per page
//Open the database
$LinkID=mysql_connect($HOST,$USER,$PASSWD);
mysql_select_db($DATABASE,$LinkID) or die(mysql_error());
?>
The following is the function to display products and TOP10 products
//**********************
//
< ?php
include "config.inc.php";
include "tview.class.php"; //Class file
//*****Display product list* *******
function PrintList(){
global $view;
$ct=time();
//Sentence to set conditions! It must meet the SQL syntax. Only display products that have not ended bidding
$view->SetCondition("where endtime>'$ct' order by id desc");
//Call member function to read records
/ /Result$result[record number][field name] is a two-dimensional array.
$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>