0

I got an error like this :

A PHP Error was encountered Severity: Notice

Message: Undefined variable: tasks

controller

 public function show() { $data['tasks'] = array(); $data['tasks'] = $this->Tasks_model->show_task()->result(); $this->load->view('pages/all', $data); } 

view

<?php if( isset($tasks) && ( is_array($tasks) && count($tasks)>0 ) ) { //echo"<pre>"; print_r($tasks); die(); for($i=0;$i<count($tasks);$i++) { ?> <span><?php echo $task[0]['job']; ?></span><br /> <?php } ?> <?php } else { ?> <span>No tasks records found.</span> <?php } ?> 

Model

public function show_task() { return $this->db->get('task'); } 

what's wrong with my code?

2
  • Please update question and put your full updated View code. Commented Feb 22, 2019 at 18:09
  • please read: stackoverflow.com/questions/16506789/… Commented Feb 22, 2019 at 22:49

2 Answers 2

0

Controller

public function show() { $this->$data['tasks'] = array(); $this->$data['tasks'] = $this->Tasks_model->show_task(); $this->load->view('pages/all', $data); } 

View

//Here, you can also use for loop to display data.

<?php if( isset($tasks) && ( is_array($tasks) && count($tasks)>0 ) ) { //echo"<pre>"; print_r($tasks); die(); for($i=0;$i<count($tasks);$i++) { ?> <span><?php echo $task[0]['job']; ?></span><br /> <?php } ?> <?php } else { ?> <span>No tasks records found.</span> <?php } ?> 

EDIT =>

Model

public function show_task() { $query = $this->db->get('task'); if( $query->num_rows() > 0 ) //Always check you are getting result or not. { return $query->result_array(); //result_array() returns the query result as a pure array } else { return array(); } } 

Here you are returning return $this->db->get('task'); directly. So you are not getting anythingin View. First you have to assign $this->db->get('task') to variable e.g. $query and pass result array to controller return $query->result_array();.

Reference => https://www.codeigniter.com/userguide3/database/results.html#result-arrays

Sign up to request clarification or add additional context in comments.

5 Comments

the output is no task records found. it means that, i still can't get the data. here if you want to see the full code: pastebin.com/YKjbgCr2
Yes. you are not getting data from Model.
@BeingShame I have updated my answer. Please refer it.
the result is still "No tasks records found.". I'm starting to think that the problem lies elsewhere. If i don't use those condition in view, the error will be "undefined variable: tasks".
@BeingShame I have made changes to Controller. $this->$data['tasks'] = $this->Tasks_model->show_task(); Here previously $this-> was missing.
0

Controller:

 public function show() { $data['tasks'] = array(); $data['tasks'] = $this->Tasks_model->show_task(); $this->load->view('pages/all', $data); } 

Model:

public function show_task() { return $this->db->get('task')->result(); } 

View Page:

<?php if( isset($tasks) && ( is_array($tasks) && count($tasks)>0 ) ) { //echo"<pre>"; print_r($tasks); die(); for($i=0;$i<count($tasks);$i++) { ?> <span><?php echo $task[0]['job']; ?></span><br /> <?php } ?> <?php } else { ?> <span>No tasks records found.</span> <?php } ?> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.