10個超級有用的php代碼樣例,php經典實例

本文目錄一覽:

【高分】急求用php寫的購物車代碼!!!!!(十萬火急)如果您提供的好用還有加分!!!

我也要弄一個這種購物車,

我去寫個,貼出來,【嘿嘿,今天上午新寫的】。

我懶得新建資料庫,用的是我的資料庫。

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

本人水平有限,高手請指正。

你,大,爺的,雖然不咋地,保證能用

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

經過調試,

//$my-add_cart(45,3,”茶几系列”);//新增購物

//$my-updata_cart(13,13,8); //更新購物

//$my-del_cart(12,5,’Guest’); //刪除一種購物

//$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)

{

$this-alter(“最多買99件,最少1件”,”index.php”);

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”);

die();

}

}

else

{

$sql=”update shopcart set ptl= ptl+1,psum = psum+price where ID='”.$ex1[‘ID’].”‘”;

mysql_query($sql);

$this-alter(“更新數量成功”,”index.php”);

}

}

//更新購物車的單個產品的數量;

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

{

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

{

$this-alter(‘產品數量不對!’,’index.php’);

die();

}

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

$ok=mysql_query($sql);

if(!ok) { alter(“參數發生錯誤”,”index.php”);}

else

{

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

$ok=mysql_query($sql);

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.”‘”;

$ok=mysql_query($sql);

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.”‘”;

$ok=mysql_query($sql);

return $ok;

}

}

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

//$my-add_cart(45,3,”茶几系列”);

//$my-updata_cart(13,13,8);

//$my-del_cart(12,5,’Guest’);

//$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))

{

echo $rs[1].”-“.$rs[2].”-“.$rs[3].”-“.$rs[4].”-“.$rs[5].”-“.$rs[6].”-“.$rs[7].”-“.$rs[8].”-“.$rs[9].”br”;

}

?

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

CREATE TABLE IF NOT EXISTS `shopcart` (

`ID` int(10) NOT NULL auto_increment,

`usr` varchar(50) NOT NULL,

`pid` int(5) NOT NULL,

`pname` varchar(100) NOT NULL,

`ptl` int(3) NOT NULL,

`price` decimal(50,2) NOT NULL default ‘0.00’,

`pcid` varchar(100) NOT NULL,

`psum` decimal(50,2) NOT NULL default ‘0.00’,

`payment` tinyint(1) NOT NULL,

`ptime` timestamp NOT NULL default CURRENT_TIMESTAMP,

PRIMARY KEY (`ID`)

)

product 裡面用的ID CLASS1是

`ID` int(6) NOT NULL auto_increment,

`Class1` varchar(20) NOT NULL,

`Price` int(6) NOT NULL,

高質量PHP代碼的50個技巧(3)

42

43

44

45

/**

Method to execute a command in the terminal

Uses :

1. system

2. passthru

3. exec

4. shell_exec

*/

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’))

{

ob_start();

passthru($command , $return_var);

$output = ob_get_contents();

ob_end_clean();

}

//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) ;

}

else

{

$output = ‘Command execution not possible on this system’;

$return_var = 1;

}

return array(‘output’ = $output , ‘status’ = $return_var);

}

terminal(‘ls’);

上面的函數將運行shell命令, 只要有一個系統函數可用, 這保持了代碼的一致性.

5. 靈活編寫函數

?

1

2

3

4

5

6

function add_to_cart($item_id , $qty)

{

$_SESSION[‘cart’][‘item_id’] = $qty;

}

add_to_cart( ‘IPHONE3’ , 2 );

使用上面的函數添加單個項目. 而當添加項列表的時候,你要創建另一個函數嗎? 不用, 只要稍加留意不同類型的參數, 就會更靈活. 如:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

function add_to_cart($item_id , $qty)

{

if(!is_array($item_id))

{

$_SESSION[‘cart’][‘item_id’] = $qty;

}

else

{

foreach($item_id as $i_id = $qty)

{

$_SESSION[‘cart’][‘i_id’] = $qty;

}

}

}

