选择显示字体大小

php/mysql购物车

<?
if(!&#36;session && !&#36;scid) {
&#36;session = md5(uniqid(rand()));
setcookie(&quot;scid&quot;, &quot;&#36;session&quot;, time() + 14400);
} /* last number is expiration time in seconds, 14400 sec = 4 hrs */

class cart {
function check_item(&#36;table, &#36;session, &#36;product) {
&#36;query = &quot;select * from &#36;table where session='&#36;session' and product='&#36;product' &quot;;
&#36;result = mysql_query(&#36;query);

if(!&#36;result) {
return 0;
}

&#36;numrows = mysql_num_rows(&#36;result);

if(&#36;numrows == 0) {
return 0;
} else {
&#36;row = mysql_fetch_object(&#36;result);
return &#36;row->quantity;
}
}

function add_item(&#36;table, &#36;session, &#36;product, &#36;quantity) {
&#36;qty = &#36;this->check_item(&#36;table, &#36;session, &#36;product);
if(&#36;qty == 0) {
&#36;query = &quot;insert into &#36;table (session, product, quantity) values &quot;;
&#36;query .= &quot;('&#36;session', '&#36;product', '&#36;quantity') &quot;;
mysql_query(&#36;query);
} else {
&#36;quantity += &#36;qty;
&#36;query = &quot;update &#36;table set quantity='&#36;quantity' where session='&#36;session' and &quot;;
&#36;query .= &quot;product='&#36;product' &quot;;
mysql_query(&#36;query);
}
}

function delete_item(&#36;table, &#36;session, &#36;product) {
&#36;query = &quot;delete from &#36;table where session='&#36;session' and product='&#36;product' &quot;;
mysql_query(&#36;query);
}

function modify_quantity(&#36;table, &#36;session, &#36;product, &#36;quantity) {
&#36;query = &quot;update &#36;table set quantity='&#36;quantity' where session='&#36;session' &quot;;
&#36;query .= &quot;and product='&#36;product' &quot;;
mysql_query(&#36;query);
}

function clear_cart(&#36;table, &#36;session) {
&#36;query = &quot;delete from &#36;table where session='&#36;session' &quot;;
mysql_query(&#36;query);
}

function cart_total(&#36;table, &#36;session) {
&#36;query = &quot;select * from &#36;table where session='&#36;session' &quot;;
&#36;result = mysql_query(&#36;query);
if(mysql_num_rows(&#36;result) > 0) {
while(&#36;row = mysql_fetch_object(&#36;result)) {
&#36;query = &quot;select price from inventory where product='&#36;row->product' &quot;;
&#36;invresult = mysql_query(&#36;query);
&#36;row_price = mysql_fetch_object(&#36;invresult);
&#36;total += (&#36;row_price->price * &#36;row->quantity);
}
}
return &#36;total;
}

function display_contents(&#36;table, &#36;session) {
&#36;count = 0;
&#36;query = &quot;select * from &#36;table where session='&#36;session' order by id &quot;;
&#36;result = mysql_query(&#36;query);
while(&#36;row = mysql_fetch_object(&#36;result)) {
&#36;query = &quot;select * from inventory where product='&#36;row->product' &quot;;
&#36;result_inv = mysql_query(&#36;query);
&#36;row_inventory = mysql_fetch_object(&#36;result_inv);
&#36;contents[&quot;product&quot;][&#36;count] = &#36;row_inventory->product;
&#36;contents[&quot;price&quot;][&#36;count] = &#36;row_inventory->price;
&#36;contents[&quot;quantity&quot;][&#36;count] = &#36;row->quantity;
&#36;contents[&quot;total&quot;][&#36;count] = (&#36;row_inventory->price * &#36;row->quantity);
&#36;contents[&quot;description&quot;][&#36;count] = &#36;row_inventory->description;
&#36;count++;
}
&#36;total = &#36;this->cart_total(&#36;table, &#36;session);
&#36;contents[&quot;final&quot;] = &#36;total;
return &#36;contents;
}

function num_items(&#36;table, &#36;session) {
&#36;query = &quot;select * from &#36;table where session='&#36;session' &quot;;
&#36;result = mysql_query(&#36;query);
&#36;num_rows = mysql_num_rows(&#36;result);
return &#36;num_rows;
}

function quant_items(&#36;table, &#36;session) {
&#36;quant = 0;
&#36;query = &quot;select * from &#36;table where session='&#36;session' &quot;;
&#36;result = mysql_query(&#36;query);
while(&#36;row = mysql_fetch_object(&#36;result)) {
&#36;quant += &#36;row->quantity;
}
return &#36;quant;
}
}
?>

/*
this part contains a description of how to create the tables on your mysql server.

# mysql dump 6.0
#
# host: localhost database: kmartshopper
#--------------------------------------------------------
# server version 3.22.25

#
# table structure for table 'inventory'
#
create table inventory (
product tinytext not null,
quantity tinytext not null,
id int(4) default '0' not null auto_increment,
description tinytext not null,
price float(10,2) default '0.00' not null,
category char(1) default '' not null,
key id (id),
primary key (id),
key price (price)
);

#
# table structure for table 'shopping'
#
create table shopping (
session tinytext not null,
product tinytext not null,
quantity tinytext not null,
card tinytext not null,
id int(4) default '0' not null auto_increment,
key id (id),
primary key (id)
);
*/

example
<?
include(&quot;shoppingcart.php&quot;);
&#36;cart = new cart;
&#36;mysql_link = mysql_connect(&quot;localhost&quot;, &quot;wwwrun&quot;, &quot;&quot;);
&#36;mysql_select_db(&quot;kmartshopper&quot;, &#36;mysql_link) /* heh, use whatever database name you put the 2 tables under in place of kmartshopper */
?>
/* call functions like &#36;cart->add_item and such, see the code. */   


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons