0

我正在使用 codeigniter,因为我提到这是我的视图代码的一部分

foreach($projects_query as $row)// $row indicates the projects
{ ?>
<tr><td><h3><button type="submit" class="button red-gradient glossy" name = "project_click"  >
<?php echo $row->txtTaskName; ?></button></h3></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>
<?php
foreach($tasks_query as $row2)
{ 
//  if( $row->txtTaskName == "TestProject")
if($row->intTaskID == $row2->intInside)// intInside indicades that the current task($row2) is the subset of which task (system , subsystem or project)
{  
              if($row2->intSummary == 0)//if the task(the system) is an executable task & doesn't have any subtask:
              {
                  $query_team_user_id = $this->admin_in_out_model->get_user_team_task_query($row2->intTaskID);//runs the function and generates a query from tbl_userteamtask where intTaskID equals to the selected row's intTaskID
                  foreach($query_team_user_id as $row_teamid)
                  {
                      $query_teamname = $this->admin_in_out_model->get_team_name($row_teamid->intTeamID);
                      $query_fn_ln = $this->admin_in_out_model->get_fn_ln_from_userid($row_teamid->intUserID);
                      foreach($query_teamname as $row_teamname) 
                      {?>
                         <tr><td></td><td></td><td><h4> <?php echo $row2->txtTaskName;?></h4></td>
                         <td><b><font color='#F33558'><?php echo $row_teamname->txtTeamName;?></font></b></td>
               <?php  }
                      foreach($query_fn_ln as $row_f_l_name) 
                      {?>
                         <td> <?php echo $row_f_l_name->txtFirstname." ".$row_f_l_name->txtLastname;?></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td>
                <?php }?>
                      </tr>
          <?php  }
              }
              else{ ?> <tr><td></td><td></td><td><h4> <?php echo $row2->txtTaskName;?></h4></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr><?php }

         foreach($tasks_query as $row_subsystems)
         {
                   if($row_subsystems->intInside == $row2->intTaskID )//if the task is the subtask of a system(it means the task is a subsystem)
                   {
                       if($row_subsystems->intSummary == 0)//if the task is an executable task & doesn't have any subtask:
                                {
                                   $query_team_user_id = $this->admin_in_out_model->get_user_team_task_query($row_subsystems->intTaskID);
                                   foreach($query_team_user_id as $row_teamid)
                                    {?>
                                       <tr><?php
                                        $query_teamname = $this->admin_in_out_model->get_team_name($row_teamid->intTeamID);
                                        $query_fn_ln = $this->admin_in_out_model->get_fn_ln_from_userid($row_teamid->intUserID);
                                        foreach($query_teamname as $row_teamname) 
                                        {?>
                                           <td></td><td></td><td><h5><?php echo $row_subsystems->txtTaskName?></h5><br/></td>
                                           <td><b><font color='#F33558'><?php echo $row_teamname->txtTeamName;?></font></b></td><?php
                                        }
                                        foreach($query_fn_ln as $row_f_l_name) 
                                        {?>
                                           <td><?php echo $row_f_l_name->txtFirstname." ".$row_f_l_name->txtLastname;?></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><?php
                                        }?>
                                        </tr><?php
                                    }
                                } 
                                else{ ?><tr><td></td><td></td><td><h5><?php echo $row_subsystems->txtTaskName?></h5></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr><?php }
                                  foreach($tasks_query as $row_tasks)
                                  {
                                            if($row_tasks->intInside == $row_subsystems->intTaskID )//if the task is the subtask of a subsystem
                                              { 
                                                           if($row_tasks->intSummary == 0)//if the task is an executable task & doesn't have any subtask:
                                                          {
                                                              $query_team_user_id = $this->admin_in_out_model->get_user_team_task_query($row_tasks->intTaskID);
                                                              foreach($query_team_user_id as $row_teamid)
                                                              {?>
                                                                  <tr><?php
                                                                  $query_teamname = $this->admin_in_out_model->get_team_name($row_teamid->intTeamID);
                                                                  $query_fn_ln = $this->admin_in_out_model->get_fn_ln_from_userid($row_teamid->intUserID);
                                                                 foreach($query_teamname as $row_teamname) 
                                                                  {?>
                                                                      <td></td><td></td><td><b><?php echo $row_tasks->txtTaskName;?></b></td>
                                                                      <td><b><font color='#F33558'><?php echo $row_teamname->txtTeamName;?></font></b></td><?php
                                                                  }
                                                                  foreach($query_fn_ln as $row_f_l_name) 
                                                                  {?>
                                                                     <td><?php echo $row_f_l_name->txtFirstname." ".$row_f_l_name->txtLastname;?></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><?php
                                                                  }?>
                                                                  </tr><?php
                                                              }
                                                          } 

                                           }
                                   }
              }

           }

         }



 }

}?>

