CodeIgniter Forums
[solved] Making a parentlist of id - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: [solved] Making a parentlist of id (/showthread.php?tid=66803)



[solved] Making a parentlist of id - wolfgang1983 - 12-05-2016

I have a forum table.

I need to make a parent list of all my pids.

Lets say if I need to make parentlist for fid 36 As shown in image below Then then it should return PID numbers only 35, 34, 1 not to return 0 if pid is 0


[Image: 34JIC85bBynS.png]



Example

I have tried


PHP Code:
public function make_parent_list() {

$this->db->where('fid''36');
               
$query 
$this->db->get('forum');
        
$pid_list 
= array();

$pid_list[] = $query->row()->fid;
        
foreach ($query->result() as $category) {
        
$this
->db->where('pid'$category->pid);
            
$query 
$this->db->get('forum');

if (
$query->num_rows() > 0) {

$pid_list[] = $query->row()->pid;

}

}

return 
$pid_list;




Question How can I get it to properly make the parentlist. Can not seem to return 35, 34, 1  if fid 36


RE: Making a parentlist of id - InsiteFX - 12-05-2016

Revisiting the multilevel menu in PHP with some additional code


RE: Making a parentlist of id - wolfgang1983 - 12-05-2016

(12-05-2016, 04:01 AM)InsiteFX Wrote: Revisiting the multilevel menu in PHP with some additional code

Don't think is what I am after to not need to create a menu.


RE: Making a parentlist of id - Wouter60 - 12-05-2016

Try making a recursive function.
Maybe this will work:

PHP Code:
private function get_parents($fid, &$parents = array())
{
 
  $this->db->where('fid'$fid);
 
  $this->db->where('pid >'0);
 
  $query $this->db->get('forum');
 
  if ($query->num_rows() > 0) {
 
     foreach($query->result() as $row) {
 
        $parents[] = $row->pid;
 
        $this->get_parents($row->fid,$parents);
 
     }
 
    
   
return $parents




RE: Making a parentlist of id - wolfgang1983 - 12-05-2016

I now have tried this

PHP Code:
public function make_parent_list($fid) {

$sql "SELECT * FROM forum WHERE fid  = '" $fid "'"
     
      
$query 
$this->db->query($sql);

$arr = array();
     
      
foreach ($query->result() as $row) {

if (
$row->pid) {

$arr[] = $row->pid;

$arr[] = $this->make_parent_list($row->pid);

}
         
      
}

return 
$arr;



But the implode throws error here

Code:
A PHP Error was encountered
Severity: Warning
Message: implode(): Invalid arguments passed
Filename: forums/Forum_management.php

Echo out put is 34,Array


PHP Code:
$results $this->make_parent_list('36');

foreach (
$results as $result) {
echo 
implode(','$result);




RE: Making a parentlist of id - Paradinight - 12-05-2016

(12-05-2016, 08:53 PM)wolfgang1983 Wrote: I now have tried this

PHP Code:
public function make_parent_list($fid) {

$sql "SELECT * FROM forum WHERE fid  = '" $fid "'"
 
  
$query 
$this->db->query($sql);

$arr = array();
 
  
foreach ($query->result() as $row) {

if (
$row->pid) {

$arr[] = $row->pid;

$arr[] = $this->make_parent_list($row->pid);

}
 
  
}

return 
$arr;



But the implode throws error here

Code:
A PHP Error was encountered
Severity: Warning
Message: implode(): Invalid arguments passed
Filename: forums/Forum_management.php

Echo out put is 34,Array


PHP Code:
$results $this->make_parent_list('36');

foreach (
$results as $result) {
echo 
implode(','$result);

PHP Code:
string implode string $glue , array $pieces 

1. $result is a int or an array
2. you forgot to escape $fid
3. http://stackoverflow.com/questions/29384548/php-how-to-build-tree-structure-list


RE: Making a parentlist of id - wolfgang1983 - 12-05-2016

Solution found


PHP Code:
function getCategoriesByParentId($fid) {
     
   $category_data = array();

     
   $category_query $this->db->query("SELECT * FROM forum WHERE fid = '" . (int)$fid "'");

     
   foreach ($category_query->result() as $category) {
     
       
            $category_data
[] = array(
     
           'pid' => $category->pid,
     
       );

     
       $children $this->getCategoriesByParentId($category->pid);

     
       if ($children) {
     
           $category_data array_merge($children$category_data);
     
                 
        
}

     
   return $category_data;
    } 


And

PHP Code:
      $all_categories $this->getCategoriesByParentId(36);

        $arr = array();

        foreach ($all_categories as $category) {
            $arr[] = $category['pid'];
        }

        echo implode(','$arr);