• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[solved] Making a parentlist of id

#1
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
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply

#2
Revisiting the multilevel menu in PHP with some additional code
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#3
(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.
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply

#4
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

Reply

#5
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);

There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply

#6
(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/29384...cture-list
Reply

#7
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); 
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.