简体   繁体   English

使用mysql和php的两级邻接表

[英]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.. 我创建了一个新任务,并为该任务分配了父代ID为100,一旦创建新任务ID为101。

Now again i create a new task and assign 100 or 101 to that new id which is being created(102) 现在我再次创建一个新任务,并将100或101分配给正在创建的新ID(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 现在,当我创建ID为104的新任务时,它应该给我一个选择以选择100,101作为父任务ID,而不是102,因为102连接到101且101连接到100父ID

How i can do this two level access? 我该如何进行两级访问? My table structure is taskid ,parenttask id where task id is autoincrement ,primary key 我的表结构是taskid,parenttask id,其中task id是autoincrement,主键

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 (或该列的默认值是什么)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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