在控制器中我有

$projects_query = $this->admin_in_out_model->get_projects();                
$tasks_query = $this->admin_in_out_model->get_systems();
$userteamtask = $this->admin_in_out_model->get_user_team_task();

$data['tasks_query'] = $tasks_query;
$data['projects_query'] = $projects_query;

$this->load->view('project_view',$data);

但正如你所见,我在视图中调用我的模型函数我怎么能做其他事情来做到这一点我的意思是不在我的视图中调用我的模型函数

我必须补充一点,我的模型函数有参数,这些是模型函数:

    function get_projects()
    {
    $this -> db -> select('*');
            $this -> db -> from('tbl_task');
    $this -> db -> where('intInside','0');
    $query = $this->db->get();
    return $query->result();             
}

function get_systems()
    {
    $this -> db -> select('*');
    $this -> db -> from('tbl_task ');

    $this -> db -> where('intInside <> ','0');
        $query = $this->db->get();
    return $query->result();     
}


    function get_user_team_task_query($task_id)//gets information from tbl_userteamtask where the field intTaskID is equal to $task_id
    {
    $this -> db -> select('*');
            $this -> db -> from('tbl_userteamtask');
    $this -> db -> where('intTaskID',$task_id);
    $query_teamid = $this->db->get();
    return $query_teamid->result();
}   

    function get_user_team_task()//gets information from tbl_userteamtask where the field intTaskID is equal to $task_id
    {
    $this -> db -> select('*');
            $this -> db -> from('tbl_userteamtask');
 // $this -> db -> where('intTaskID',$task_id);
    $query_teamid = $this->db->get();
    return $query_teamid->result();
}   

function get_team_name($query_teamid)
    {
    $this -> db -> select('*');
    $this -> db -> from('tbl_team');
    $this -> db -> where('intTeamID',$query_teamid);
    $query_teamname = $this->db->get();
    return $query_teamname->result();
}   

function get_user_name($query_userid)
    {
    $this -> db -> select('*');
    $this -> db -> from('tbl_user');
    $this -> db -> where('intUserID',$query_userid);
    $query_username = $this->db->get();
    return $query_username->result();
}

function get_fn_ln_from_userid($selected_id)
{
        $this -> db -> select('tbl_user.intUserID, tbl_user.intPersonID,tbl_person.intPersonID,tbl_person.txtFirstname, tbl_person.txtLastname');
        $this -> db -> from('tbl_user , tbl_person');
        $where = "tbl_user.intPersonID = tbl_person.intPersonID ";
        $this -> db -> where($where);
        $this -> db -> where('tbl_user.intUserID', $selected_id);
        $query = $this -> db -> get();//makes query from DB
        return $query->result();            
    }

我必须使用子查询吗?

这是真的?我的意思是我可以这样做吗?

foreach( $data as $key => $each )
{
    $data[$key]['team_id']    = $this->get_user_team_task_query( $each['intTaskID'] ); 
    foreach($data[$key]['team_id'] as $key_teamname => $each)
    {
         $data[$key_teamname]['team_name']    = $this->get_team_name( $each['intTeamID'] ); 
    }

}

型号代码:

foreach( $data as $key => $each )
{
     $data[$key]['intTaskID']  = $each['intTaskID'];
     $data[$key]['team_id']    = $this->get_user_team_task_query( $each['intTaskID'] ); 
     foreach($data[$key]['team_id'] as $key => $each)
     {
        $data[$key]['team_name']    = $this->get_team_name( $each['intTeamID'] ); #fetching of the teamname and saving in the array
        $data[$key]['user_name']    = $this->get_fn_ln_from_userid( $each['intUserID'] );
        foreach($data[$key]['user_name'] as $key => $each)
        {
            $data[$key]['first_name']    =  $each['txtFirstname'] ;
            $data[$key]['last_name']    =  $each['txtLastname'] ;                      
        }
        $data[$key]['first_name'] = $data[$key]['first_name'];
        $data[$key]['last_name'] = $data[$key]['last_name'];
     }

}

查看代码的一部分:

foreach($tasks_query as $row_systems)
{ 
   if($row->intTaskID == $row_systems['intInside'])// intInside indicades that the current task($row2) is the subset of which task (system , subsystem or project)
    {?>
       <tr><td></td><td></td><td><h4> <?php echo $row_systems['txtTaskName'];?></h4></td><?php
       foreach($tasks_query as $row_subsystems)
        { 
            if($row_systems['intTaskID'] == $row_subsystems['intInside'])
            {?>
                 <tr><td></td><td></td><td><h4> <?php echo $row_subsystems['txtTaskName'];?></h4></td><?php
                 foreach($tasks_query as $row_tasks)
                 { 
                    if($row_subsystems['intTaskID'] == $row_tasks['intInside'])
                    {?>
                          <tr><td></td><td></td><td><h4> <?php echo $row_tasks['txtTaskName'];?></h4></td>
                          <td><?php echo $row_tasks['team_name'];?>   </td>
                          <td><?php echo $row_tasks['first_name'];?>   </td>
                          <?php  
                    }
                 }
           }
      }

} ?>

4

2 回答 2

0

举个例子,你在循环foreach($projects_query as $row),你要求get_projects()这个。在循环中你用 调用get_team_name($team_id)team_id对吗?get_projects()现在,只需稍微更改您的模型功能即可立即获得team name

function get_projects()
{
    $this->db->select('*');
    $this->db->from('tbl_task');
    $this->db->where('intInside','0');
    $query  = $this->db->get();
    //return $query->result();   //instead of returning loop here and fetch the team name  
    $data   = $query->result_array();
    if( is_array( $data ) && count( $data ) > 0 ){
        foreach( $data as $key => $each ){
            $data[$key]['team_name']    = $this->get_team_name( $each['intTeamID'] ); //fetching of the teamname and saving in the array
        }
    }
    return $data; //Now return the data array. :D        
}

鉴于:

//$query_team_user_id = $this->admin_in_out_model->get_user_team_task_query($row2->intTaskID); #you dont need it now if you make a 2d array 
foreach($row['team_name'] as $row_teamid){    #changed $query_team_user_id to $row['team_name'] as this will now represent an array
    //inside the loop
}

同样,您需要获取所有需要在模型本身的页面中打印的相关数据,而无需从视图中调用。
希望这对您有所帮助。

更新:

foreach( $data as $key => $each )
{
    $data[$key]['intTaskID']    = $each['intTaskID'];
    $data[$key]['team_id']      = $this->get_user_team_task_query( $each['intTaskID'] ); 
    foreach($data[$key]['team_id'] as $key => $each)
    {
        $data[$key]['team_name']    = $this->get_team_name( $each['intTeamID'] ); #fetching of the teamname and saving in the array
        $user_name    = $this->get_fn_ln_from_userid( $each['intUserID'] ); #you are returning tbl_person.txtFirstname, tbl_person.txtLastname from get_fn_ln_from_userid() with ->result()?
        $data[$key]['first_name']   =  $user_name->txtFirstname; #changes here
        $data[$key]['last_name']    =  $user_name->txtLastname;  #changes here 
    }
}

永远记住你从你的函数中返回的内容,并按照你将得到的预期结果更改你的代码。尝试在循环中调试您的代码以获取返回值并相应地更改您的代码。例如 :

foreach( $data as $key => $each )
{
    $data[$key]['intTaskID']    = $each['intTaskID'];
    $data[$key]['team_id']      = $this->get_user_team_task_query( $each['intTaskID'] ); 
    foreach($data[$key]['team_id'] as $key => $each)
    {
        echo "<pre>";print_r( $each );die; #will print the $each
        $data[$key]['team_name']    = $this->get_team_name( $each['intTeamID'] ); #fetching of the teamname and saving in the array
        $user_name    = $this->get_fn_ln_from_userid( $each['intUserID'] ); #you are returning tbl_person.txtFirstname, tbl_person.txtLastname from get_fn_ln_from_userid() with ->result()?
        echo "<pre>";print_r( $user_name );die;  #will print the returned results from the get_fn_ln_from_userid()
        $data[$key]['first_name']   =  $user_name->txtFirstname; #changes here
        $data[$key]['last_name']    =  $user_name->txtLastname;  #changes here 
    }
}
于 2013-10-29T12:21:36.710 回答
0

将代码移动到控制器,然后将其传递给视图。

于 2013-10-29T10:53:42.407 回答