2013-10-16 55 views
0

在我的下面的模式中,slu are被保存爲 $parent_slug . '_' . url_title($posted_category_name)在遞歸循環中使用父數據

所以,如果id_category = 1的蛞蝓的變化,例如在Acura => My acura,這意味着所有的潛艇蛞蝓必須按照循環過程中被改變,再次與$parent_slug . '_' . url_title($posted_category_name) ...怎麼會呢?

在下面的數組之後,我添加了遞歸函數。

數據陣列

Array 
(
    [0] => Array 
     (
      [id_category] => 1 
      [id_parent] => 0 
      [level] => 1 
      [category] => Acura 
      [slug] => acura 
      [children] => Array 
       (
        [0] => Array 
         (
          [id_category] => 3 
          [id_parent] => 1 
          [level] => 2 
          [category] => Vigor 
          [slug] => acura_vigor 
          [children] => Array 
           (
            [0] => Array 
             (
              [id_category] => 5 
              [id_parent] => 3 
              [level] => 3 
              [category] => LS 
              [slug] => acura_vigor_ls 
              [children] => Array 
               (
                [0] => Array 
                 (
                  [id_category] => 6 
                  [id_parent] => 5 
                  [level] => 4 
                  [category] => Alt 1 
                  [slug] => acura_vigor_ls_alt-1 
                 ) 

                [1] => Array 
                 (
                  [id_category] => 7 
                  [id_parent] => 5 
                  [level] => 4 
                  [category] => Alt 2 
                  [slug] => acura_vigor_ls_alt-2 
                 ) 

               ) 

             ) 

           ) 

         ) 

       ) 

     ) 

) 

rebuild_category_slug_index($ target_map)

function rebuild_category_slug_index($target_table) 
    { 
    $active_table = tr_url_title($target_table); 
    $this->db->table_exists('my_'.$active_table) || show_error(lang('admin|erros|table_not_found'),'500',lang('admin|errors|header500')); 

    $raw_data = $this->category_m->get('my_'.$active_table,array(),$order_str='id_category asc, id_parent asc',array(),'result_array'); // It just returns get all from table. 

    if($raw_data) 
    { 
     $treearr = array('0' => array('children'=> array())); 
     foreach($raw_data as $item) 
     { 
     // Here is the work. I should get $parent_slug and create current slug. 
     // $item['slug'] = $parent_slug . '_' . tr_url_title($item['category']); 
     $treearr[$item['id_category']] = $item; 
     if(!isset($treearr[$item['id_parent']])) $treearr[$item['id_parent']] = array('children'=> array()); 
     $treearr[$item['id_parent']]['children'][] = &$treearr[$item['id_category']]; 
     } 
     $tree = $treearr[0]['children']; 
     unset($treearr); 
     vdebug($tree); // For displaying formatted results 
     //return $tree; 
    } 
    } 

我堆疊與此,任何的光被理解。

回答

1

我會將數據恢復拉出該函數,或創建一個新的遞歸函數。只是爲了讓你開始:

function recursive_rename($data, $parent_slug='') 
    { 
    foreach($data as $item) 
    { 
    // Set the $parent_slug 
    if($imen['level'] = 1) 
     { 
     $item['slug'] = $item['category']; 
     if ($parent_slug == '' || $parent_slug <> $item['category']) 
     { 
     $parent_slug = $item['category'] 
     } 
     } 
    else 
     { 
     $item['slug'] = $parent_slug."_".$item['category']; 
     } 
    if(isset($item[children])) 
     { 
    // call function again 
     $out = recursive_rename($item['children'], $item['slug']); 
     } 
+0

費爾韋達我玩了很多功能..它適用於一個完美的水平。但是在等級3中,只需要一級slu as作爲母體slu。。代碼http://pastebin.com/U0ZdyyEJ和結果http://prntscr.com/1xnps6 – YahyaE

+0

這是好的,我編輯了一點點更多。 http://pastebin.com/PEH3Wkq8感謝您的光! – YahyaE

+0

的關鍵是將當前的slug傳遞給遞歸函數。因爲我沒有這臺機器上的服務器,所以我無法在mo上爲你創建工作版本,對邏輯沒有深入的思考:) –