ホームページ >バックエンド開発 >PHPチュートリアル >良いステップですね~
もっと良い方法をお探しですか~?
次のようなデータ:
INSERT INTO `vm_nav` (`id`, `ctype`, `cid`, `name`, `ifshow`, `vieworder`, `opennew`, `url`, `type`) VALUES<br />(1, NULL, 0, 'index', 0, 1, 0, '', 'nav'),<br />(2, NULL, 0, 'product', 1, 2, 0, '', 'nav'),<br />(3, NULL, 0, 'member', 1, 3, 0, '', 'nav'),<br />(4, NULL, 0, 'guide', 1, 4, 0, '', 'nav'),<br />(5, NULL, 0, 'kehu', 1, 5, 0, '', 'nav'),<br />(6, NULL, 0, 'about', 1, 6, 0, '', 'nav'),<br />(7, NULL, 0, 'tehui', 1, 7, 0, '', 'nav'),<br />(8, NULL, 3, 'member_index', 1, 1, 0, '', 'nav'),<br />(9, NULL, 3, 'member_info', 1, 2, 0, '', 'nav');<br />
array(8) {<br /> [0]=><br /> array(6) {<br /> ["id"]=><br /> string(1) "2"<br /> ["name"]=><br /> string(12) "product"<br /> ["opennew"]=><br /> string(1) "0"<br /> ["url"]=><br /> string(0) ""<br /> ["ctype"]=><br /> NULL<br /> ["cid"]=><br /> string(1) "0"<br /> }<br /> [1]=><br /> array(6) {<br /> ["id"]=><br /> string(1) "3"<br /> ["name"]=><br /> string(18) "member"<br /> ["opennew"]=><br /> string(1) "0"<br /> ["url"]=><br /> string(0) ""<br /> ["ctype"]=><br /> NULL<br /> ["cid"]=><br /> string(1) "0"<br /> }<br /> [2]=><br /> array(6) {<br /> ["id"]=><br /> string(1) "8"<br /> ["name"]=><br /> string(12) "member_index"<br /> ["opennew"]=><br /> string(1) "0"<br /> ["url"]=><br /> string(0) ""<br /> ["ctype"]=><br /> NULL<br /> ["cid"]=><br /> string(1) "3"<br /> }<br /> [3]=><br /> array(6) {<br /> ["id"]=><br /> string(1) "9"<br /> ["name"]=><br /> string(12) "member_info"<br /> ["opennew"]=><br /> string(1) "0"<br /> ["url"]=><br /> string(0) ""<br /> ["ctype"]=><br /> NULL<br /> ["cid"]=><br /> string(1) "3"<br /> }<br /> [4]=><br /> array(6) {<br /> ["id"]=><br /> string(1) "4"<br /> ["name"]=><br /> string(12) "guide"<br /> ["opennew"]=><br /> string(1) "0"<br /> ["url"]=><br /> string(0) ""<br /> ["ctype"]=><br /> NULL<br /> ["cid"]=><br /> string(1) "0"<br /> }<br /> [5]=><br /> array(6) {<br /> ["id"]=><br /> string(1) "5"<br /> ["name"]=><br /> string(12) "kehu"<br /> ["opennew"]=><br /> string(1) "0"<br /> ["url"]=><br /> string(0) ""<br /> ["ctype"]=><br /> NULL<br /> ["cid"]=><br /> string(1) "0"<br /> }<br /> [6]=><br /> array(6) {<br /> ["id"]=><br /> string(1) "6"<br /> ["name"]=><br /> string(12) "about"<br /> ["opennew"]=><br /> string(1) "0"<br /> ["url"]=><br /> string(0) ""<br /> ["ctype"]=><br /> NULL<br /> ["cid"]=><br /> string(1) "0"<br /> }<br /> [7]=><br /> array(6) {<br /> ["id"]=><br /> string(1) "7"<br /> ["name"]=><br /> string(12) "tehui"<br /> ["opennew"]=><br /> string(1) "0"<br /> ["url"]=><br /> string(0) ""<br /> ["ctype"]=><br /> NULL<br /> ["cid"]=><br /> string(1) "0"<br /> }<br />}
function nav_list(){<br /> $navlist = get_navigator();<br /> sort($navlist['nav']);<br /> //$navlist['nav'] 就是取出所有数据 ,上面用sort排了下序<br /> foreach($navlist['nav'] as $k=>$v)<br /> {<br /> if($v['cid'] != 0)<br /> {<br /> //插入到一个son成员里<br /> $l[$v['cid']]['son'][] = $v;<br /> <br /> }else{<br /> $l[$v['id']] = $v;<br /> }<br /> }<br /> foreach($l as $k=>$v){<br /> if($v['son']){<br /> //消除son成员<br /> $tmp[] = array_diff_key($v,array('son'=>''));<br /> foreach($v['son'] as $key=>$val){<br /> //再一个foreach 放到其后面<br /> $tmp[] = $val;<br /> }<br /> }else{<br /> $tmp[] = $v;<br /> }<br /> }<br /> return $tmp;<br />}<br />
select id, ctype, cid, name, ifshow, vieworder, opennew, url, type from vm_nav where ifshow and cid=0<br />union<br />select cid as id, ctype, cid, name, ifshow, vieworder, opennew, url, type from vm_nav where ifshow and cid>0<br />order by id