Login
网站首页 > 文章中心 > 其它

10个超级有用的php代码样例

作者:小编 更新时间:2023-08-16 08:52:43 浏览量:272人看过

【高分】急求用php写的购物车代码!!!!!(十万火急)如果您提供的好用还有加分!!!

我也要弄一个这种购物车,

我去写个,贴出来,【嘿嘿,今天上午新写的】.

我懒得新建数据库,用的是我的数据库.

你按照我的改一下就能用了

本人水平有限,高手请指正.

你,大,爷的,虽然不咋地,保证能用

、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、

经过调试,

//$my-empty_cart('Guest'); //清空购物车

$ok=$my-get_cart('Guest'); //返回购物车

这些都可用

-------------------------------------------------------------------

php

class Cart

{

public $totalCost=0; //商品总金额

function cart($host,$usr,$pwd,$db)

mysql_connect($host,$usr,$pwd) or die(mysql_error);

mysql_select_db($db) or die(mysql_error);

mysql_query("SET Names GBk");

//只要有人访问,就自动清除一天前所有没付款的订单;

$sql="delete FROM shopcart WHERE TO_DAYS( NOW( )) - TO_DAYS( ptime ) =1 and payment=0";

mysql_query($sql);

}

// 弹出提示

function alter($Str,$Url)

echo "Script language='JavaScript' alert('".$Str."');/Script";

echo "meta http-equiv=refresh content=0;URL=".$Url."";

//增加购物;三个参数:pid:产品ID,ptl:产品数量,pcid:产品类别

//查询数据库,是否存在此人在本日内订过本产品

//如果订过,那么数量累加,否则插入一个数据库行

function add_cart($pid,$ptl=1,$pcid)

if($ptl=100 || $ptl=0)

die();

if(!$_SESSION['usr']) { $usr='Guest';}

else { $usr=$_SESSION['usr'];}

$sql="select * from shopcart where pid='".$pid."' and usr='".$usr."' and pcid='".$pcid."'";

$ex=mysql_query($sql);

$ex1=mysql_fetch_array($ex);

if(!$ex1)

$sql="select * from product where ID='".$pid."' and class1='".$pcid."'";

$ok=mysql_query($sql);

$rs=mysql_fetch_array($ok);

if($rs)

$totalCost= $rs['Price'] * $ptl;

$sql="insert into shopcart(usr,pid,pname,ptl,price,pcid,psum,payment) Values(";

$sql.="'".$usr."',";

$sql.="'".$rs['ID']."',";

$sql.="'".$rs['Name']."',";

$sql.="'".$ptl."',";

$sql.="'".$rs['Price']."',";

$sql.="'".$rs['Class1']."',";

$sql.="'".$totalCost."',";

$sql.="'0')";

mysql_query($sql) or die(mysql_error());

if($ok) { $this-alter("购物成功","index.php"); }

else { $this-alter("购物失败","index.php"); }

else

$this-alter("不存在的商品,或者参数错误","index.php");

$sql="update shopcart set ptl= ptl◆1,psum = psum◆price where ID='".$ex1['ID']."'";

$this-alter("更新数量成功","index.php");

//更新购物车的单个产品的数量;

function updata_cart($cid,$ptl,$pid)

if($ptl=100||$ptl=0)

$this-alter('产品数量不对!','index.php');

$sql="select * from shopcart where ID='".$cid."' and pid='".$pid."'";

if(!ok) { alter("参数发生错误","index.php");}

$sql="update shopcart set ptl='".$ptl."',psum=price * '".$ptl."' where ID='".$cid."' and pid='".$pid."'";

if(!ok) { $this-alter("更新失败","index.php");}

else { $this-alter("更新成功","index.php");}

function del_cart($cid,$pid,$usr)

$sql="delete from shopcart where usr='".$usr."' and ID='".$cid."' and pid='".$pid."'";

if(!$ok) {$this-alter("删除失败","index.php");}

else {$this-alter("删除成功","index.php");}

function empty_cart($usr)

$sql="delete from shopcart where usr='".$usr."'";

mysql_query($sql) or die(mysql_error);

function get_cart($usr)

