开发者

CodeIgniter passing array with queries into view

开发者 https://www.devze.com 2023-02-06 18:57 出处:网络
Hi I am having an issue getting the view to show any results for the following code. Controller: $datedue = 2011-01-27;

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->开发者_如何学C;load->view('dashboard', $data);

View:

    <?php 

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

   }

?>

Any help would be nice thanks.


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消