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" i开发者_运维技巧d="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
(or whatever the default value for that column is)
精彩评论