$sql="select * from shopcart where usr='".$usr."'";

return $ok;

$my = new Cart("localhost","root","root","mybbs");

//$my-empty_cart('Guest');

$ok=$my-get_cart('Admin');

echo "usr pid pname ptl price pcid psum payment ptime brhrbr";

while($rs=mysql_fetch_array($ok))

、、、、、、、、、、、、、、、、、SQL、、、、、、、、、、、、、、

CREATE TABLE IF NOT EXISTS ◆shopcart◆ (

◆ID◆ int(10) NOT NULL auto_increment,

◆pname◆ varchar(100) NOT NULL,

◆pcid◆ varchar(100) NOT NULL,

◆payment◆ tinyint(1) NOT NULL,

◆ptime◆ timestamp NOT NULL default CURRENT_TIMESTAMP,

PRIMARY KEY (◆ID◆)

)

product 里面用的ID CLASS1是

高质量PHP代码的50个技巧(3)

/**

Method to execute a command in the terminal

Uses :

① system

*/

function terminal($command)

//system

if(function_exists('system'))

ob_start();

system($command , $return_var);

$output = ob_get_contents();

ob_end_clean();

//passthru

else if(function_exists('passthru'))

passthru($command , $return_var);

//exec

else if(function_exists('exec'))

exec($command , $output , $return_var);

$output = implode("\n" , $output);

//shell_exec

else if(function_exists('shell_exec'))

$output = shell_exec($command) ;

$output = 'Command execution not possible on this system';

$return_var = 1;

return array('output' = $output , 'status' = $return_var);

terminal('ls');

上面的函数将运行shell命令, 只要有一个系统函数可用, 这保持了代码的一致性.

①.

function add_to_cart($item_id , $qty)

$_SESSION['cart']['item_id'] = $qty;

使用上面的函数添加单个项目. 而当添加项列表的时候,你要创建另一个函数吗? 不用, 只要稍加留意不同类型的参数, 就会更灵活. 如:

①.0

①.1

if(!is_array($item_id))

foreach($item_id as $i_id = $qty)

$_SESSION['cart']['i_id'] = $qty;

现在, 同个函数可以处理不同类型的输入参数了. 可以参照上面的例子重构你的多处代码, 使其更智能.

我很想知道为什么这么多关于php建议的博客文章都没提到这点.

echo "Hello";

//Now dont close this tag

这将节约你很多时间. 我们举个例子:

一个 super_class.php 文件

class super_class

function super_function()

//super code

//super extra character after the closing tag

index.php

require_once('super_class.php');

//echo an image or pdf , or set the cookies or session data

这样, 你将会得到一个 Headers already send error. 为什么? 因为 "super extra character" 已经被输出了. 现在你得开始调试啦. 这会花费大量时间寻找 super extra 的位置.所以呢, 养成省略关闭符的习惯:

//No closing tag

这会更好.

这称为输出缓冲, 假如说你已在不同的函数输出内容:

function print_header()

echo "p id='header'Site Log and Login links/p";

function print_footer()

echo "p id='footer'Site was made by me/p";

print_header();

for($i = 0 ; $i 100; $i◆◆)

echo "I is : $i ';

print_footer();

替代方案, 在某地方集中收集输出. 你可以存储在函数的局部变量中, 也可以使用ob_start和ob_end_clean. 如下:

$o = "p id='header'Site Log and Login links/p";

return $o;

$o = "p id='footer'Site was made by me/p";

echo print_header();

echo print_footer();

为什么需要输出缓冲:

可以在发送给浏览器前更改输出. 如 str_replaces 函数或可能是 preg_replaces 或添加些监控/调试的html内容.

输出给浏览器的同时又做php的处理很糟糕. 你应该看到过有些站点的侧边栏或中间出现错误信息. 知道为什么会发生吗? 因为处理和输出混合了.

输出一些xml.

$xml = "response

code0/code

/response";

//Send xml data

echo $xml;

工作得不错. 但需要一些改进.

几种常用PHP连接数据库的代码示例

PHP连接数据库之PHP连接MYSQL数据库代码

php?

$mysql_server_name='localhost';?

