Question: I am working on a project, where I have a domain `xyz.com`, I have been requested that a subdomain example `abc.xyz.com` should point to website which has ipaddress
example `http://199.152.57.120/client/` and when a visitor browse `abc.xyz.com` it should open the website hosted on `http://199.152.57.120/client/` but by hidding this ip address the visitor should always see `abc.xyz.com`.

I also need to host another website to `xyz.com`
domain which is registered with *x* company and webhosting is taken for *z* company both different.
It is something similar to Reseller business where Reseller company assign a website to their client on their custom domain.

Answer: 
Here's the solution for [image2](http://genxcoders.in/demo/TDirectory/2.png)

`echo '<select>';`

`structureTree(0,0);`

`echo '</select>';`

`function structureTree($deptParentId, $level){`

```
$result = getDataFromDb($deptParentId); // change this into SQL
foreach($result as $result){
    echo '<option value="'.$deptParentId.'">'.str_repeat('-',$level).$result['deptName'].'</option>';
    structureTree($result['deptId'], $level+2);
}

```

`}`

`// mocking database record
function getDataFromDb($deptParentId)
{`

```
$data = array(
    array(
        'deptId' => 1,
        'deptName' => 'IT',
        'deptParentId' => 0,
    ),
    array(
        'deptId' => 2,
        'deptName' => 'Human Resources',
        'deptParentId' => 1,    
    ),
    array(
        'deptId' => 3,
        'deptName' => 'Opreational',
        'deptParentId' => 1,    
    ),
    array(
        'deptId' => 4,
        'deptName' => 'Networking Department',
        'deptParentId' => 1,
    ),
    array(
        'deptId' => 5,
        'deptName' => 'Software Development',
        'deptParentId' => 1,    
    ),
    array(
        'deptId' => 6,
        'deptName' => 'Mobile Software Development',
        'deptParentId' => 5,    
    ),
    array(
        'deptId' => 7,
        'deptName' => 'ERP',
        'deptParentId' => 5,    
    ),
    array(
        'deptId' => 8,
        'deptName' => 'Product Development',
        'deptParentId' => 5,    
    ),
);
$result = array();
foreach ($data as $record) {
    if ($record['deptParentId'] === $deptParentId) {
        $result[] = $record;
    }
}

return $result;

```

`}`

regarding [1.png](http://genxcoders.in/demo/TDirectory/1.png) Here's the `SQL` based on your sample db table

`SELEC dep.deptId As DepId`

`,dep.deptName As DepartmenName`

`,parent.deptId As ParentId`

`,parent.deptName As ParentDepartmentName`

`FROM tddept As dep`

`JOIN tddept As parent ON parent.deptParentId = dep.deptId;`
recursion is the solution for this problem. While the parent still has a child then it keeps querying until there's no child left.

e.g 

One
 .One1
 .One2
 .One3

Here's the php application

`function structureTree($depParentId = 0){`

```
    $result = queryFunction("SELECT *FROM 
                          tddept WHERE deptParentId = '$depParentId'");
   foreach($results as $result){ 
         echo $result['deptName'].'<br/>';
         structureTree($resultp['deptParentId']);
   }     

```

`}`

`structureTree();`