0

Hi
currently trying to fighure out how i would need to solve this task...

I have a database table which has lots of records and each record will have a parent id attached...

e.g.

Table_1
-----------------------------------
id  name    parent
1   test1   4
2   test2   3
3   test3 
4   test4   2
5   test5   1
6   test6   
7   test7   4
8   test8   5

What i want to do in php is when i am given an id e.g. 4 then i want to get all the ids from the db that are childrens of 4.
it could be 0 to many....

so result in this case should be

1, 7, 5, 8

because 1 is child of 4, 7 is child of 4, 5 is child of 1, 8 is child of 5

so how would i construct this array?

this table is in mysql table...

i just want to create an array which stores all the ids of the childrens

any help is much appreciated

Thanks

3
Contributors
3
Replies
22
Views
3 Years
Discussion Span
Last Post by t_thakar
0

Maybe something like this

$parent = $_POST['parent'];

$query="select $parent from table";
0

Try this

<?php
mysql_connect("localhost", "root", "") or die("Connection failed! " . mysql_error());
mysql_select_db("your_database") or  die("Error in database selection" . mysql_error());

 function  qryTry ($parentId) {
    $find_query = "SELECT Id  FROM  Table_1 WHERE parent = '$parentId' ";
    $strSQL = mysql_query($find_query);
    while($row = mysql_fetch_array($strSQL)) {
        echo $row['id'] . "<br />";
    }
    $strSQL2 = mysql_query($find_query);
    while($row = mysql_fetch_array($strSQL2)) {
        qryTry ($row['id']);
    }
 }
 $parentValue = 4;
 qryTry ($parentValue);

mysql_close();
?>
0

hi dany12 ... thanks for your reply but it doesnot work

hi Bachov Varghese the qry seems to work :)

wow

is it possible for you to explain the code if possible...

if not its fine too..

Big thank you for this...

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.