add_to_cart( ‘IPHONE3’ , 2 );

add_to_cart( array(‘IPHONE3’ = 2 , ‘IPAD’ = 5) );

現在, 同個函數可以處理不同類型的輸入參數了. 可以參照上面的例子重構你的多處代碼, 使其更智能.

6. 有意忽略php關閉標籤

我很想知道為什麼這麼多關於php建議的博客文章都沒提到這點.

?

1

2

3

?php

echo “Hello”;

//Now dont close this tag

這將節約你很多時間. 我們舉個例子:

一個 super_class.php 文件

?

1

2

3

4

5

6

7

8

9

?php

class super_class

{

function super_function()

{

//super code

}

}

?

//super extra character after the closing tag

index.php

?

1

2

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 的位置。因此, 養成省略關閉符的習慣:

?

1

2

3

4

5

6

7

8

9

?php

class super_class

{

function super_function()

{

//super code

}

}

//No closing tag

這會更好.

7. 在某地方收集所有輸入, 一次輸出給瀏覽器

這稱為輸出緩衝, 假如說你已在不同的函數輸出內容:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

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. 如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

function print_header()

{

$o = “p id=’header’Site Log and Login links/p”;

return $o;

}

function print_footer()

{

$o = “p id=’footer’Site was made by me/p”;

return $o;

}

echo print_header();

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

{

echo “I is : $i ‘;

}

echo print_footer();

為什麼需要輸出緩衝:

可以在發送給瀏覽器前更改輸出. 如 str_replaces 函數或可能是 preg_replaces 或添加些監控/調試的html內容.

輸出給瀏覽器的同時又做php的處理很糟糕. 你應該看到過有些站點的側邊欄或中間出現錯誤信息. 知道為什麼會發生嗎? 因為處理和輸出混合了.

8. 發送正確的mime類型頭信息, 如果輸出非html內容的話.

輸出一些xml.

?

1

2

3

4

5

6

$xml = ‘?xml version=”1.0″ encoding=”utf-8″ standalone=”yes”?’;

$xml = “response

code0/code

/response”;

//Send xml data

echo $xml;

工作得不錯. 但需要一些改進.

?

1

2

3

4

5

6

7

$xml = ‘?xml version=”1.0″ encoding=”utf-8″ standalone=”yes”?’;

$xml = “response

code0/code

幾種常用PHP連接資料庫的代碼示例

PHP連接資料庫之PHP連接MYSQL資料庫代碼

 ?php   

$mysql_server_name=’localhost’; 

//改成自己的mysql資料庫伺服器  

$mysql_username=’root’; 

//改成自己的mysql資料庫用戶名  

$mysql_password=’12345678′; 

//改成自己的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` 

