0

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

6
  • So why aren't you using mysql_fetch_array()? Commented Dec 19, 2013 at 17:20
  • @EisaAdil - what's wrong with mysql_fetch_object? Commented Dec 19, 2013 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. Commented Dec 19, 2013 at 17:24
  • andrewsi , how to do that :D ? Commented Dec 19, 2013 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. Commented Dec 19, 2013 at 17:39

1 Answer 1

3
$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;
 }
Sign up to request clarification or add additional context in comments.

3 Comments

ok now how can we do the foreach loop ?to look for the clients under each client id?
foreach($ids as $id){}
look define a array variable first .Then store each element in that array. Then loop through foreach ok

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.