2013-10-16 3 views
0

아래 패턴에서 슬러그는 $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; 
    } 
    } 

I이 적층하고는, 임의의 광 인정된다.

답변

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

Ferweda 나는 1 레벨 아래로 완벽하게 작동한다. 그러나 레벨 3에서는 레벨 1 슬러그가 부모 슬러그로만 사용됩니다. 코드 http://pastebin.com/U0ZdyyEJ 및 결과 http://prntscr.com/1xnps6 – YahyaE

+0

괜찮습니다. 조금 더 편집했습니다. http://pastebin.com/PEH3Wkq8 빛을 가져 주셔서 감사합니다! – YahyaE

+0

키는 재귀 함수에 현재 슬러그를 전달하는 것입니다. 이 머신에 서버가 없기 때문에, 로직에 대해 깊이 생각하지 않고서도 mo에서 작업 버전을 만들 수는 없습니다. –