The method of realizing infinite classification by Php+mysql database _php skill

Source: Internet
Author: User
Tags phpmyadmin

This paper illustrates the method of realizing infinite classification of php+mysql database. Share to everyone for your reference. The specific analysis is as follows:

This PHP unlimited classification code more complete, including the database is MySQL, there are additions, deletions, edits, mobile functions, but also provide the database SQL table structure. The code is as follows:

Copy Code code as follows:
Connecting to a database
$link = mysql_connect (' localhost ', ' root ', ') ' or Die (mysql_error ());
mysql_select_db (' class ', $link) or Die (Mysql_error ());
mysql_query ("Set names GBK");
Infinite Classification Class Library
Class sortclass{
var $data = array ();
var $child = array ( -1=>array ());
var $layer = array ( -1=>-1);
var $parent = array ();
var $link;
var $table;
function Sortclass ($link, $table) {
$this->setnode (0,-1, ' top pole node ');
$this->link = $link;
$this->table = $table;
$node = Array ();
$results = mysql_query ("select * from $this->table", $this->link);
while ($node = Mysql_fetch_array ($results)) {
$this->setnode ($node [' id '], $node [' f_id '], $node [' name ']);
}
}
function Setnode ($id, $parent, $value) {
$parent = $parent? $parent: 0;
$this->data[$id] = $value;
$this->child[$id] = array ();
$this->child[$parent] = $id;
$this->parent[$id] = $parent;
$this->layer[$id] =!isset ($this->layer[$parent])? 0: $this->layer[$parent] + 1;
}
Function GetList (& $tree, $root = 0) {
foreach ($this->child[$root] as $key => $id) {
$tree [] = $id;
if ($this->child[$id]) $this->getlist ($tree, $id);
}
}
function GetValue ($id) {return $this->data[$id];}
function Getlayer ($id, $space = False) {
Return $space str_repeat ($space, $this->layer[$id]): $this->layer[$id];
}
function GetParent ($id) {return $this->parent[$id];}
function Getparents ($id) {
while ($this->parent[$id]!=-1) {
$id = $parent [$this->layer[$id]] = $this->parent[$id];
}
Ksort ($parent);
Reset ($parent);
return $parent;
}
function Getchild ($id) {return $this->child[$id];}
function Getchilds ($id = 0) {
$child = Array ($id);
$this->getlist ($child, $id);
return $child;
}
function AddNode ($name, $pid) {
echo "INSERT into $this->table (' f_id ', ' name ') VALUES (' $pid ', ' $name ')"; exit;
mysql_query ("INSERT INTO $this->table (' f_id ', ' name ') VALUES (' $pid ', ' $name ')", $this->link);
}
function Modnode ($cid, $newname) {
mysql_query ("Update $this->table set ' name ' = ' $newname ' where ' id ' = $cid", $this->link);
}
function Delnode ($cid) {
$allchilds = $this->getchilds ($cid);
$sql = ';
if (Emptyempty ($allchilds)) {
$sql = "Delete from $this->table where ' id ' = $cid";
}else{
$sql = ' Delete from '. $this->table. ' WHERE ' id '. Implode (', ', $allchilds). ', '. $cid. ') ';
}
mysql_query ($sql, $this->link);
}
function MoveNode ($cid, $topid) {
mysql_query ("Update $this->table set ' f_id ' = $topid where ' id ' = $cid", $this->link);
}
}
Function
function back () {
Echo ' <script language= ' web effects ' >window.location.href= ' news.class.php? +new date (). gettime ();</script> ';
Exit
}
Generate Select
function Makeselect ($array, $formname) {
Global $tree;
$select = ' <select name= '. $formname. ' " > ';
foreach ($array as $id) {
$select. = ' <option value= '. $id. ' > $tree->getlayer ($id, ' | | '). $tree->getvalue ($id). " </option> ";
}
return $select. ' </select> ';
}
$tree = new Sortclass ($link, ' p_newsclass ');
$op =!emptyempty ($_post[' op '])? $_post[' op ']: $_get[' op '];
if (!emptyempty ($op)) {
if ($op = = ' Add ') {
$tree->addnode ($_post[' cname '],$_post[' pid '));
Back ();
}
if ($op = = ' MoD ') {
$tree->modnode ($_post[' cid '],$_post[' cname '));
Back ();
}
if ($op = = ' del ') {
$tree->delnode ($_get[' CID '));
Back ();
}
if ($op = = ' Move ') {
$tree->movenode ($_post[' Who '],$_post[' to '));
Back ();
}
}
$category = $tree->getchilds ();
?>

The foreground call instance code is as follows:
Copy Code code as follows:
<style type= "Text/css" >
body{font-size:12px;}
Ul{list-style:none;}
A{cursor:pointer;}
</style>
<script language= "JavaScript" >
function $ (e) {return document.getElementById (e);}
Function mod (CID) {
$ (' CID '). Value=cid;
$ (' op '). value= ' mod ';
$ (' name '). style.border= ' 1px solid red ';
}
</script>
<form action= "" method= "POST" >
Name: <input type= "text" id= "name" name= "CNAME"/> Added to: <?=makeselect ($category, ' pid ')? ><br/>
<input type= "hidden" id= "Op" name= "Op" value= "Add"/>
<input type= "hidden" id= "CID" name= "CID"/>
<input type= "Submit" value= "Submit"/>
</form>
<form action= "" method= "POST" >
<?=makeselect ($category, ' who ')?> gt; Move to: <?=makeselect ($category, ' to ')?>
<input type= "hidden" id= "Op" name= "Op" value= "Move"/>
<input type= "Submit" value= "Submit"/>
</form>
<ul>
<?php
foreach ($category as $id) {
Echo ' <li> ' $tree->getlayer ($id, ' | | '). $tree->getvalue ($id). ' <a href= ' time.php?op=del&cid= '. $ Id. ' " >del</a> <a onclick= "mod ('. $id. ')" >edit</a> </li> ';
}
?>
</ul>

Importing this database with phpMyAdmin is OK, and the instance code is as follows:
Copy Code code as follows:
--phpMyAdmin SQL dump
--Version 3.2.4
--
--Host: localhost
--Date Created: July 02, 2010 03:02
--Server version: 5.1.41
--PHP Version: 5.3.1
Set sql_mode= "No_auto_value_on_zero";

/*!40101 set @old_character_set_client =@ @character_set_client * *;
/*!40101 set @old_character_set_results =@ @character_set_results * *;
/*!40101 set @old_collation_connection =@ @collation_connection * *;
/*!40101 Set Names UTF8 * *;
--
--Database: ' Class '
--
-- --------------------------------------------------------
--
--The structure of the table ' P_newsclass '
--
CREATE table if not EXISTS ' p_newsclass ' (
' ID ' int (7) NOT NULL auto_increment,
' f_id ' int (7) NOT NULL,
' Name ' varchar (255) NOT NULL,
Primary KEY (' ID ')
) Engine=innodb default Charset=utf8 auto_increment=10;
--
--the data in the Dump table ' P_newsclass '
--
Insert INTO ' p_newsclass ' (' id ', ' f_id ', ' name ') values
(3, 0, ' China '),
(4, 3, ' Fujian '),
(5, 4, ' Longyan '),
(7, 4, ' Xiamen City '),
(9, 5, ' Zhangping ');
/*!40101 Set character_set_client= @old_character_set_client * *;
/*!40101 Set character_set_results= @old_character_set_results * *;
/*!40101 Set collation_connection= @old_collation_connection * *;

I hope this article will help you with the Php+mysql program design.

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.