0

My Model File:

Class productModel extends CI_Model{

  function __construct() {
    parent::__construct();
  }
  function getProduct() {

    $query =  $this->db->get('urunler');
    return $query->result();
  }
}

Controller file:

class product extends CI_Controller {


  public function index()
  {
            $model= $this->load->model('productModel',NULL,TRUE);
            $data['urunListesi']=$model->getProduct();

    $this->load->view("site/header");
            $this->load->view("site/product",$data);
            $this->load->view("site/footer");
  }
}

and view file:

foreach ($urunListele as $value) {


                         $adi=$value["adi"];
                         $fotolik= $value['fotolink'];
                         $kucukfot=$value["kucukfoto"];
                         $hakkinda = $value["hakkinda"];

                          echo "<div class='portfolio-item logo animation col-md-3'>";
                                  echo  "<div class='portfolio-border'>";
                                  //<!-- Start Portfolio Item Thumb -->
                                  echo "<div class='portfolio-thumb'>";
                                      echo "<a class='lightbox' title='$hakkinda' href='$fotolik'> ";
                                          echo "<div class='thumb-overlay'><i class='icon-resize-full'></i></div>";
                                          echo "<img alt='' src='$kucukfot' />";
                                      echo "</a>";
                                  echo "</div>";

                                 echo " <div class='portfolio-details'>";

                                          echo "<h4>";
                                          echo "<p align='center'>" . $value["adi"]. "</p>";
                                          echo "</h4>";

                                  echo "</div>";

                              echo "</div>";
                          echo "</div>";
}

And I have this error:

Fatal error: Call to a member function getProduct() on a non-object in C:\xampp\htdocs\ModaGabar\application\controllers\product.php on line 9

I do this also:

$autoload['libraries'] = array('database','session'); 
1
  • Look at the view too, this assignment might bite you later, $fotolik= $value['fotolink']; Commented May 28, 2014 at 15:14

2 Answers 2

1

Does your product class have a constructor ? if it doesn't add:

public function __construct() {        
    parent::__construct();
}

if it does try removing the null parameter :

$model= $this->load->model('productModel',NULL,TRUE);

Change it to:

$model= $this->load->model('productModel','',TRUE);
Sign up to request clarification or add additional context in comments.

Comments

1

Your usage of loading models and referencing them is wrong. The second argument of loading function is used to assign object to a name other than the model name. Additionally, you are referencing the model by using the variable to make calls when the loading function itself returns nothing.

So, what you would need to do is, load the model.

$this->load->model('productModel');

To make calls you would have to use,

$this->productModel->method();

or

You can use another object name via the second parameter.

$this->load->model('productModel', 'pmodel');

To make calls you would have to use,

$this->pmodel->method();

1 Comment

In addition, this->load->model() does not return a value, so you can not store it in a variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.