//改成自己的mysql数据库服务器?

$mysql_username='root';?

//改成自己的mysql数据库用户名?

//改成自己的mysql数据库密码?

$mysql_database='mycounter';

//改成自己的mysql数据库名?

$conn=mysql_connect($mysql_server_name,

$mysql_username,$mysql_password,

$mysql_database);?

$sql='CREATE?DATABASE?mycounter?

DEFAULT?CHARACTER?SET?gbk?COLLATE?gbk_chinese_ci;?

';?

mysql_query($sql);?

$sql='CREATE?TABLE?◆counter◆?

UNSIGNED?NOT?NULL?DEFAULT?0,PRIMARY?KEY?

(?◆id◆?)?)?TYPE?=?innodb;';?

mysql_select_db($mysql_database,$conn);?

$result=mysql_query($sql);?

//echo?$sql;?

mysql_close($conn);?

echo?"Hello!数据库mycounter已经成功建立!";?

PHP连接数据库之PHP连接ACCESS数据库代码方法

$conn?=?new?com("ADODB.Connection");?

$connstr?=?"DRIVER={Microsoft

Access?Driver?(*.mdb)};?

DBQ=".?realpath("data/db.mdb");?

$conn-Open($connstr);?

$rs?=?new?com("ADODB.RecordSet");?

$rs-Open("select?*

from?szd_t",$conn,1,1);?

while(!?$rs-eof)?{?

$f?=?$rs-Fields(1);?

echo?$f-value;?

$rs-MoveNext();?

}?

PHP多态代码实例

多态定义:只关心一个接口或者基类,而不关心一个对象的具体类.(同一类型,不同结果)

这里两个例子:

第一个,我们发现,基类定义了标准,子类进行了自我规则的实现.这是多态的一个要求.同时,这是满足重写;实际上这是不同类的不同表现;没有严格满足一个接口,或者基类编程.因为你调用的时候不是

stu-showGrade()

而是各自自己的方法;

class

stu{

public

function

showGrade(){

echo

base

class;

xiaomin

extends

is

son

show

xiaoli

doit($obj){

if(get_class($obj)

!=

stu){

$obj-showGrade();

doit(new

xiaoli());

xiaomin());

第二个例子:dovoice

参数规定了$obj

为animal,意识就是用接口

接受了

实现类对象.了向上转型.这就符合同一类型,不同结果了,这就是多态;

实际上在Java中

会是

animal

a

=

new

dog();这样子的;因为PHP

是若类型语言.没有对象转型机制.

interface

animal{

voice();

cat

implements

voice(){

miao~~~br;

dog

wang

~~~br;

dovoice(animal

$obj){

$obj-voice();

dovoice(new

dog());

cat());

php代码 $a = 10; $b = & $a; $c = 100; $a = &$c; echo $b; echo $a;

不画图还真不好说,你理解的还不够深入

首先$a = 10; 假设分配了一个地址为A的内存空间给$a并存值10.

然后$b = $a 这样就把大A这个地址也给了$b(如果不加的话就会又分配一个空间给$b,加了就直接映射到A)

$c = 100,又分配一个地址为C的空间存了值100

$a = $c 关键就在这了,这个时候你把刚刚的C的地址付给了$a,但是原来大A的值是没有变得,$b仍然是映射的A的这个地址 只不过$a换了个地址指到C去了.你这里如果不传址,而传值的话,就会把A里的值给改了,$b也就改了.

打个比方就是原来$a 是一楼住户,里面有10个美女

然后$b你告诉他他也是一楼的.

$c是二楼的,有100个美女.

然后你现在告诉$a你不是一楼的了,你给换到二楼去了!

也不知道你听懂没,反正你自己捋捋,就明白传值跟传址的含义了

以上就是土嘎嘎小编为大家整理的10个超级有用的php代码样例相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!

版权声明:倡导尊重与保护知识产权。未经许可,任何人不得复制、转载、或以其他方式使用本站《原创》内容,违者将追究其法律责任。本站文章内容,部分图片来源于网络,如有侵权,请联系我们修改或者删除处理。

编辑推荐

热门文章