Hello guys i am trying to connect my database to get some data using codeigniter , there is my code:
i am getting this error:
Fatal error: Class 'Model' not found in /Applications/MAMP/htdocs/ci/application/models/Data_model.php on line 3
A PHP Error was encountered
Severity: Warning
Message: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/ci/application/models/Data_model.php:3)
Filename: core/Common.php
Line Number: 569
Backtrace:
A PHP Error was encountered
Severity: Error
Message: Class 'Model' not found
Filename: models/Data_model.php
Line Number: 3
Backtrace:
Models:
Data_model.php:
<?php
class Data_model extends Model {
function getAll(){
$q = $this->db->query("SELECT * from data");
if($q->num_rows() > 0) {
foreach ($q->result() as $row) {
$data [] = $row;
}
return $data;
}
}
}
views:
home.php
<htmL>
<body>
<div> view has been loaded</div>
<!--<p> <?php echo $myValue; ?> </p>
<p> <?php echo $anotherValue; ?> </p> -->
<pre>
<?php foreach ($rows as $r) {
echo '<h1>' . $r->title . '</h1>';
}
?>
</pre>
</body>
</htmL>
controllers:
site.php
<?php
Class Site extends CI_Controller {
function __construct()
{
// Call the Model constructor
parent::__construct();
}
function index(){
$this->load->model('data_model');
$data['rows'] = $this->data_model->getAll();
$this->load->view('home', $data);
}
}
Always make sure that you extends your model with CI_Model for it will be recognized .
class Model extends CI_Model {
//you can always put function construct
public function __construct (){
parent::__construct ();
}
}
In your controller :
class Sample extends CI_Controller {
public function __construct () {
parent:: __construct();
//you can load here the model that you will just often so will load it everytime to use it in a function
$this->load->model('nameofModel');
}
}
Remember : The name of the model or controller must be the same with its filename.
Make sure your class extends the base Model class. In CodeIgniter this is "CI_Model"...
class Data_model extends CI_Model {
// Your model class code here...
}
make sure you have already load Model
for that:
1.open application/config/autoload.php
and
2. edit
$autoload['model'] = array(''); with
$autoload['model'] = array('Model');
thats works
Related
I'm doing an app and I'm using a base controller to create others ones. When I launch a controller I see that error:
Fatal error: Class 'BaseController' not found in C:\xampp\htdocs\sFitness\api\1-dev\controllers\PeopleController.php on line 3
The problem is that in the same folder I've BaseController.php
following I send you the PeopleController.php:
<?php
class PeopleController extends BaseController {
public function processGetRequest($request) {
$result = NULL;
$result = $this->_model->getAllAthletes();
return $result;
}
}
?>
following the BaseController.php
<?php
abstract class BaseController {
public $_model;
public function __construct() {
$this->_model = new Model();
}
}
?>
How can I resolve this problem? What's the problem?
Thanks so much
You should import your other file so php can see it;
in PeopleController, do the following:
<?php
require_once "BaseController.php";
class PeopleController extends BaseController
{
public function processGetRequest($request)
{
$result = NULL;
$result = $this->_model->getAllAthletes();
return $result;
}
}
?>
I am a beginner in codeigniter and currently I am working on a shopping cart, taking help from http://net.tutsplus.com/tutorials/php/how-to-build-a-shopping-cart-using-codeigniter-and-jquery/ tutorial. I am using codeigniter 2.1.3.
I am getting an error:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Cart::$load
Filename: controllers/cart.php
Line Number: 7
Fatal error: Call to a member function model() on a non-object in D:\xampp\htdocs\ci\application\controllers\cart.php on line 7
Can someone please tell me why it is not working?
The name of my controller is cart.php
<?php
class Cart extends CI_Controller {
public function Cart()
{
//parent::CI_Controller(); // We define the the Controller class is the parent.
$this->load->model("cart_model"); // Load our cart model for our entire class
}
public function index()
{
$data['products'] = $this->cart_model->retrieve_products(); // Retrieve an array with all products
print_r($data['products']);
//$data['content'] = 'cart/products'; // Select view to display
//$this->load->view('index', $data); // Display the page
}
}
?>
and my model is cart_model.php
<?php
class Cart_model extends CI_Model{
//public function _construct(){
//parent::_construct();
//}
public function retive_products(){
$query = $this->db->get("products");
return $query->result_array();
}
}
/* End of file cart_model.php */
/* Location: ./application/models/cart_model.php */
?>
Codeigniter 2.1.3 is intended to support PHP 5.2.4 and newer.
Change the class constructor:
<?php
class Cart extends CI_Controller {
public function __construct()
{
parent::__construct();
}
instead of
public function cart()
{
parent::CI_Controller();
Try to review your code. You calling the retrieve_products function on model but in model you have retive_products function .
Controller
public function index() {
$data['products'] = $this->cart_model->retrieve_products(); // Retrieve an array with all products
print_r($data['products']);
//$data['content'] = 'cart/products'; // Select view to display
//$this->load->view('index', $data); // Display the page
}
Model
public function retive_products() {
$query = $this->db->get("products");
return $query->result_array();
}
I'm using the following from the controller to call a method from the model but receiving and error:
//from the controller:(main.php)
<?php
class Main extends CI_Controller {
public function __construct() {
parent::__construct();
}
function index() {
.....
$this->load->view('view_form');
}//END Fn index()
function get_th() {
//$the=$this->input->post('th', TRUE);
$d['shit']=$this->model_data->tst();
$this->load->view('view_form',$d);
}//END Fn get_th()
}//END Cls Main
?>
//from the model:(model_data.php)
<?php
class Model_data extends CI_Model {
function slider() {
...
}//END Fn slider()
function check_input($data) {
...
}//END Fn check_input()
function tst() {
$tsts= "hellos";
return $this->tsts;
}
}//END Cls model_data
?>
$autoload['model'] = array('model_data');
The error:
Fatal error: Call to undefined method Model_data::tst() in ... application\controllers\main.php...
i think you forgot to load the model in the controller.
$this->load->model('Model_name');
function get_th() {
$this->load->model('model_data');
$d['shit']=$this->model_data->tst();
$this->load->view('view_form',$d);
}
FIXED :/ can t believe I had an additional bracket at the the end of a long file :( wtf
"}"<-- this was the problem.
btw as I said #pramodhkumar use autoload.php.. I had the model autoload so no need for $this->load->...
MY_Controller.php (application/core) :
<?php
class MY_Controller extends CI_Controller
{
public $data = array();
function __construct()
{
parent::__construct();
$this->data['errors'] = array();
$this->data['my_site_name'] = config_item('site_name');
}
}
Frontend_Controller.php (application/libraries)
<?php
class Frontend_Controller extends MY_Controller
{
function __construct()
{
parent::__construct();
}
}
pages.php (application/controllers)
class Pages extends Frontend_Controller {
function __construct()
{
parent::__construct();
$this->load->model('app_model');
}
public function index()
{
$this->view_data['main_content'] = 'frontpage';
$this->load->view('template', $this->view_data);
}
}
pages is set as default route in routes and if I try to load a HMVC module called testmodule like http://localhost/testmodule it works nice.
But, when I want to echo this module (index method contains just one line
echo "Welcome from testmodule!"; ) like:
<div class="page">
<?php $this->load->view('includes/block_left') ?>
<div id="content">
<div>
<?php
echo modules::run('testmodule');
?>
<?php $this->load->view('articles_new'); ?>
</div>
</div><!-- /content -->
</div>
I get this error:
Fatal error: Cannot redeclare class CI in C:\wamp\www\application\third_party\MX\Base.php on line 57
EDIT:
If I tried to change
class MY_Controller extends CI_Controller
to
class MY_Controller extends MX_Controller
I got this error:
An Error Was Encountered
Unable to load the requested file:
Any idea what is going wrong? ;(
index_model.php is below:
<?php
class index_model extends CI_Model {
function __construct() {
parent::__construct();
}
function getVideo()
{
$query = $this->db->get('videolar');
return $query->result_array();
}
}
?>
And index controller is below, too
<?php
class index extends CI_Controller {
function __construct() {
parent::__construct();
}
function index()
{
$this->load->model('index_model');
$data['video'] = $this->index_model->getVideo();
$this->load->view('index',$data);
}
}
?>
When I call index controller it returns this error
Fatal error: Call to a member function get() on a non-object in
/var/www/atlet/application/models/index_model.php on line 10
I set database in autoload.php.
$autoload['packages'] = array('database');
You need to load the database in the libraries array, not the packages array:
$autoload['libraries'] = array('database');