2015-01-01 4 views
0

새해 복 많이 받으세요! :)실행 기능

나는 PHP로 비교적 새로운 그리고 난 부모 범주의 차일을 얻을 수있는 함수를 작성하는 것을 시도하고, 그리고 다시 기능을 다시 사용 차일드 카테고리의 차일드를 가져옵니다.

그러나 나는 기대했던대로 작동하지 않으며 실제로 그 이유를 알 수 없습니다. 여기서 나를 도와 주시길 바랍니다.

감사합니다. 로이

$Parents = 

Array 
(
[0] => Array 
    (
     [category_id] => 3 
     [parent_id] => 1 
     [name] => PC Onderdelen 
     [is_active] => 1 
     [position] => 2 
     [level] => 1 
    ) 

[1] => Array 
    (
     [category_id] => 11 
     [parent_id] => 1 
     [name] => test 
     [is_active] => 1 
     [position] => 3 
     [level] => 1 
    ) 

[2] => Array 
    (
     [category_id] => 14 
     [parent_id] => 1 
     [name] => Rootcat3 
     [is_active] => 1 
     [position] => 4 
     [level] => 1 
    ) 
) 

기능

public function getchilds($parents) 
{ 
    $subcategories = array(); 

    foreach ($parents as $parent) 
     { 
      $parentid = $parent['category_id']; 

      $sql = "SELECT * FROM categories WHERE parent_id = $parentid"; 

      $db = new DB ('novacpos'); 
      $result = $db->query($sql); 

      while ($rows = mysqli_fetch_assoc($result)) 
       { 
        $subcategories[] = $rows; 
       } 
      $parent["children"] = $subcategories; 
      unset ($subcategories); 
      $parents1[] = $parent; 
     } 
    return $parents1; 
} 

결과

Array 
(
[0] => Array 
    (
     [category_id] => 3 
     [parent_id] => 1 
     [name] => PC Onderdelen 
     [is_active] => 1 
     [position] => 2 
     [level] => 1 
     [children] => Array 
      (
       [0] => Array 
        (
         [category_id] => 4 
         [parent_id] => 3 
         [name] => Moederborden 
         [is_active] => 1 
         [position] => 2 
         [level] => 2 
        ) 

       [1] => Array 
        (
         [category_id] => 6 
         [parent_id] => 3 
         [name] => Behuizingen 
         [is_active] => 1 
         [position] => 1 
         [level] => 2 
        ) 

       [2] => Array 
        (
         [category_id] => 8 
         [parent_id] => 3 
         [name] => Laptops 
         [is_active] => 1 
         [position] => 3 
         [level] => 2 
        ) 

       [3] => Array 
        (
         [category_id] => 9 
         [parent_id] => 3 
         [name] => Muizen 
         [is_active] => 1 
         [position] => 4 
         [level] => 2 
        ) 

      ) 

    ) 

[1] => Array 
    (
     [category_id] => 11 
     [parent_id] => 1 
     [name] => test 
     [is_active] => 1 
     [position] => 3 
     [level] => 1 
     [children] => Array 
      (
       [0] => Array 
        (
         [category_id] => 13 
         [parent_id] => 11 
         [name] => Test2 
         [is_active] => 1 
         [position] => 1 
         [level] => 2 
        ) 

      ) 

    ) 

[2] => Array 
    (
     [category_id] => 14 
     [parent_id] => 1 
     [name] => Rootcat3 
     [is_active] => 1 
     [position] => 4 
     [level] => 1 
     [children] => Array 
      (
       [0] => Array 
        (
         [category_id] => 15 
         [parent_id] => 14 
         [name] => Extracat1 
         [is_active] => 1 
         [position] => 1 
         [level] => 2 
        ) 

      ) 

    ) 

) 

결과 그래서 내가 생각 print_r ($parent["children"]);

Array 
(
[0] => Array 
    (
     [category_id] => 4 
     [parent_id] => 3 
     [name] => Moederborden 
     [is_active] => 1 
     [position] => 2 
     [level] => 2 
    ) 
[1] => Array 
    (
     [category_id] => 6 
     [parent_id] => 3 
     [name] => Behuizingen 
     [is_active] => 1 
     [position] => 1 
     [level] => 2 
    ) 

[2] => Array 
    (
     [category_id] => 8 
     [parent_id] => 3 
     [name] => Laptops 
     [is_active] => 1 
     [position] => 3 
     [level] => 2 
    ) 

[3] => Array 
    (
     [category_id] => 9 
     [parent_id] => 3 
     [name] => Muizen 
     [is_active] => 1 
     [position] => 4 
     [level] => 2 
    ) 

) 
Array 
(
[0] => Array 
    (
     [category_id] => 13 
     [parent_id] => 11 
     [name] => Test2 
     [is_active] => 1 
     [position] => 1 
     [level] => 2 
    ) 

) 
Array 
(
[0] => Array 
    (
     [category_id] => 15 
     [parent_id] => 14 
     [name] => Extracat1 
     [is_active] => 1 
     [position] => 1 
     [level] => 2 
    ) 

) 

