2010-03-07 19 views
8

Oto, co próbuję zrobić: - Potrzebuję funkcji, która po przekazaniu jako argument ID (dla kategorii rzeczy) zapewni wszystkie podkategorie i podkategorie -sub kategorie i sub-sub..etc. - myślałem użyć funkcji rekurencyjnej, ponieważ nie wiem liczbę podkategorii swoich podrzędnych podkategorii i tak dalej więc o to, co starałem się zrobić tak dalekofunkcja rekursywna, aby uzyskać wszystkie kategorie podrzędne

function categoryChild($id) { 

    $s = "SELECT * FROM PLD_CATEGORY WHERE PARENT_ID = $id"; 
    $r = mysql_query($s); 

    if(mysql_num_rows($r) > 0) { 

     while($row = mysql_fetch_array($r)) 
      echo $row['ID'].",".categoryChild($row['ID']); 
    } 
    else { 
     $row = mysql_fetch_array($r); 
     return $row['ID']; 
    } 
} 

Jeśli użyję return zamiast echo, nie otrzymam tego samego wyniku. Potrzebuję pomocy w naprawieniu tego lub przepisaniu go od nowa

Odpowiedz

12

Trudno mi było wymyślić twoją funkcję. Myślę, że to zrobi to, co chcesz. Otrzymuje wszystkie dzieci kategorii o identyfikatorze ID $, a także ich dzieci (uzyskując w ten sposób całą podkategorię, efekt podkategorii, który chciałeś).

function categoryChild($id) { 
    $s = "SELECT ID FROM PLD_CATEGORY WHERE PARENT_ID = $id"; 
    $r = mysql_query($s); 

    $children = array(); 

    if(mysql_num_rows($r) > 0) { 
     # It has children, let's get them. 
     while($row = mysql_fetch_array($r)) { 
      # Add the child to the list of children, and get its subchildren 
      $children[$row['ID']] = categoryChild($row['ID']); 
     } 
    } 

    return $children; 
} 

Ta funkcja zwróci:

$var = array(
     'categoryChild ID' => array(
       'subcategoryChild ID' => array(
         'subcategoryChild child 1' => array(), 
         'subcategoryChild child 2' => array() 
       ) 
     ), 
     'anotherCategoryChild ID' => array() # This child has no children of its own 
); 

Zasadniczo zwraca tablicę z ID dziecka i tablicę zawierającą identyfikatory swoich dzieci. Mam nadzieję, że to pomoże.

+0

Dzięki, że pomógł mi też – Umair

4

database tree to multidimensional array

<?php 
function getTree($rootid) 
{ 
    $arr = array(); 

    $result = mysql_query("select * from PLD_CATEGORY where PARENT_ID='$rootid'"); 
    while ($row = mysql_fetch_array($result)) { 
    $arr[] = array(
     "Title" => $row["Title"], 
     "Children" => getTree($row["id"]) 
    ); 
    } 
    return $arr; 
} 
?> 
1
function categoryChild($id) 
{ 
    $s = "SELECT category_id,name FROM proads_categories WHERE parent_id =".$id;  
    $r = mysql_query($s); 
    $children = array(); 
    if(mysql_num_rows($r) > 0) 
    { 
     #It has children, let's get them. 
     while($row = mysql_fetch_array($r)) 
     {   
      #Add the child to the list of children, and get its subchildren 
      $children[$row['category_id']]['nam'] = $row['name']; 
      $arr = categoryChild($row['category_id']); 
      if(count($arr) > 0) 
      { 
       $children[$row['category_id']]['child'] = categoryChild($row['category_id']); 
      } 
     } 
    } 
    return $children; 
} 

Jest to idealne rozwiązanie. Jeśli potrzebujesz, spróbuj tego

4

Jak to zostało poruszone przez @Pawan Sharma, myślałem, że mogę dać również odpowiedź.

Wszystkie podane rozwiązania cierpią na powszechny problem - wykonują zapytania SQL dla każdego dziecka. Np. Jeśli na drugim poziomie znajduje się 100 obiektów potomnych, wówczas zostanie wykonane 100 zapytań, podczas gdy w rzeczywistości można je wykonać za pomocą zapytania za pomocą where parent_id in (<list_of_ids>).DB


Próbka:

create table category (
    id   int auto_increment primary key, 
    parent_id int default null, 
    title  tinytext, 
    foreign key (parent_id) references category (id) 
) engine = InnoDB; 

insert into category (id, parent_id, title) values 
    (1, null, '1'), 
    (2, null, '2'), 
    (3, null, '3'), 
    (4, 1 , '1.1'), 
    (5, 1 , '1.2'), 
    (6, 1 , '1.3'), 
    (7, 4 , '1.1.1'), 
    (8, 4 , '1.1.2'), 
    (9, 7 , '1.1.1.1'); 

Oto moje rozwiązanie:

/** 
* @param null|int|array $parentID 
*/ 
function getTree($parentID) { 
    $sql = "select id, parent_id, title from category where "; 
    if (is_null($parentID)) { 
     $sql .= "parent_id is null"; 
    } 
    elseif (is_array($parentID)) { 
     $parentID = implode(',', $parentID); 
     $sql .= "parent_id in ({$parentID})"; 
    } 
    else { 
     $sql .= "parent_id = {$parentID}"; 
    } 

    $tree = array(); 
    $idList = array(); 

    $res = mysql_query($sql); 
    while ($row = mysql_fetch_assoc($res)) { 
     $row['children'] = array(); 
     $tree[$row['id']] = $row; 
     $idList[] = $row['id']; 
    } 
    mysql_free_result($res); 

    if ($idList) { 
     $children = getTree($idList); 
     foreach ($children as $child) { 
      $tree[$child['parent_id']]['children'][] = $child; 
     } 
    } 
    return $tree; 
} 

Z dostarczonych przykładowych danych, robi co najwyżej 5 zapytań, gdy zwanych jako getTree(null) (dla wszystkich wpisy):

select id, parent_id, title from category where parent_id is null 
select id, parent_id, title from category where parent_id in (1,2,3) 
select id, parent_id, title from category where parent_id in (4,5,6) 
select id, parent_id, title from category where parent_id in (7,8) 
select id, parent_id, title from category where parent_id in (9) 

Wywołany jako getTree(4), 3 zapytania są wykonywane:

select id, parent_id, title from category where parent_id = 4 
select id, parent_id, title from category where parent_id in (7,8) 
select id, parent_id, title from category where parent_id in (9) 
1
function breadCrumb($id) 
{ 
    $ar = array(); 
    $result = mysql_query("SELECT * FROM groups WHERE ParentID = '$id'"); 
    if(mysql_num_rows($result) > 0) 
    { 
     while($row = mysql_fetch_object($result)) 
     { 
      $ar[] = $row->DBGroupID; 
      $r = mysql_query("SELECT * FROM groups WHERE ParentID = '".$row->GroupID."'"); 
      if(mysql_num_rows($r) > 0) 
       $ar = array_merge($ar, breadCrumb($row->GroupID, 1)); 
     } 
    } 
    return $ar; 
} 
0

Korzystanie z funkcji Prestashop:

public function getRecursiveChildren() { 

    $subCategories = $this->recurseLiteCategTree(); 
    //print_r($subCategories); 


    $my_tab = array(); 

    foreach ($subCategories['children'] as $subc) { 
     $my_tab[] = $subc['id']; 
     foreach ($subc['children'] as $subc2) { 
      $my_tab[] = $subc2['id']; 
      foreach ($subc2['children'] as $subc3) { 
       $my_tab[] = $subc3['id']; 
       foreach ($subc3['children'] as $subc4) { 
        $my_tab[] = $subc4['id']; 
       } 
      } 
     } 
    } 
    $my_tab [] = $this->id; 

    return $my_tab; 
} 

to można łagodzić stosując recursivity ale nie ma na to czasu dzisiaj: '(

1
<?php  
require('db/dbconnect.php'); 

$user_id='triD-100'; 
$sql="select * from ajent_joining where sponser_id='".$user_id."'"; 
$qR=mysql_query($sql); 
while($rowD=mysql_fetch_assoc($qR)){  
    echo $childId=$rowD["user_id"]; 
    echo "<br/>"; 
    categoryChild($childId);  
    } 

    function categoryChild($childId) { 

    $s = "select user_id from ajent_joining where sponser_id='".$childId."'"; 
    $r = mysql_query($s); 
    if(mysql_num_rows($r) > 0) { 

     while($row = mysql_fetch_array($r)) { 

      echo $childId=$row["user_id"]; 
      echo "<br/>"; 
      categoryChild($childId); 
    } 
} 

} 


?> 
Powiązane problemy