Php CodeIgniter将带有查询的数组传递到视图中

Php CodeIgniter将带有查询的数组传递到视图中,php,arrays,codeigniter,tankauth,Php,Arrays,Codeigniter,Tankauth,您好,我在获取视图以显示以下代码的任何结果时遇到问题 控制器: $datedue = 2011-01-27; $username = $this->tank_auth->get_username(); $sql = "SELECT taskid FROM relationships WHERE username = ? AND datedue = ?"; $tasks = $this->db->query($sql, array($username,

您好,我在获取视图以显示以下代码的任何结果时遇到问题

控制器:

   $datedue = 2011-01-27;
  $username = $this->tank_auth->get_username();
  $sql = "SELECT taskid FROM relationships WHERE username = ? AND datedue = ?"; 
  $tasks = $this->db->query($sql, array($username, $datedue));
  $count = 0;
      $taskdetails = array();

  foreach($tasks->result() as $row)
  {

   $taskid = $row->taskid;

    $subsql = "SELECT * FROM tasks WHERE id = ?"; 
    $taskdetails[$count] = $this->db->query($subsql, array($taskid));
    $count++;
  }

  $data['taskdetails'] = $taskdetails;
  $data['total'] = $count;

  $this->header();
  $this->load->view('dashboard', $data);
    <?php 

 foreach($taskdetails as $entry)
   {
  foreach($entry->result() as $row)
  {
   echo $row->name;
  }

   }

?>
   foreach($taskdetails as $task)
   {
     echo $task->name;
   }
查看:

   $datedue = 2011-01-27;
  $username = $this->tank_auth->get_username();
  $sql = "SELECT taskid FROM relationships WHERE username = ? AND datedue = ?"; 
  $tasks = $this->db->query($sql, array($username, $datedue));
  $count = 0;
      $taskdetails = array();

  foreach($tasks->result() as $row)
  {

   $taskid = $row->taskid;

    $subsql = "SELECT * FROM tasks WHERE id = ?"; 
    $taskdetails[$count] = $this->db->query($subsql, array($taskid));
    $count++;
  }

  $data['taskdetails'] = $taskdetails;
  $data['total'] = $count;

  $this->header();
  $this->load->view('dashboard', $data);
    <?php 

 foreach($taskdetails as $entry)
   {
  foreach($entry->result() as $row)
  {
   echo $row->name;
  }

   }

?>
   foreach($taskdetails as $task)
   {
     echo $task->name;
   }


任何帮助都很好,谢谢。

验证任务表是否包含名称字段

查看

 foreach($taskdetails as $entry)
{
   foreach($entry->result() as $row)
   {
       echo $row[0];
   }

}
为什么要在此行中传递数组

$taskdetails[$count]=$this->db->query($subsql,array($taskid))

而是写

$taskdetails[$count]=$this->db->query($subsql,$taskid)


视图未显示某些内容的原因是您的查询不完全正确

 $datedue = 2011-01-27;
应该用引号括起来,因为日期是字符串

 $datedue = "2011-01-27";
此外,您没有正确地遵循MVC的概念。所有数据库查询和结果都应该发生在模型内部。以及控制器内部的所有数据处理

控制器和视图都应该处理任何数据库连接,这是模型的职责。因此,我建议创建一个模型,并将所有数据库查询放在一个或两个函数中。然后,控制器将调用这些函数并接收返回的数据。然后它应该操纵它,并将它以干净的方式传递给视图(我的意思是,视图永远不应该调用
result()

以下是您的代码应该如何构造:

控制器

class Tasks extends Controller {

function Tasks ()
{
    parent::Controller();   
    $this->load->model('tasks_model');
    $this->load->database();
}

function index()
{
  $datedue = "2011-01-27";
  $username = $this->tank_auth->get_username();

  $tasks = $this->tasks_model->getTasks($username, $datedue);


  $count = count($tasks);


  $data['taskdetails'] = $tasks;
  $data['total'] = $count;

   $this->header();
  $this->load->view('dashboard', $data);
}
}
型号

class Tasks_model extends Model {


    function Tasks_model()
    {
        // Call the Model constructor
        parent::Model();
    }

    function getTasks($username, $datedue) {
        $sql = "SELECT taskid FROM relationships WHERE username = ? AND datedue = ?"; 
        $tasks = $this->db->query($sql, array($username, $datedue));

        $taskdetails = array();

        foreach($tasks->result() as $row){

            $taskid = $row->taskid;

            array_push( $taskdetails, $this->getTasksDetails( $taskid ) );
        }

        return $taskdetails;
    }

    function getTasksDetails($taskid) {

        $subsql = "SELECT * FROM tasks WHERE id = ?"; 
        $taskdetails = $this->db->query($subsql, array($taskid));

        return $taskdetails->row();


    }


}
查看:

   $datedue = 2011-01-27;
  $username = $this->tank_auth->get_username();
  $sql = "SELECT taskid FROM relationships WHERE username = ? AND datedue = ?"; 
  $tasks = $this->db->query($sql, array($username, $datedue));
  $count = 0;
      $taskdetails = array();

  foreach($tasks->result() as $row)
  {

   $taskid = $row->taskid;

    $subsql = "SELECT * FROM tasks WHERE id = ?"; 
    $taskdetails[$count] = $this->db->query($subsql, array($taskid));
    $count++;
  }

  $data['taskdetails'] = $taskdetails;
  $data['total'] = $count;

  $this->header();
  $this->load->view('dashboard', $data);
    <?php 

 foreach($taskdetails as $entry)
   {
  foreach($entry->result() as $row)
  {
   echo $row->name;
  }

   }

?>
   foreach($taskdetails as $task)
   {
     echo $task->name;
   }

您是否得到任何输出或完全空白的页面?我的标题显示,但不是仪表板视图尝试在foreach之外插入一些内容,如“dashboard”或其他内容是的,直到您确认“dashboard”视图实际正在加载,这很难帮助。可能会显示
$this->header()的代码
它正在加载,我通过回显'total'变量对其进行了测试。+1,用于显示MVC的用途。此外,我还将检查他的
标题()中发生了什么情况,这可能会阻止加载其他内容/视图。