1

I have this mysql query that i want to loop till there is no more result found in the table,

$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;

}

and what i want is loop the same query again and again like this:

$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;

$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;

}
}

this last code will loop the query twice but the problem i have is that i want to loop it till there is no more result found

$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;

<--! REPEAT SAME CODE IN HERE AGAIN AND AGAIN AND AGAIN --> }

looping 3 times:

$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;

$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;

$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;

}
}
}

so how do i repeat the code infinite times till there is no result in the database

4
  • 2
    You want 'recursion' and 'tree structure' Commented Dec 20, 2013 at 20:56
  • No I think he wants just to fetch all rowns on that table and the code just did it once... Commented Dec 20, 2013 at 21:05
  • Yes jessica thats what i want, any way , example how to do it right? Commented Dec 20, 2013 at 21:07
  • So it sounds like you are trying to represent a tree structure in your database table. You probably need to think about your schema a little more. You should not need to recursively query a the table to get this data, but rather be able to get to the data in a single query if you have a properly designed table schema. See these links for more information: mikehillyer.com/articles/managing-hierarchical-data-in-mysql stackoverflow.com/questions/5916482/… Commented Dec 20, 2013 at 22:04

1 Answer 1

0

A quick solution using recursion:

function getChildIds($id, &$count) { // Note that $count is passed in as a reference (the & before the $count)
    ++$count;
    echo $id;
    $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)){
        if ($row->id) { // Checking for false-like values here to remove the need for array_filter
            $ids[] = $row->id;
        }
    }

    foreach ($ids as $next) { // using foreach instead of array_walk as we cannot pass a reference into array_walk without raising a warning (or fatal error, depending on PHP version)
        getChildIds($next, $count);
    }
}

$count = 0;
getChildIds($firstId, $count);
// $count now has the number of times the function was called
echo $count;

Note, though, that the mysql_* functions are deprecated and you should instead use mysqli or PDO.

Sign up to request clarification or add additional context in comments.

10 Comments

You did not echoed the id!
echo $id; is the first line of the function.
Ooh sorry, missed it... but it should be inside the if... since at least one will not have data! and you will print it anyway right?!
is there a way to count the ids echoed?
@JorgeCampos Nope, the if ($row->id) check ensures the function doesn't get called if the $id retrieved is null (or zero).
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.