SoFunction
Updated on 2025-04-12

php Advanced: Implementing Unlimited Classification Page 3/4


3. Program Control
------------------------------------------------------------ 

Implementing the infinite classification function is the most complex and arduous step. First, look at the steps that the program needs to complete:

1) Create a classification upload;
2) Create information upload;
3) Clearly display each category and its relationship;
4) Processing query function;
5) How to deal with editing and deletion functions;

The most difficult of these five steps is the fifth step, because the editing and deletion of classification involves the issue of consistency.

Below I will describe the program control of php one by one:

1) Create a category upload

Before introducing this function, let me first introduce the function exploit( ). This is a string processing function used to decompose strings. The specific usage is:

Decompose the numbers in "0:1:2:3:4"

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

After the exploit( ) function processing, all the numbers in $val are decomposed into the $rid array. When you want to reference, just print: echo '$rid[0], $rid[1], $rid[2]..."; . The exploit( ) function plays a very important role in the entire classification processing. Now let’s start introducing program control for no-present classification.

You can assume that a total classification is 0. All categories are their descendants. Now let’s establish the first classification 'system' and see how it is stored in the database:

id | uid | type | rout_id | rout_char 
1 | 0 | System | 0:1 | System

Then, the following is divided into 'Linux':

id | uid | type | rout_id | rout_char 
2 | 1 | Linux| 0:1:2 | System: Linux

The above is the form of database storage. Now let’s complete the php code. This is very similar to the forum code. All we have to do is put the id of the class into the uid, and the uid of the parent class is 0. Let’s take a look at the code below:

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

//Set the default page
if (empty($func)) $func=='showtype'; 

//Set the uid of the parent class
if (empty($uid)) $uid=0; 

//Database storage******************************************************
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=="") { 

//Get the route_id of the parent class
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"; 
//Form your own route_id
$route_; 
$values.=",'$route_id'"; 


//Form your own 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 */ 


//Category Upload***************************************************
if ($func=='createtype'): 

//Get your own 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; 


//Judge the status of the classification
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='parent classification';

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

echo "<table>"; 
echo "<tr><td>Category:$type</td></tr>";
echo "<tr><td>Create a category:<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='Save'></td></tr>";

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

//Show classification******************************************************
if ($func=='showtype'): 

echo "<table>"; 

//Judge the status of the classification
if ($uid!=0) { 
$result=mysql_query("select * from type where id=$uid"); 
$type=mysql_result($result,0,"type"); 
} else { 
$type='parent classification';


echo "<tr><td><a href='$php_self?func=createtype&uid=$uid'>Create classification</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 */ 
..... 
..... 

?> 

The above program completes the basic creation, storage and display of infinite classification, and then improves various parts of the classification creation function.
Previous page1234Next pageRead the full text