首页 >后端开发 >php教程 >求好的步骤~

求好的步骤~

WBOY
WBOY原创
2016-06-13 12:09:15822浏览

求好的方法~?
数据如:

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 />}


我用了一个笨办法:先foreach吧cid  插入到一个son成员里 然后再一个foreach 放到其后面,描述的不清楚,代码如下:

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

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn