2

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.

Jason
  • 23
  • 1
  • 3

2 Answers2

5

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;
   }
Carlos
  • 2,222
  • 12
  • 21
  • +1, for showing the purpose of MVC. Also I would check what is happening in his `header()` that might be preventing other content/views from loading. – ifaour Jan 27 '11 at 12:32
0

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

enthusiastic
  • 732
  • 2
  • 9
  • 18