在數據庫建立的時候就已經分好了 給每個欄目一個p_id
公司主營業務:成都做網站、網站設計、外貿營銷網站建設、移動網站開發等業務。幫助企業客戶真正實現互聯網宣傳,提高企業的競爭能力。成都創新互聯公司是一支青春激揚、勤奮敬業、活力青春激揚、勤奮敬業、活力澎湃、和諧高效的團隊。公司秉承以“開放、自由、嚴謹、自律”為核心的企業文化,感謝他們對我們的高要求,感謝他們從不同領域給我們帶來的挑戰,讓我們激情的團隊有機會用頭腦與智慧不斷的給客戶帶來驚喜。成都創新互聯公司推出衢江免費做網站回饋大家。
最上層菜單p_id為0
子菜單p_id為上級菜單的id
查詢的時候就根據id和pid來查找
找出所有就按照遞歸來找
function?selectReferee($rid){
//查詢該推薦人是否滿3個人
$sql?=?"SELECT?*?FROM?talbe_name?WHERE?`id`?=?".$rid;
$res?=?array();//$res為查詢結果
if(count($res)=3){
foreach?($res?as?$k=$v){
selectReferee($v['id']);//如果該推薦人滿了3個人,則遞歸查詢下面的所有人
}
}else{
return?$rid;//返回推薦人id
}
}
大概的思路就是這樣,主要是一個遞歸,你自己完善,測試一下
這個需要用程序遞歸處理
$dept_tree=[];
$deptid?=?19;
while($deptid0){
//假設你的數據庫查詢是這個函數,根據條件直接查詢一條記錄返回
$dept?=?getone('department',['id'=$deptid]);
//防止數據丟失出錯
if(empty($dept))break;
array_unshift($dept_tree,$dept);
$deptid?=?$dept['dep_parentid'];
//如果需要防止數據錯亂出現遞歸,這里可以判斷一下
if(in_array($deptid,array_column($dept_tree,'id')?!==?false){
//說明職位關系亂了,有死循環
break;
}
}
可以把這段代碼封裝成一個函數使用。這里就不論從哪一級開始查詢了,總是能把該職位及其上級全部查詢出來,按順序放進數組里
$dept_tree 類似這樣
array(
0=array(
'id'=1,
'dep_parentid'=0,
'dep_name'='頂級',
),
1=array(
'id'=2,
'dep_parentid'=1,
'dep_name'='國燦金融',
),
2=array(
'id'=4,
'dep_parentid'=2,
'dep_name'='招聘部',
),
3=array(
'id'=19,
'dep_parentid'=4,
'dep_name'='經理助理',
),
)
?php
if(!defined("INCLUDE_MYSQL_OK")) {
define("INCLUDE_MYSQL_OK","");
class MySQL_class {
var $debug = true;
var $db,
$id,
$result, /* 查詢結果指針 */
$rows, /* 查詢結果行數 */
$fields, /* 查詢結果列數 */
$data, /* 數據結果 */
$arows, /* 發生作用的紀錄行數目 */
$iid; /* 上次插入操作后,可能存在的"AUTO_INCREMENT"屬性字段的值,如果為"0",則為空 */
var $user, $pass, $host, $charset;
/*
* 請注意用戶名和密碼是否正確
*/
function Setup ($host, $user, $pass, $charset='utf8') {
$this-host = $host;
$this-user = $user;
$this-pass = $pass;
$this-charset = $charset;
}
function Connect ($db = "") {
global $CFG_MYSQL_INFO;
if (!$this-host) {
$this-host = $CFG_MYSQL_INFO["host"];
}
if (!$this-user) {
$this-user = $CFG_MYSQL_INFO["user"]; /* 在這里作修改 */
}
if (!$this-pass) {
$this-pass = $CFG_MYSQL_INFO["passwd"]; /* 在這里作修改 */
}
if (!$this-charset) {
$this-charset = "utf8"; /* 在這里作修改 */
}
if (empty($db))
$this-db = $CFG_MYSQL_INFO["database"];
else
$this-db = $db;
$this-id = @mysql_connect($this-host, $this-user, $this-pass);
if (!$this-id)
return false;
$this-SelectDB($this-db); /* 定位到指定數據庫 */
$this-Query("SET NAMES '".$this-charset."'");
return true;
}
function Close(){
@mysql_close($this-id);
}
function SelectDB ($db) {
if(!@mysql_select_db($db, $this-id))
return false;
else
return true;
}
function Begin () {
$this-result = @mysql_query("START TRANSACTION WITH CONSISTENT SNAPSHOT", $this-id);
if (!$this-result)
return false;
return true;
}
function Commit () {
$this-result = @mysql_query("COMMIT", $this-id);
if (!$this-result)
return false;
return true;
}
function Rollback () {
$this-result = @mysql_query("ROLLBACK", $this-id);
if (!$this-result)
return false;
return true;
}
function Escape ($str) {
$escstr = mysql_escape_string($str);
return $escstr;
}
# 普通查詢功能,主要用于返回結果是多條記錄的情況
# 請使用 Fetch 方法取得每條記錄信息
function Query ($query) {
$this-result = @mysql_query($query, $this-id);
if (!$this-result)
{
if ($this-debug)
MySQL_ErrorMsg ("不能執行查詢(query): $query");
else
return false;
}
$this-rows = @mysql_num_rows($this-result);
$this-fields = @mysql_num_fields($this-result);
if (!$this-rows) return false;
return true;
}
function QuerySql ($query) {
$ret = @mysql_query($query, $this-id);
if ($ret === false)
{
if ($this-debug)
MySQL_ErrorMsg ("不能執行查詢(query): $query");
else
return false;
}
$this-result = $ret;
$this-rows = @mysql_num_rows($this-result);
$this-fields = @mysql_num_fields($this-result);
return true;
}
# 如果查詢結果為單條記錄時使用,返回結果存儲于數組 data 中
function QueryRow ($query) {
$this-result = @mysql_query($query, $this-id);
if (!$this-result)
{
if ($this-debug)
MySQL_ErrorMsg ("不能執行查詢(query): $query");
else
return false;
}
$this-rows = @mysql_num_rows($this-result);
$this-data = @mysql_fetch_array($this-result, MYSQL_ASSOC);
//MySQL_ErrorMsg ("不能從查詢結果中取得數據 $query");
if (!$this-result || !$this-rows)
return false;
return true;
}
# 移動到指定記錄行,將該行結果儲存于數組 data 中
function Fetch ($row) {
if(!@mysql_data_seek($this-result, $row))
//MySQL_ErrorMsg ("不能定位到指定數據行 $row");
return false;
$this-data = @mysql_fetch_array($this-result, MYSQL_ASSOC);
//MySQL_ErrorMsg ("不能提取指定數據行數據 $row");
if (!$this-data)
return false;
return true;
}
/* 以下方法將作用于 arows */
/* 此方法將作用于 iid */
function Insert ($query) {
$this-result = @mysql_query($query, $this-id);
if (!$this-result)
{
if ($this-debug)
MySQL_ErrorMsg ("不能執行查詢(query): $query");
else
return false;
}
$this-arows = @mysql_affected_rows($this-id);
$this-iid = @mysql_insert_id($this-id);
return true;
}
function Update ($query) {
$this-result = @mysql_query($query, $this-id);
if (!$this-result)
{
if ($this-debug)
MySQL_ErrorMsg ("不能執行查詢(query): $query");
else
return false;
}
$this-arows = @mysql_affected_rows($this-id);
if (!$this-arows || $this-arows == -1)
return false;
return true;
}
function Delete ($query) {
$this-result = @mysql_query($query, $this-id);
if (!$this-result)
{
if ($this-debug)
MySQL_ErrorMsg ("不能執行查詢(query): $query");
else
return false;
}
$this-arows = @mysql_affected_rows($this-id);
return true;
}
function Error (){
return mysql_error()."(".mysql_errno().")";
}
function Errno (){
return mysql_errno();
}
}
/*
* MySQL_ErrorMsg
* 輸出錯誤信息
*/
function MySQL_ErrorMsg ($msg) {
# 關閉可能影響字符顯示的HTML代碼
echo("/ul/dl/ol\n");
echo("/table/script\n");
# 錯誤信息
$text = "font color=\"#000000\" style=\"font-size: 9pt; line-height: 12pt\"p系統提示:".$msg."br";
$text .= "錯誤信息:";
$text .= mysql_error()."br";
$text .= "錯誤代碼:".mysql_errno()."brbr";
$text .= "請稍候再試,如果問題仍然存在,請與 a href=\"mailto:wuqiong@igenus.org\"系統管理員/a 聯系!";
$text .= "/font\n";
die($text);
}
}
?
一些細節的地方自己修改吧 主要是我在別的文件專門定義了全局變量,你看一遍,把應改的地方改一下就好了
分享題目:php數據庫分類查詢 php數據庫查詢結果處理
網頁路徑:http://vcdvsql.cn/article40/doisceo.html
成都網站建設公司_創新互聯,為您提供服務器托管、品牌網站建設、Google、虛擬主機、微信公眾號、品牌網站設計
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