의 : (하지만 작동하지 않습니다)

,
public function getchilds($parents) 
{ 
    $subcategories = array(); 

    foreach ($parents as $parent) 
     { 
      $parentid = $parent['category_id']; 

      $sql = "SELECT * FROM categories WHERE parent_id = $parentid"; 

      $db = new DB ('novacpos'); 
      $result = $db->query($sql); 

      while ($rows = mysqli_fetch_assoc($result)) 
       { 
        $subcategories[] = $rows; 
       } 
      $parent["children"] = $subcategories; 
      unset ($subcategories); 

      getchilds($parent["children"]); <----------- 

      $parents1[] = $parent; 
     } 

    return $parents1; 
} 
+0

당신은'getchilds를 호출하고을()'이 필요로가, 단지 그것을 다시 작품, 결과로 아무 것도하지 않으면 반환됩니다. – Barmar

+0

또한 클래스에 있으면'self :: getchilds()'를 호출해야합니다. – Barmar

+0

"$ parents1 [] = $ parent;"아래의 행으로 이동 했으므로 함수 내부에서해야 할 일을 선언했습니다. 맞습니까? –

답변

0
class myModel{ 
public function get_categories(){ 

    $sql = "SELECT * FROM categories"; 

      $db = new DB ('novacpos'); // You should use a singleton pattern for this 
      $result = $db->query($sql); 
      $categories = array(); 

      while ($rows = mysqli_fetch_assoc($result)) 
       { 
        $categories[] = $rows; 
       } 

    return $categories; 
} 

public function organize_categories($categories) { 
     $resultParents = array(); 
     $resultChildren = array(); 

     foreach ($categories as $row) { 
      if(empty($row['parent'])) 
       $resultParents[] = $row; 
      else 
       $resultChildren[] = $row; 
     } 

     foreach ($resultParents as &$parent){ 
      $parent['children'] = $this->_organize_category($resultChildren, $parent['category_id']); 
     } 
     unset($parent); 
     return $resultParents; 
    } 

    private function _organize_category($resultChildren, $parent) { 
     $children = array(); 
     foreach ($resultChildren as $child) { 
      if ($child['parent_id'] == $parent){ 
       $child['children'] = $this->_organize_category($resultChildren, $child['category_id']); 
       $children[] = $child; 
      } 
     } 

     return $children; 
    } 
} 

마지막 :

$categories = $myModel->get_categories(); 
$organized_categories = $myModel->organize_categories($categories); 
var_dump($organized_categories); 
0

그것은 절차 적 스타일로 구현,하지만 당신은

$db = new mysqli('localhost', 'root', 'mysql', 'test'); 

$parents = []; 
$result = $db->query('SELECT * FROM categories WHERE category_id IN (0,1)'); 

while ($row = $result->fetch_assoc()) 
{ 
    $parents[] = $row; 
} 


function getchilds($parents, $db) 
{ 
    foreach ($parents as &$parent) 
    { 
     $result = $db->query("SELECT * FROM categories WHERE parent_id = {$parent['category_id']}"); 

     if ($result && $result->num_rows > 0) 
     { 
      while ($row = $result->fetch_assoc()) 
      { 
       $parent['children'][] = $row; 
      } 

      $parent['children'] = getchilds($parent['children'], $db); 
     } 
    } 

    return $parents; 
} 

echo '<pre>'; 
print_r(getchilds($parents, $db)); 
+0

답해 주셔서 감사합니다.내 코드를 다음과 같이 변경했습니다. (코드가 작동하지 않습니다.) –

+0

public function getchilds ($ parents) \t { \t $ subcategories = array(); \t \t \t \t 의 foreach은 ($ 부모로서 부모 $) \t \t \t \t { \t \t \t $ parentid = $ 부모 [ 'CATEGORY_ID']; \t \t \t \t \t \t \t \t \t $ SQL = "WHERE PARENT_ID = $ parentid 카테고리 SELECT *"; \t \t \t \t \t \t \t \t \t \t $의 dB = 새로운 DB ('novacpos'); \t \t \t \t $ result = $ db-> query ($ sql); \t \t \t \t \t \t \t \t \t \t 동안 ($ 행 = mysqli_fetch_assoc ($ 결과)) \t \t \t \t \t { \t \t \t \t \t \t $ 부모 [ "어린이"] [] = $ 행; \t \t \t \t \t \t} \t \t \t $ 부모 [] = $ 상위; \t \t \t \t $ parent [ "children"] = self :: getchilds ($ parent [ "children"]); \t \t \t \t \t \t \t} \t \t 반환 $ 부모; \t} –

+0

결과보기 : view-source : http : //pos.admin-x.com/pages/productgroups/classtest.php. –