• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Model Error

#1
[eluser]georgerobbo[/eluser]
I have a model which pulls content from a MySQL database. When I use the query in my controller it works fine, however I am having difficulties in the model. The error being returned is:

Quote:A PHP Error was encountered
Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: views/content.php

Line Number: 13

This is my code:

Code:
<?php

class Welcome extends Controller {

    function Welcome()
    {
        parent::Controller();
    }
    
     function index()
    {
        $data['title'] = 'Home';
        
        $this->load->model('Ros_sidecont');
        $data['sidecont'] = $this->Ros_sidecont->get_sidecont();
                
        $this->load->view('meta', $data);
        $this->load->view('header');
        $this->load->view('panel');
        $this->load->view('content', $data);
        $this->load->view('footer');    
   }
}
?>

Code:
<?php

class Ros_sidecont extends Model {

    function Ros_sidecont()
    {
        parent::Model();
    }
    
    function get_sidecont()
    {    
        $query = $this->db->query('SELECT ros_sidecont_title, ros_sidecont_text FROM ros_sidecont');
        $data['sidecont'] = array();
        foreach($query->result_array() as $row)
        {
            $data['sidecont'][] = $row;
        }
    }
}

Code:
<div id="content">
        
        &lt;?php foreach($pagecont as $content_item):?&gt;
        <h4>&lt;?php echo $content_item['ros_sidecont_title']; ?&gt;</h4>
        <p>&lt;?php echo $content_item['ros_sidecont_text']; ?&gt;</p>
        &lt;?php endforeach; ?&gt;

    </div>
    
    <div id="sidebar">

        &lt;?php foreach($sidecont as $side_item):?&gt;
        <div class="sideitem">
            <div class="sidetitle">
                <h3>&lt;?php echo $side_item['ros_sidecont_title']; ?&gt;</h3>
            </div>
                <p>&lt;?php echo $side_item['ros_sidecont_text']; ?&gt;</p>
        </div>
        &lt;?php endforeach; ?&gt;
          
    </div>
        
    </div>

#2
[eluser]OES[/eluser]
You are not returning anything from the get_sidecont model.

instead of

Code:
foreach($query->result_array() as $row)
        {
            $data['sidecont'][] = $row;
        }

do

Code:
return $query->result_array()

#3
[eluser]Johan André[/eluser]
I usually do it like this:

Code:
// The model
class Simple_model extends Model
{
    function __construct()
    {
        parent::Model();
    }
    
    function get_all()
    {
        $this->db->select('name, phone');
        $this->db->from('adressbook');
        return $this->db->get()->result_array();
    }
}

Code:
// The view
&lt;? if(count($data)) { ?&gt;
<ul>
&lt;? foreach($data AS $item) : ?&gt;
<li>&lt;?=$item['name']?&gt;, &lt;?=$item['phone']?&gt;</li>
&lt;? endforeach; ?&gt;
</ul>
&lt;? } else { ?&gt;
<p>Nothing to display</p>
&lt;? } ?&gt;

#4
[eluser]georgerobbo[/eluser]
[quote author="OES" date="1250005005"]You are not returning anything from the get_sidecont model.

instead of

Code:
foreach($query->result_array() as $row)
        {
            $data['sidecont'][] = $row;
        }

do

Code:
return $query->result_array()
[/quote]

I don't understand why the model doesn't return anything. When I have the query in my controller it works perfectly?

#5
[eluser]Johan André[/eluser]
When you use the code in your controller it populates the $data-array and sends it to the view. Remember that the $data-array in the model is not the same as the $data-array in the controller. The array is a property of the class refeering to it.

The model methods that are used to fetch model-data to the controller needs to return something (ie end the method with return $somethingWink.

#6
[eluser]georgerobbo[/eluser]
[quote author="OES" date="1250005005"]You are not returning anything from the get_sidecont model.

instead of

Code:
foreach($query->result_array() as $row)
        {
            $data['sidecont'][] = $row;
        }

do

Code:
return $query->result_array()
[/quote]

Such a more efficient way of doing it. Thank you very much.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.