Natty Guurl
Natty Guurl

Reputation: 125

Invalid argument supplied for foreach() due to var_dump is null

Yesterday when I var_dump($this->m_test->result_getGrades());it gave me an array[1356] now it returned null. Can anyone help me figure out why it's NULL? I'm still new in PHP and Codeigniter and its pretty stressful figuring out why I can't retrieve any data from my database.

I assume the reason why I have this error because ($this->m_test->result_getGrades()) is NULL

A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: views/v_display.php

Line Number: 7

Also, What are the factors why I can't retrieve any data from my database? For future references

This is my code:

Controller c_test.php

function getGrades() {
       $data['query'] = $this->m_test->result_getGrades(); 
       $this->load->view('v_display', $data);
    }

Model m_test.php

function result_getGrades()
    {
          $this->db->select('grades.blockcode,subjectblocking.subjectcode,subjects.description,grades.final');
          $this->db->from('grades');
          $this->db->join('subjectblocking','grades.blockcode=subjectblocking.blockcode');
          $this->db->join('subjects','subjectblocking.subjectcode=subject.subjectcode');
          $this->db->where('2013-F019');
          $query=$this->db->get();          
    }

Views v_display.php

<?php foreach ($query as $row): ?>

               <?php echo $row->studentid;?><br>
               <?php echo $row->subjectcode;?><br>
               <?php echo $row->description;?><br>
               <?php echo $row->final;?><br>


         <?php endforeach; ?>

Thank you once again! :)

Upvotes: 0

Views: 310

Answers (3)

Macks
Macks

Reputation: 431

There are many faults in your result_getGrades() function :

function result_getGrades()
{
    $this->db->select('grades.blockcode,subjectblocking.subjectcode,subjects.description,grades.final');
    $this->db->from('grades');
    $this->db->join('subjectblocking','grades.blockcode=subjectblocking.blockcode');
    $this->db->join('subjects','subjectblocking.subjectcode = subjects.subjectcode');
    $this->db->where('your_field', '2013-F019');

    $query = $this->db->get()->result();

    return $query;        
}

There was :

  • add the return $query;

  • field name in your where clauses

  • subject to subjects in your join

Upvotes: 1

soedjarwoAli
soedjarwoAli

Reputation: 31

You should try this:

First, you will need to check your where clause.

see example below:

$this->db->where('name', 'test');

that will produce WHERE name = 'test' in the MySQL query

then you have to put a return statement after your get() method:

$query = $this->db->get();
return $query->result_array();

so your model should be something like this:

function result_getGrades()
{
      $this->db->select('grades.blockcode,subjectblocking.subjectcode,subjects.description,grades.final');
      $this->db->from('grades');
      $this->db->join('subjectblocking','grades.blockcode=subjectblocking.blockcode');
      $this->db->join('subjects','subjectblocking.subjectcode=subject.subjectcode');

      $this->db->where('name of field', '2013-F019');

      $query=$this->db->get();  

      return $query->result_array();        
}

Upvotes: 3

user3940738
user3940738

Reputation:

var dumping the query itself usually doesn't return much for me usually but it depends on when you dump it.

<?php foreach ($query as $row): ?>

           <?php echo $row->studentid;?><br>
           <?php echo $row->subjectcode;?><br>
           <?php echo $row->description;?><br>
           <?php echo $row->final;?><br>


     <?php endforeach; ?>

For this you don't need to keep opening and closing the php tags unless you want alot of html around it:

<?php foreach ($query as $row){

           echo $row->studentid.'<br>';
           echo $row->subjectcode.'<br>';
           echo $row->description.'<br>';
           echo $row->final.'<br>';


     }?>

Notice the . ? It joins the $row->studentid to the html meaning it's alot cleaner code.

Now onto the question, you're assigning a variable as a variable in your foreach...Which is pointless. Thankfully i know the fix. You need to turn the query into a result from your database:

<?php foreach ($query->result() as $row){

           echo $row->studentid.'<br>';
           echo $row->subjectcode.'<br>';
           echo $row->description.'<br>';
           echo $row->final.'<br>';


     }?>

$query->result() as $row will enable you to echo out the information you get back from the database.

Also at the end of the model, you need to add return $query; otherwise your model is getting the data and not doing anything with it.

Upvotes: 1

Related Questions