Unlimited PHP classification (3)

Source: Internet
Author: User
3. Program Control
------------------------------------------------------------

This step is the most complex and difficult to implement the unlimited classification function. First, let's look at the steps that need to be completed by the Program:

1) create a category for upload;
2) create Information upload;
3) clearly show the relationship between each category and its relationship;
4) query processing function;
5) how to handle the editing and deletion functions;

The fifth step is the most difficult of the five steps, because the editing and deletion of categories involves a one-to-one problem.

Next I will describe PHP program control one by one:

1) Create category upload

Before introducing this function, we will first introduce the explode () function, which is a string processing function used to break down strings. The specific usage is as follows:

Break down the numbers in "0: 1: 2: 3: 4"

$ Val = '0: 1: 2: 3: 4 ';
$ Rid = explode (":", $ Val );

After processing by the explode () function, all the numbers in $ Val are decomposed into the $ rid array. to reference the number, print: Echo '$ rid [0]. $ rid [1], $ rid [2]... "; that's all. the explode () function plays an important role in the entire classification process. Now we will introduce program control for non-existing classification.

Assume that a total category is 0, and all categories are its descendant categories. Now we will create the first classification 'system' to see its storage format in the database:

Id | uid | type | rout_id | rout_char
1 | 0 | system | 0: 1 | System

Next we will divide it into 'linux ':

Id | uid | type | rout_id | rout_char
2 | 1 | Linux | System: Linux

The above is the form of database storage. Now we can complete the PHP code, which is very similar to the Forum code. All we need to do is to put the classification ID into the UID, the UID of the parent category is 0. Let's look at the code below:

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

// Set the hosts page
If (empty ($ func) $ func = 'showtype ';

// Set the UID of the parent category
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 = ""){

// Obtain the route_id of the parent category
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_id = "$ route_id: $ id ";
$ 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 */

// Upload by category ************************************ ************
If ($ func = 'createtype '):

// Obtain your own ID
$ Result = mysqlquery ("select * from type order
Id DESC ");
$ Num = mysql_numrows ($ result );
If (! Empty ($ num )){
$ Cat = mysql_result ($ result, 0, "ID ");
} Else {
$ Cat = 0;
}

// Determine the classification status
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 category ';
}
Echo "<form action =" $ php_self? Func = save "method = post> ";

Echo "<Table> ";
Echo "<tr> <TD> category: $ type </TD> </tr> ";
Echo "<tr> <TD> Create 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 */

// Display the category ************************************ ************
If ($ func = 'showtype '):

Echo "<Table> ";

// Determine the classification status
If ($ uid! = 0 ){
$ Result = mysql_query ("select * from type where id = $ uid ");
$ Type = mysql_result ($ result, 0, "type ");
} Else {
$ Type = 'parent category ';
}

Echo "<tr> <TD> <a href = '$ php_self? Func = createtype & uid = $ uid '> Create a Category </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 */
.....
.....

?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.