Welcome Guest, Not a member yet? Register   Sign In
Message: Undefined variable: rows ?
#1

[eluser]solid9[/eluser]
Hello anyone can help me with this.

Error message:
Code:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: rows
Filename: views/data_model_test.php
Line Number: 3

Controller:
Code:
class Main extends CI_Controller {
public $data = array();

    ... more codes here ...

//show all user
function show_user() {
$this->data['rows'] = $this->data_model->getAll();
  
$this->data['midnav'] = $this->load->view('data_model_test', null, TRUE);
$this->load->view('front_page', $this->data);
}


Model:
Code:
class Data_model extends CI_Model {

      ... more codes here ...

function getAll() {
  $this->db->select('username, email');
  $this->db->from('users');
  $this->db->where('id', 1);
  
  $q = $this->db->get();
  
  if($q->num_rows() > 0) {
   foreach ($q->result() as $row) {
    $data[] = $row;
   }
   return $data;
  }  
  
}
}


View:
Code:
<div class='midnav'>

&lt;?php foreach($rows as $r) : ?&gt;
  
<h1>&lt;?php echo $r->username; ?&gt;</h1>
<div>&lt;?php echo $r->email; ?&gt;</div>
  
&lt;?php endforeach; ?&gt;
  
</div>


Thanks in advanced.
#2

[eluser]John_Betong_002[/eluser]
This post deleted so as not to confuse the issue.

Please see following post.

My apologies for being hasty and not checking the problem
#3

[eluser]Aken[/eluser]
John, it has nothing to do with his query.

You forgot to pass the data to your midnav view when you loaded and returned it.
#4

[eluser]solid9[/eluser]
@aken

thanks for the hint,

But I already passed the data in the controller;
Code:
function show_user() {
   $this->data['rows'] = $this->data_model->getAll();
   $this->data['midnav'] = $this->load->view('data_model_test', null, TRUE);

   $this->load->view('front_page', $this->data);
}

Sorry I'm noob, So what is missing in this codes?

tnx n advanced.




#5

[eluser]John_Betong_002[/eluser]
Try this:

Code:
function show_user() {
   # corrected again - see following posts
   #$this->data['rows']  = $this->data_model->getAll();
   $this->data['midnav'] = $this->load->view('data_model_test', null, TRUE);

   $data['rows']   = $this->data_model->getAll();
   $data['midnav'] = $this->load->view('data_model_test', null, TRUE);

   $this->load->view('front_page', $data);
}
&nbsp;
edit: removed $this->
#6

[eluser]LuckyFella73[/eluser]
Just a hint:
If your model method expects only one row as result it's
easier to just return like this:
Code:
if ($query->num_rows() > 0)
{
   return $query->row();
}
Look at (section ROW):
http://ellislab.com/codeigniter/user-gui...sults.html


And: if you pass data to your view you better do this:
Code:
$data['rows'] = $this->data_model->getAll();

instead of:
Code:
$this->data['rows'] = $this->data_model->getAll();

If you assign data to $this->whatever the values are available
in your views anyway (without the need to pass them).


Would be easier to find your error if you post both views
and the actual controller code.
But I guess its like Aken allready said:
you have to pass the values to you midnav:
Code:
function show_user()
{
$data_midnav['rows'] = $this->data_model->getAll();
$data['midnav'] = $this->load->view('data_model_test', $data_midnav, TRUE);

$this->load->view('front_page', $data);
}
// in view front_page.php you can echo $midnav then
#7

[eluser]solid9[/eluser]
@John_Betong

sorry man your suggestion is not right...

but thanks anyway.
#8

[eluser]solid9[/eluser]
@luckyfella73

Thanks.

I'll try that.

#9

[eluser]Jason Stanley[/eluser]
Change this line.

Code:
$this->data['midnav'] = $this->load->view('data_model_test', null, TRUE);

To

Code:
$this->data['midnav'] = $this->load->view('data_model_test', $this->data, TRUE);

Have a look at how the views are made. Then it will make more sense to you.

Quote:The third optional parameter lets you change the behavior of the function so that it returns data as a string rather than sending it to your browser.

The view is already processed and saved to a string. Passing data to your template file after this has happened won't do anything.

Out of interest did you even try what Aken wrote? If you did this problem should have been solved 3 hours ago :roll:
#10

[eluser]solid9[/eluser]
@jasonstanley

LuckyFella is right, I only tweaked a little his work.
Below is the right codes,

Code:
function show_user() {
$data_midnav['rows'] = $this->data_model->getAll();
$this->data['midnav'] = $this->load->view('data_model_test', $data_midnav, TRUE);
  
$this->load->view('front_page', $this->data);
}

Problem Solved.

Thanks again guys...!!!




Theme © iAndrew 2016 - Forum software by © MyBB