5

I'm using CodeIgniter 2.1.2 and here's my situation right now. I have a model named math.php in C:\wamp\www\cr8v\application\models. I am trying to load this in my controller from C:\wamp\www\cr8v\application\controllers\site.php yet I am getting this error:

Unable to locate the model you have specified: math_model

Here's the content of math.php:

 <?php
    class Math_model extends CI_Model(){
        public function add(){
            return 1+1;
        }
    }
 ?>

And here's the content of my site.php:

<?php
    class Site extends CI_Controller{
        public function index(){
            $this->addstuff();
        }

        public function addstuff(){
            $this->load->model("math_model");
            echo $this->math->add();
        }
    }  
?>

I followed this in a tutorial in YouTube. Yet, it doesn't give me my desired output. I've tried Googling about it and read other related questions with this, however those information are not enough.

I have read this just a while ago.

Now I have the following error:

( ! ) Parse error: syntax error, unexpected '(', expecting '{' in C:\wamp\www\cr8v\application\models\math_model.php on line 2
Call Stack
#   Time    Memory  Function    Location
1   0.0036  148768  {main}( )   ..\index.php:0
2   0.0062  185072  require_once( 'C:\wamp\www\cr8v\system\core\CodeIgniter.php' )  ..\index.php:202
3   0.0561  784312  call_user_func_array ( )    ..\CodeIgniter.php:359
4   0.0561  784360  Site->index( )  ..\CodeIgniter.php:359
5   0.0561  784376  Site->addstuff( )   ..\site.php:4
6   0.0561  784504  CI_Loader->model( ) ..\site.php:8
double-beep
  • 5,031
  • 17
  • 33
  • 41
Charmie
  • 2,468
  • 7
  • 33
  • 47

5 Answers5

15

The name needs to be the same in all places:

Here:

class Math_model extends CI_Model {
   // your model
}

Here:

$this->load->model("math_model");

When using it:

$this->math_model->add();

And: in your file system. So rename math.php to math_model.php and it will work.

Robert
  • 1,899
  • 1
  • 17
  • 24
  • uh i have done what you have said however i got the same error as per the part where i edited my question.. The parse error and so on and so forth – Charmie Jun 30 '12 at 19:13
3

The name of the file should be math_model.php and you should call it like this:

echo $this->math_model->add();
fedejp
  • 958
  • 3
  • 12
  • 20
  • uh i have done what you have said however i got the same error as per the part where i edited my question.. The parse error and so on and so forth. – Charmie Jun 30 '12 at 19:13
1

File name and model name should be same try this instead

class Math_model extends CI_Model {
   // your code
}

and the file name should be Math_model.php

Nihad P P
  • 11
  • 1
0

The question has been answered really and the problem was this:

 <?php
    class Math_model extends CI_Model(){
        //so on
        } 
 ?>

..it's the open and close parethesis after declaring the class.. it should be:

 <?php
    class Math_model extends CI_Model{
        //so on
        } 
 ?>

thank you so much for those who responded

Charmie
  • 2,468
  • 7
  • 33
  • 47
0

Well, it should always be noted that function name should not be replicated with controller or model name such as below;

<?php
  class Abc extends CI_Controller{

public function abc(){} // this confused CI in loading   

}

this may be helpful for developers new to CI,

Shariati
  • 121
  • 9