Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Any one have idea how to do the following:

I have the following mysql query :

client 1026 has two clients under him: 1056 (on the left ) & 1497 ( on the right ) and each of client 1056 and client 1497 has two other clients under them, and so on

now i want to make a loop to collect all the clients under the client 1026

i have this mysql query

$sql_query="select id from rev_r_clients WHERE parent_client_id='1026'";
$res = mysql_query($sql_query);
$ids=array();
while($row = mysql_fetch_object($res)){
    $ids[]=$row->id;
}
$ids=array_filter($ids);
foreach($ids as $id){
    echo $id;
    echo '<br>';
    $sql_query="select id from rev_r_clients WHERE parent_client_id='$id'";
    $res = mysql_query($sql_query);
    $ids=array();
    while($row = mysql_fetch_object($res)){
        $ids[]=$row->id;
    }
    $ids=array_filter($ids);
    foreach($ids as $id){
        echo $id;
    }
}

it return

1056

106410801497

15051521

now how can i make this query get array result ( like 1056,1497 ) and then use foreach loop to get the result and the result of the result of the result etc.. until there is no more result?

You can use mysql , mysqli , PDO all i want is to accomplish the request

share|improve this question
    
So why aren't you using mysql_fetch_array()? –  Eisa Adil Dec 19 '13 at 17:20
    
@EisaAdil - what's wrong with mysql_fetch_object? –  andrewsi Dec 19 '13 at 17:23
2  
If you've got an arbitrarily deep tree, then the only way I can think of to do what you want is through recursion - you'll need to get a client's sub-clients; then check if any of those have their own sub-clients, and so on. –  andrewsi Dec 19 '13 at 17:24
    
andrewsi , how to do that :D ? –  user3117183 Dec 19 '13 at 17:26
    
@user3117183 - the standard way would be to create a function that takes a client's ID as the parameter; that queries for sub-clients of that client. If there are any, it calls itself, passing in each sub-client's ID in turn. –  andrewsi Dec 19 '13 at 17:39

1 Answer 1

up vote 3 down vote accepted
$ids=array();
while($row = mysql_fetch_object($res)){
ids[]=$row->id;
 }
$ids=array_filter($ids);
/*To remove empty array*/
foreach($ids as $id){
echo $id;
 }
share|improve this answer
    
ok now how can we do the foreach loop ?to look for the clients under each client id? –  user3117183 Dec 19 '13 at 17:54
    
foreach($ids as $id){} –  Rahi Dec 19 '13 at 17:59
2  
You should explain these things not just give code –  Sterling Archer Dec 19 '13 at 18:00
    
look define a array variable first .Then store each element in that array. Then loop through foreach ok –  Rahi Dec 19 '13 at 18:02
    
ok updated the code, how to make it loop till empty id? –  user3117183 Dec 19 '13 at 18:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.