欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php 進階:實現(xiàn)無限分類第3/4頁

 更新時間:2006年12月16日 00:00:00   作者:  

3.程序控制 
------------------------------------------------------------ 

實現(xiàn)無限分類這個功能中就屬這一步最為復(fù)雜辛苦,首先看看程序需要完成的步驟: 

1)創(chuàng)建分類上傳; 
2)創(chuàng)建信息上傳; 
3)明確顯示各分類及其之間的關(guān)系; 
4)處理查詢功能; 
5)如何處理編輯和刪除的功能; 

而這五步中最為困難的就是第五個步驟,因為對分類的編輯和刪除涉及到一至性的問題. 

下面我就逐一描述 php 的程序控制: 

1)創(chuàng)建分類上傳 

在介紹這個功能前,先介紹一下 explode( ) 這個函數(shù),這是個字串處理函數(shù),用來分解字串的,具體的用法,例: 

分解"0:1:2:3:4"里的數(shù)字 

$val='0:1:2:3:4'; 
$rid=explode(":",$val); 

經(jīng)過 explode( ) 函數(shù)處理,$val 內(nèi)的所有數(shù)字都分解到 $rid 數(shù)組中了,要引用時只需打印:echo '$rid[0],$rid[1],$rid[2]..."; 就行了.explode( ) 函數(shù)在整個分類處理中起著非常重要的作用,好現(xiàn)在開始介紹無現(xiàn)分類的程序控制. 

可以假設(shè)個總分類 0 ,所有的分類都是它的子孫分類,現(xiàn)在來建立第一個分類'系統(tǒng)',來看看它在數(shù)據(jù)庫的存儲形式: 

id | uid | type | rout_id | rout_char 
1 | 0 | 系統(tǒng) | 0:1 | 系統(tǒng) 

接著又在下面分'Linux': 

id | uid | type | rout_id | rout_char 
2 | 1 | Linux| 0:1:2 | 系統(tǒng):Linux 

以上就是數(shù)據(jù)庫存儲的形式,現(xiàn)在就來完成 php 的代碼,這與論壇的代碼很相似,我們所要做的就是將分類的 id 放入 uid,而父分類的 uid 就放 0,下面來看看代碼: 

<? 
..... 
..... 

//設(shè)置默認頁 
if (empty($func)) $func=='showtype'; 

//設(shè)置父分類的 uid 
if (empty($uid)) $uid=0; 

//數(shù)據(jù)庫存儲************************************************ 
if ($func=='save'): 

$fields = ""; 
$values = ""; 

if ($id!="") { 
$fields .= ",id"; 
$values.=",$id"; 


if ($uid!="") { 
$fields .= ",uid"; 
$values.=",$uid"; 


if ($type!="") { 
$fields .= ",type"; 
$values.=",'$type'"; 


if ($route_id=="") { 

//取得父分類的 route_id 
if ($uid!=0) { 
$result = mysqlquery("select * from type where id=$uid"); 
$route_id=mysql_result($result,0,"route_id"); 
} else { 
$routr_id='0'; 

$fields .= ",route_id"; 
//形成自己的 route_id 
$route_id="$route_id:$id"; 
$values.=",'$route_id'"; 


//形成自己的 route_char 
if ($route_char!="") { 
$fields .= ",route_char"; 
$route_char="$route_char:$type"; 
$values.=",'$route_char'"; 
} else { 
$fields .= ",route_char"; 
$route_char=$type; 
$values.=",'$route_char'"; 


$fields = substr($fields,1,strlen($fields)-1); 
$values = substr($values,1,strlen($values)-1); 

$result = mysqlquery("insert into type ($fields) values ($values)"); 
... 
endif; /* end save */ 


//分類上傳************************************************ 
if ($func=='createtype'): 

//取得自己的 id 
$result = mysqlquery("select * from type order by 
id desc"); 
$num=mysql_numrows($result); 
if (!empty($num)) { 
$cat = mysql_result($result,0,"id"); 
} else { 
$cat=0; 


//判斷分類的狀態(tài) 
if ($uid != 0) { 
$result=mysql_query("select * from type where id=$uid"); 
$type=mysql_result($result,0,"type"); 
$route_char=mysql_result($result,0,"route_char"); 
} else { 
$type='父分類'; 

echo "<FORM ACTION="$PHP_SELF?func=save" METHOD=POST>"; 

echo "<table>"; 
echo "<tr><td>所屬類別:$type</td></tr>"; 
echo "<tr><td>創(chuàng)建分類:<input type=text name='type' SIZE=10 MAXLENGTH=100></td></tr>"; 

echo "<tr><td>"; 
$cat=$cat+1; 
echo "<input type=hidden name=id value='$cat'>"; 
echo "<input type=hidden name=uid value='$uid'>"; 
echo "<input type=hidden name=route_char value='$route_char'>"; 
echo "<INPUT TYPE=submit NAME='Save' VALUE='保存'></td></tr>"; 

echo "</table>"; 
echo "</form>"; 
endif; /* end createtype */ 

//顯示分類************************************************ 
if ($func=='showtype'): 

echo "<table>"; 

//判斷分類的狀態(tài) 
if ($uid!=0) { 
$result=mysql_query("select * from type where id=$uid"); 
$type=mysql_result($result,0,"type"); 
} else { 
$type='父分類'; 


echo "<tr><td><a href='$php_self?func=createtype&uid=$uid'>創(chuàng)建分類</a></td></tr>"; 

echo "<tr><td>$type</td></tr>"; 

$result=mysql_query("select * from type where uid=$uid"); 
$num=mysql_numrows($result); 

if (!empty($num)) { 
for ($i=0;$i<$num;$i++) { 

$id=mysql_result($result,$i,"id"); 
$type=mysql_result($result,$i,"type"); 

echo "<tr><td>"; 
echo "<a href='$php_self?func=showtype&uid=$id'>$type</a>"; 
echo "</td></tr>"; 



echo "</table>"; 
endif; /* end showtype */ 
..... 
..... 

?> 

以上的程序便完成了無限分類的基本創(chuàng)建,存儲和顯示,接著就是完善分類創(chuàng)建功能的各個部分了.

相關(guān)文章

最新評論