Current location: Hot Scripts Forums » Programming Languages » PHP » how to make this recursion ?


how to make this recursion ?

Reply
  #1 (permalink)  
Old 06-23-09, 12:54 PM
m_abdelfattah m_abdelfattah is offline
Newbie Coder
 
Join Date: Sep 2004
Location: Alexandria, Egypt.
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
how to make this recursion ?

I've Mysql Table with these data :

id parent_id name
1 0 Sub 1
2 1 sub 2
3 2 sub 3

I want to make function that I can pass `id` to it , and it return an array with all parents,
for example :
function get_parent_ids(3) {
..
..
return (2, 1, 0)
}

so, how can I write this code ??

and what if I want to get the childs ?
for example :
function get_parent_ids(0) {
..
..
return (1, 2, 3)
}


Thanks for your time !!

Last edited by m_abdelfattah; 06-23-09 at 01:07 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 06-23-09, 04:45 PM
m_abdelfattah m_abdelfattah is offline
Newbie Coder
 
Join Date: Sep 2004
Location: Alexandria, Egypt.
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
I found a solution for 1st one :
PHP Code:

function get_parent_districts($district_id) {

        
$parent_ids = array();
        
$exclude = array();
        while(
$district_id !== 0) {
            
$query $this->db->query("SELECT `parent_id` FROM `districts` WHERE `district_id`=" $this->db->escape($district_id) . "") or die(mysql_error());
            
$row $query->row_array();
            
$parent_ids[] = $row['parent_id'];
            
$district_id $row['parent_id'];
            if(
$district_id === "0") {
                break;
            }
        }
        return 
$parent_ids;
    } 
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump


All times are GMT -5. The time now is 08:40 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.