how to limit to main task to be not to display in remaining loop?
i have this query and code
if(isset($_REQUEST['main_task']) && ($_REQUEST['main_task'] !='')) {
$query_task = "SELECT * from manage_project_task where prj_task_name LIKE '%".$_REQUEST['main_task']."%' and prj_task_parent_id='0'";//Sort Main Task
} else {
$query_task = "SELECT * from manage_project_task where prj_task_project_id=$project_id and prj_task_parent_id='0'";//Sort Main Task
}
$SelectQuery = $query_task." LIMIT ".$start.",".$limit."";
$result_task = $db->GetALL($SelectQuery);
$cnt_task=count($result_task);
if ($db->Affected_Rows($result_task)>0)
{
for ($i=0;$i<$cnt_task;$i++) {
$actual_hours_worked=0;
$actual_hours_worked2=0;
// subtask hyperlink
if (isset($_REQUEST['sub_task']) && ($_REQUEST['sub_开发者_运维百科task'] !='')) {
$query_subtask1 = "SELECT * from manage_project_task where prj_task_parent_id='".$result_task[$i]['prj_task_id']."' AND prj_task_name LIKE '%".$_REQUEST['sub_task']."%'";
} else {
$query_subtask1 = "SELECT * from manage_project_task where prj_task_parent_id='".$result_task[$i]['prj_task_id']."'";
}
$result_subtask1 = $db->GetALL($query_subtask1);
$cnt_subtask1=count($result_subtask1);
}
}
this gives me main tasks and sub task in serial pattern .Now I have taken 2 textfield 1 for main task and 2nd for sub task now if i only entered subtsk in 2nd textfield then it searches the subtask with its main task but along with it also give in result the remaining main task like this
- main task
- sub task (matching string in 2nd field)
- main task
- main task
- main task
- main task
I think its because the second loop depends on first one.So my question is hw can get only matching subtask with its main task and not the remaining main tasks?? Please sugest any solution.
In the 6th line of your code you overwrite the $query_task
variable so the $_REQUEST['main_task']
makes no difference. This is why every main task is listed, like there were no search string given.
Edit: if you have 3 matching tasks and 2 matching subtasks in the first matching task, you would get the result you describe. What you have to do is: use only those task which have matching subtasks.
Making it simple: loop through the matching tasks (like you do) but only if you find a matching subtask inside it, add that task to the ones you want to display.
$displayed_tasks = array();
/* Query for matching tasks */
foreach ($matched_tasks as $t)
{
/* Query for matching subtasks */
if (!empty($matched_subtasks))
{
$displayed_tasks[] = $t;
}
}
shift for loop inside the if part
if (isset($_REQUEST['sub_task']) && ($_REQUEST['sub_task'] !='')) {
for ($i=0;$i<$cnt_task;$i++) {
$actual_hours_worked=0;
$actual_hours_worked2=0;
$query_subtask1 = "SELECT * from manage_project_task where prj_task_parent_id='".$result_task[$i]['prj_task_id']."' AND prj_task_name LIKE '%".$_REQUEST['sub_task']."%'";
$result_subtask1 = $db->GetALL($query_subtask1);
$cnt_subtask1=count($result_subtask1);
}
}
else {
for ($i=0;$i<$cnt_task;$i++) {
$actual_hours_worked=0;
$actual_hours_worked2=0;
$query_subtask1 = "SELECT * from manage_project_task where prj_task_parent_id='".$result_task[$i]['prj_task_id']."'";
$result_subtask1 = $db->GetALL($query_subtask1);
$cnt_subtask1=count($result_subtask1);
}
}
精彩评论