(`id` INT(255) UNSIGNED NOT NULL 

AUTO_INCREMENT ,`count` INT(255) 

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多態代碼實例

這篇文章主要介紹了PHP多態代碼實例,本文用2個代碼實例來演示PHP中的多態,需要的朋友可以參考下

多態定義:只關心一個介面或者基類,而不關心一個對象的具體類。(同一類型,不同結果)

這裡兩個例子:

第一個,我們發現,基類定義了標準,子類進行了自我規則的實現。這是多態的一個要求。同時,這是滿足重寫;實際上這是不同類的不同表現;沒有嚴格滿足一個介面,或者基類編程。因為你調用的時候不是

stu-showGrade()

而是各自自己的方法;

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

class

stu{

public

function

showGrade(){

echo

base

class;

}

}

class

xiaomin

extends

stu{

public

function

showGrade(){

echo

is

son

show

80;

}

}

class

xiaoli

extends

stu{

public

function

showGrade(){

echo

is

son

show

60;

}

}

function

doit($obj){

if(get_class($obj)

!=

stu){

$obj-showGrade();

}

}

doit(new

xiaoli());

doit(new

xiaomin());

第二個例子:dovoice

參數規定了$obj

為animal,意識就是用介面

接受了

實現類對象。了向上轉型。這就符合同一類型,不同結果了,這就是多態;

實際上在Java中

會是

animal

a

=

new

dog();這樣子的;因為PHP

是若類型語言。沒有對象轉型機制。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

interface

animal{

public

function

voice();

}

class

cat

implements

animal{

public

function

voice(){

echo

miao~~~br;

}

}

class

dog

implements

animal{

public

function

voice(){

echo

wang

~~~br;

}

}

function

dovoice(animal

$obj){

$obj-voice();

}

dovoice(new

dog());

dovoice(new

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你不是一樓的了,你給換到二樓去了!

也不知道你聽懂沒,反正你自己捋捋,就明白傳值跟傳址的含義了

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/240198.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-12 12:20
下一篇 2024-12-12 12:20

相關推薦

  • Python周杰倫代碼用法介紹

    本文將從多個方面對Python周杰倫代碼進行詳細的闡述。 一、代碼介紹 from urllib.request import urlopen from bs4 import Bea…

    編程 2025-04-29
  • Python字元串寬度不限制怎麼打代碼

    本文將為大家詳細介紹Python字元串寬度不限制時如何打代碼的幾個方面。 一、保持代碼風格的統一 在Python字元串寬度不限制的情況下,我們可以寫出很長很長的一行代碼。但是,為了…

    編程 2025-04-29
  • Python基礎代碼用法介紹

    本文將從多個方面對Python基礎代碼進行解析和詳細闡述,力求讓讀者深刻理解Python基礎代碼。通過本文的學習,相信大家對Python的學習和應用會更加輕鬆和高效。 一、變數和數…

    編程 2025-04-29
  • Python生成隨機數的應用和實例

    本文將向您介紹如何使用Python生成50個60到100之間的隨機數,並將列舉使用隨機數的幾個實際應用場景。 一、生成隨機數的代碼示例 import random # 生成50個6…

    編程 2025-04-29
  • Python滿天星代碼:讓編程變得更加簡單

    本文將從多個方面詳細闡述Python滿天星代碼,為大家介紹它的優點以及如何在編程中使用。無論是剛剛接觸編程還是資深程序員,都能從中獲得一定的收穫。 一、簡介 Python滿天星代碼…

    編程 2025-04-29
  • 倉庫管理系統代碼設計Python

    這篇文章將詳細探討如何設計一個基於Python的倉庫管理系統。 一、基本需求 在著手設計之前,我們首先需要確定倉庫管理系統的基本需求。 我們可以將需求分為以下幾個方面: 1、庫存管…

    編程 2025-04-29
  • 寫代碼新手教程

    本文將從語言選擇、學習方法、編碼規範以及常見問題解答等多個方面,為編程新手提供實用、簡明的教程。 一、語言選擇 作為編程新手,選擇一門編程語言是很關鍵的一步。以下是幾個有代表性的編…

    編程 2025-04-29
  • Python實現簡易心形代碼

    在這個文章中,我們將會介紹如何用Python語言編寫一個非常簡單的代碼來生成一個心形圖案。我們將會從安裝Python開始介紹,逐步深入了解如何實現這一任務。 一、安裝Python …

    編程 2025-04-29
  • 怎麼寫不影響Python運行的長段代碼

    在Python編程的過程中,我們不可避免地需要編寫一些長段代碼,包括函數、類、複雜的控制語句等等。在編寫這些代碼時,我們需要考慮代碼可讀性、易用性以及對Python運行性能的影響。…

    編程 2025-04-29
  • 北化教務管理系統介紹及開發代碼示例

    本文將從多個方面對北化教務管理系統進行介紹及開發代碼示例,幫助開發者更好地理解和應用該系統。 一、項目介紹 北化教務管理系統是一款針對高校學生和教職工的綜合信息管理系統。系統實現的…

    編程 2025-04-29

發表回復

登錄後才能評論