简体   繁体   中英

Two level adjacency list using mysql and php

Im creating a new task and assign the parentid as 100 to that task, once created the newtask id is 101..

Now again i create a new task and assign 100 or 101 to that new id which is being created(102)

Now when i create new task whose id is 104 then it should give me an option to select 100,101 as the parent task id but not 102 since 102 is connected to 101 and 101 is connected to 100 parent id

How i can do this two level access? My table structure is taskid ,parenttask id where task id is autoincrement ,primary key

So while getting the values from database how can i filter. Below is my code.

<select name="mastertaskid" size="1" id="mastertaskid" STYLE="width: 350px" >
<option>Select One</option>

<?php 

$q = "SELECT * FROM Tasks order by Task_id asc";        
$r = mysql_query ($db, $q

if (mysql_num_rows($r) > 0) {
  while ($row = mysql_fetch_array ($r, MYSQLI_NUM)) {
    echo "<option value=\"$row[0]\"";
    // Check for stickyness:
    if (isset($return_val['ParentTask_Id']) 
       && ($return_val['ParentTask_Id'] == $row[0]) ) 
       echo ' selected="selected"'; 
    echo ">$row[1](<b>$row[0]</b>)</option>\n";
  }
}                                   
?>
</select>

this should do the trick:

SELECT * FROM `Tasks` 
WHERE `ParentTask_id` = 0 OR `ParentTask_id` IN 
(SELECT `Task_id` FROM `Tasks` WHERE `ParentTask_id` = 0)

... WHERE parent_task_id = 0 (或该列的默认值是什么)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM