Home > php教程 > php手册 > 一个用数据库的php二级联动

一个用数据库的php二级联动

WBOY
Release: 2016-06-21 09:09:39
Original
1370 people have browsed it

数据|数据库

/*******************************************
**********功能:php二级联动菜单*************
**********作者:冲星*************************
**********Email:njj@nuc.edu.cn**************
**********日期:2004/10/02******************
**********请转载时保留版权信息**************
*******************************************/

require_once('db.inc.php');//数据库连接
$db=new hq_online;
$db1=new hq_online;
$db->query("select * from news_bclass order by id desc");
$fMenu="";
$fValue="";
while($db->next_record()){
$fMenu.="\"".$db->Record["bname"]."\",";
$fValue.="\"".$db->Record["id"]."\",";

}
$fMenu=substr($fMenu,0,(strlen($fMenu)-1));
$fMenu="[".$fMenu."]";//*****************************得到var fMenu
$fValue=substr($fValue,0,(strlen($fValue)-1));
$fValue="[".$fValue."]";//*****************************得到var fValue

//得到*****************************var sMenu
$db->query("select * from news_bclass order by id desc");
while($db->next_record()){
$parentid=$db->Record["id"];
$db1->query("select * from news_sclass where parentid='$parentid' order by parentid desc");
while($db1->next_record()){
$num=$db1->num_rows();
$i++;
$sMenu.="\"".$db1->Record["sname"]."\",";
if($i==$num){
$sMenu="[".$sMenu."],[";
$i=0;
}
}
}
$sMenu.="]";
$sMenu=str_replace("\",]","\"]",$sMenu);
$sMenu=str_replace(",[]","]",$sMenu);
if((substr($sMenu,0,2))=="[\""){
$sMenu="[".$sMenu;
}else{
$sMenu=preg_replace("/\[{3,}/is","[[\\1",$sMenu);//匹配
}

//得到*****************************var sValue
$db->query("select * from news_bclass order by id desc");
while($db->next_record()){
$parentid=$db->Record["id"];
$db1->query("select * from news_sclass where parentid='$parentid' order by parentid desc");
while($db1->next_record()){
$nums=$db1->num_rows();
$j++;
$sValue.="\"".$db1->Record["id"]."\",";
if($j==$nums){
$sValue="[".$sValue."],[";
$j=0;
}
}
}
$sValue.="]";
$sValue=str_replace("\",]","\"]",$sValue);
$sValue=str_replace(",[]","]",$sValue);
if((substr($sValue,0,2))=="[\""){
$sValue="[".$sValue;
}else{
$sValue=preg_replace("/\[{3,}/is","[[\\1",$sValue);//匹配
}

?>




/*
调用方法:
将此代码保存为文件ld2.php
例如要在write.php页面放一个二级联动,则在write.php页面
要放置二级联动的地方加一句require_once("ld2.php");
若write.php要向save.php页面提交数据则在save.php中使用
$bigclass=$_POST["bigclass"];//取得大类的id值
$smallclass=$_POST["smallclass"];//取得小类的id值
接下来就知道该怎么做了吧……
附表的结构:
 可能有的表的结构和我有些不同,但基本点都一样,修改程序
中对应字段和表名就ok
程序写的有点乱,不过绝对可以放心的使用
欢迎与我交流探讨!
---------------------------------------------------------
大类的表news_bclass结构

CREATE TABLE news_bclass (
id int(11) NOT NULL auto_increment,
bname varchar(10) NOT NULL default '',
PRIMARY KEY (id),
UNIQUE KEY id_2 (id),
KEY id (id)
) TYPE=MyISAM;

字段说明:bname为大类中文名称
------------------------------

小类的表news_sclass结构

CREATE TABLE news_sclass (
id int(11) NOT NULL auto_increment,
sname varchar(10) NOT NULL default '',
parentid int(10) NOT NULL default '0',
bname varchar(10) NOT NULL default '',
PRIMARY KEY (id),
UNIQUE KEY id_2 (id),
KEY id (id)
) TYPE=MyISAM;

字段说明:sname为小类中文名称
     parendid为大类中的id值
bname为大类中文名称
-----------------------------------------------------------


*/
?>

db.inc.php文件的内容
class hq_online extends db_sql{
var $Host="localhost";
var $Database="hq";
var $User="root";
var $password="";
}



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template