推薦:解析PHP網站開發中常見的問題【1】頁面之間無法傳遞變量
get,post,session在最新的php版本中自動全局變量是關閉的,所以要從上一頁面取得提交過來得變量要使用_GET['foo'],_POST['foo'],_SESSION['foo']來得到
可以假設個總分類 0 ,所有的分類都是它的子孫分類,現在來建立第一個分類'系統',來看看它在數據庫的存儲形式:
id | uid | type | rout_id | rout_char 1 | 0 | 系統 | 0:1 | 系統
接著又在下面分'Linux':
id | uid | type | rout_id | rout_char 2 | 1 | Linux| 0:1:2 | 系統:Linux
以上就是數據庫存儲的形式,現在就來完成 php 的代碼,這與論壇的代碼很相似,我們所要做的就是將分類的 id 放入 uid,而父分類的 uid 就放 0,下面來看看代碼:
以下為引用的內容:
<?
.....
.....
//設置默認頁
if (empty(func)) func=='showtype';
//設置父分類的 uid
if (empty(uid)) uid=0;
//數據庫存儲************************************************
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;
}
//判斷分類的狀態
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>創建分類:<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>";
//判斷分類的狀態
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'>創建分類</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 */
.....
.....
?>
|
以上的程序便完成了無限分類的基本創建,存儲和顯示,接著就是完善分類創建功能的各個部分了。
分享:淺析關于cookie和session1. PHP的COOKIE
cookie 是一種在遠程瀏覽器端儲存數據并以此來跟蹤和識別用戶的機制。
PHP在http協議的頭信息里發送cookie, 因此 setcookie() 函數必須在其它信息被輸出到瀏覽器前調用,