찾다
php教程php手册php 进阶:实现无限分类(3)

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

实现无限分类这个功能中就属这一步最为复杂辛苦,首先看看程序需要完成的步骤:

1)创建分类上传;
2)创建信息上传;
3)明确显示各分类及其之间的关系;
4)处理查询功能;
5)如何处理编辑和删除的功能;

而这五步中最为困难的就是第五个步骤,因为对分类的编辑和删除涉及到一至性的问题.

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

1)创建分类上传

在介绍这个功能前,先介绍一下 explode( ) 这个函数,这是个字串处理函数,用来分解字串的,具体的用法,例:

分解"0:1:2:3:4"里的数字

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

经过 explode( ) 函数处理,$val 内的所有数字都分解到 $rid 数组中了,要引用时只需打印:echo '$rid[0],$rid[1],$rid[2]..."; 就行了.explode( ) 函数在整个分类处理中起着非常重要的作用,好现在开始介绍无现分类的程序控制.

可以假设个总分类 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 "

";

echo "";
echo "";
echo "";

echo "";

echo "
所属类别:$type
创建分类:
";
$cat=$cat+1;
echo "";
echo "";
echo "";
echo "
";
echo "
";
endif; /* end createtype */

//显示分类************************************************
if ($func=='showtype'):

echo "";

//判断分类的状态
if ($uid!=0) {
$result=mysql_query("select * from type where id=$uid");
$type=mysql_result($result,0,"type");
} else {
$type='父分类';
}

echo "";

echo "";

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

if (!empty($num)) {
for ($i=0;$i
$id=mysql_result($result,$i,"id");
$type=mysql_result($result,$i,"type");

echo "";
}
}

echo "
创建分类
$type
";
echo "$type";
echo "
";
endif; /* end showtype */
.....
.....

?>



성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

인기 기사

R.E.P.O. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 최고의 그래픽 설정
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 아무도들을 수없는 경우 오디오를 수정하는 방법
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25 : Myrise에서 모든 것을 잠금 해제하는 방법
4 몇 주 전By尊渡假赌尊渡假赌尊渡假赌

뜨거운 도구

MinGW - Windows용 미니멀리스트 GNU

MinGW - Windows용 미니멀리스트 GNU

이 프로젝트는 osdn.net/projects/mingw로 마이그레이션되는 중입니다. 계속해서 그곳에서 우리를 팔로우할 수 있습니다. MinGW: GCC(GNU Compiler Collection)의 기본 Windows 포트로, 기본 Windows 애플리케이션을 구축하기 위한 무료 배포 가능 가져오기 라이브러리 및 헤더 파일로 C99 기능을 지원하는 MSVC 런타임에 대한 확장이 포함되어 있습니다. 모든 MinGW 소프트웨어는 64비트 Windows 플랫폼에서 실행될 수 있습니다.

Eclipse용 SAP NetWeaver 서버 어댑터

Eclipse용 SAP NetWeaver 서버 어댑터

Eclipse를 SAP NetWeaver 애플리케이션 서버와 통합합니다.

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

Dreamweaver Mac版

Dreamweaver Mac版

시각적 웹 개발 도구

SublimeText3 Linux 새 버전

SublimeText3 Linux 새 버전

SublimeText3 Linux 최신 버전