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

发布于 2024-10-14 04:36:04 字数 936 浏览 6 评论 0原文

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

控制器:

   $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;
  }

   }

?>

任何帮助都会很好,谢谢。

Hi I am having an issue getting the view to show any results for the following code.

Controller:

   $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);

View:

    <?php 

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

   }

?>

Any help would be nice thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柠檬 2024-10-21 04:36:04

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

 $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();


    }


}

视图:

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

The reason why your view is not displaying something is because you're query is not completely correct.

 $datedue = 2011-01-27;

should be enclosed in quotes, since the date is a string.

 $datedue = "2011-01-27";

Also, you're not correctly following the concept of MVC. All the database querying and results should occur inside of the Model. And all of the data handling inside the Controller.

The Controller nor the View should handle any of the database connections, that is the duty of the Model. Therefore I would advise to create a Model and put all of the Database querying in one or two functions. The Controller would then call these functions and receive the data back. Then it should manipulate it and pass it to the View in a clean matter (by this I mean, that the View should NEVER call result()

Here is how you're code should be structured:

CONTROLLER

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);
}
}

MODEL

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();


    }


}

VIEW:

   foreach($taskdetails as $task)
   {
     echo $task->name;
   }
墨离汐 2024-10-21 04:36:04

验证您的任务表包含名称字段

查看

 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);

verify your task table contain the name field

view

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

}

why you are passing array in this line

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

instead write

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文