Welcome Guest, Not a member yet? Register   Sign In
Help! Call to a member function on a non-object in ....
#1

[eluser]otherjohn[/eluser]
Ok,so I am getting this error
"Fatal error: Call to a member function on a non-object in /home/domain/public_html/****/application/views/areacode_view.php on line 49"

My controller looks like this:
Code:
class PhoneNumbers extends Controller {

    function PhoneNumbers()
    {
        parent::Controller();
        
        $this->load->helper('url');
        $this->load->helper('form');
        
        //$this->load->scaffolding('PhoneNumbers');
        
    
    }
        function areacode(){
        $this->load->model('areacode_model','ac_db',True);
        $this->load->view('areacode_view',$data);
    
    }
function _formatNumber($format,$areacode,$number){
    
        if($format ==1){
            return "(".$areacode.") ".substr($number,0,3)."-".substr($number,3,4);
        }else if($format == 2){
            return $areacode."-".substr($number,0,3)."-".substr($number,3,4);
        }else{
            return $areacode.$number;
        }
    }
}

My Model is
Code:
class AreaCode_model extends Model {


    function AreaCode_model()
    {
        parent::Model();
    }

function get_All_Numbers()
    {
    if ($this->uri->segment(1) === FALSE)
    {
        $areacode = "000";
    }
    else
    {
            $areacode = substr($this->uri->segment(1),10,3);
    }
        
        $sql = "Select * from PhoneNumbers where areaCode=?";
        $query = $this->db->query($sql,$areacode);
        return $query->result();
    }
}
And finally my areacode_view is
Code:
<?php foreach($query->result() as $row): ?>

<h3>&lt;?php echo $this->_formatNumber(1,$row->areaCode, $row->phone); ?&gt;</h3>
<p>&lt;?php echo anchor('../phonenumbers/numberpage/'.$this->_formatNumber(2,$row->areaCode, $row->phone),'Phone Number '.$this->_formatNumber(3,$row->areaCode, $row->phone)); ?&gt;</p>
<hr>
&lt;?php endforeach; ?&gt;</ol>

OK so I don't understand why I am getting this error?
Can anyone see whats going on here?
NOTES:
uri->segment(1) would return area-code-555
so I extract the 555 out of it.

thanks
#2

[eluser]Rick Jolly[/eluser]
Instead of calling a private method in your controller from your view:

1) create a helper with that function
2) load the helper in the controller
3) call the helper function in your view.

Helpers are meant for this sort of thing - formatting data in the view. Also, it allows you to reuse that formatting code in various controllers/views.
#3

[eluser]otherjohn[/eluser]
thanks, I will look into helpers.
I found the problem.
In my view page i had $query->result()
and there was already a "result()" in my model. I removed that and it worked!
thanks

ps, I am loving this framework! its so easy
#4

[eluser]RaZoR LeGaCy[/eluser]
I just upgraded my codeignitor from 1.6.1 to 1.6.2 and I am now recieving this message everywhere




Theme © iAndrew 2016 - Forum software by © MyBB