php 三級導航及無限級擴充的一些心得

來源:互聯網
上載者:User

標籤:啟動   cti   數組   迴圈   記錄   get   結束   turn   while   

前段時間給個客戶做網站,這個導航分的比較細;自己在做的時候有些心得。記錄下來 供大家參考

資料庫導航表的對應關係  

ID = 自增 唯一  

PID = 二級的PID 對應一級 ID   三級的PID 對應二級的ID

.....以此類推 

建立之後取出來的時候 就是

Array (

[0] => Array (

        [cat_id] => 1

        [cat_pid] => 0

)

[1] => Array (

        [cat_id] => 2

        [cat_pid] =>1

)

[2] => Array (

        [cat_id] => 3

        [cat_pid] =>2

)

)

就是這樣的 那麼為了把各個導航之間的關係弄明白 我們就要他變成這樣

Array (

        [0] => Array (  

                    [cat_id] => 1  

                    [cat_pid] =>0

                    [has_son] => Array (

                                      [0] => Array (

                                                     [cat_id] => 2

                                                     [cat_pid] => 1

                                                     [has_son2] => Array (

                                                                         [0] => Array (

                                                                         [cat_id] => 3

                                                                         [cat_pid] => 2                                                                     )

                                                            )

                                                   )

                   )

)

要顯示成這種明確的層級關係  那麼首先要將資料取出來(......)

比如 $get 這個是資料庫取出來的資料的話 

$cat = array();
$conut = 0;

function  方法名(){

   if (!empty($getCate)) {

     while ($getCate) {  //啟動一個迴圈 用摘除的方式來完成組合

                   //產生所有的一級分類

                   if ($conut == 0) {

                          foreach ($getCate as $k => $v) {

                                  if ($v[‘cat_pid‘] == 0) {

                                             $cat[] = $v;

                                             unset($getCate[$k]); //講收集到的一級導航從數組中摘除

                                   }

                            }

                     }

                 $conut++;

                 //產生二級分類

                 if (!empty($cat)) {

                       foreach ($cat as $pk => $pv) {

                                   foreach ($getCate as $sk => $sv) {

                                              if ($sv[‘cat_pid‘] == $pv[‘cat_id‘]) {

                                                          $cat[$pk][‘has_son‘][] = $sv;

                                                          unset($getCate[$sk]);

                                                }

                                      }

                            }

                 }

                //產生三級分類

               if (!empty($cat)) {

                    foreach ($cat as $pk => $pv) {

                                if (!empty($pv[‘has_son‘])) {

                                        foreach ($pv[‘has_son‘] as $sk2 => $sv2) {

                                                      foreach ($getCate as $sk3 => $sv3) {

                                                                   if ($sv3[‘cat_pid‘] == $sv2[‘cat_id‘]) {

                                                                               $cat[$pk][‘has_son‘][$sk2][‘has_son2‘][] = $sv3;

                                                                               unset( $getCate [$sk3] );

                                                                   }

                                                       }

                                            }

                                 }

                        }

              }

 

        }

    }  

}

以上是產生3級導航的方法  需要四級  五級的話 可以接著繼續往下寫.....

查詢的話 可以寫在方法中  產生三級分類結束後 寫

$cat_id 是你的索引 比如說是 一級的ID  二級的PID  之類的

           if ( !empty($cat_id) ){

                   //一級導航下的子類

                  foreach ( $cat as $k => $v ){

                          if ( $cat_id == $v[‘cat_id‘] ){

                                  if ( !empty( $v[‘has_son‘] )){

                                            return $cat[$k][‘has_son‘];

                                  }else{

                                           return array();

                                 }

                          }

                  }

                  //二級導航下的子類

                   foreach ( $cat as $pk => $pv ){

                         if (!empty($pv[‘has_son‘])){

                                 foreach ($pv[‘has_son‘] as $sk => $sv){

                                          if ($sv[‘cat_id‘] == $cat_id){

                                                  return  $sv[‘has_son2‘];

                                        }

                                 }

                         }

                   }

          }

如果這是四級 五級的話 就按照樣式繼續填寫

php 三級導航及無限級擴充的一些心得

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.