I am still new at PHP and the MVC concept. I have been trying to duplicate the CI News Tutorial(http://codeigniter.com/user_guide/tutorial/news_section.html), while using my own database and rewriting the code.
I have been unsuccessful.
Here is my controller:
class Main extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->library('tank_auth');
$this->load->model('structures_model');
}
public function structures()
{
$this->load->model('structures_model');
if (!$this->tank_auth->is_logged_in()) {
redirect('/auth/login/');
} else {
$data['structures_all'] = $this->structures_model->get_structures();
$this->load->view('templates/header', $data);
$this->load->view('templates/navmain', $data);
$this->load->view('structures', $data);
$this->load->view('templates/footer', $data);
}
}
Here is my model (structures_model)
<?php
class Structures_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function get_structures()
{
$query = $this->db->get('structures');
return $query->result_array();
}
}
And my view:
<?php foreach ($structures_all as $structures_info): ?>
<h2>Structures</h2>
<div id="main">
<?php echo $structures_info['str_name'] ?>
</div>
<?php endforeach ?>
The error im getting is the common :
A PHP Error was encountered<
Severity: Notice
Message: Undefined variable: structures_all
Filename: main/structures.php
Line Number: 2
I am at a loss. I have looked at all the similar errors people have gotten, but can't figure out why exactly the structure_all array is not being defined. Shouldn't it get created in the controller function where I set :
$data['structures_all'] = $this->structures_model->get_structures();
What am I missing?
The best way to debug this would be to assign $data['structures_all'] a definite array value, say: $data['structures_all'] = array('foo' => 'bar');
Is the $structures_all variable available in the view now? If it is available, you know that $this->structures_model->get_structures(); is returning null.
Do you have a table in your database called structures?
Are you sure your database connection details are filled out in config/database.php?
Do you have php error reporting set to all? There might be hidden messages... call error_reporting(E_ALL); in the constructor of your controller.
Also try echoing: $this->db->last_query(); to verify your query is being constructed the same way you tried in phpmyadmin...
Hopefully this puts you on the right track.
Related
I'm using the CI framework and I'm trying to make a foreach loop so that all products of my site are displayed.
This is my controller file:
class AlleCadeausController extends CI_Controller {
public function index()
{
$this->load->model('allecadeaus_model');
$data['cadeaus'] = $this->allecadeaus_model->get_cadeaus();
$this->load->view('allecadeaus');
}
This is my model file:
<?php
class Allecadeaus_model extends CI_Model {
public function __construct()
{
parent::__construct();
}
public function get_cadeaus()
{
$query = $this->db->query('SELECT * FROM products');
$result = $query->result_array();
return $result;
}
}
this is my view:
<?php
foreach ($cadeaus as $cadeau)
{
echo $cadeau->product_naam;
}
?>
The error is:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: cadeaus
Filename: views/allecadeaus.php
Line Number: 3
Backtrace:
File: /home/ubuntu/workspace/application/views/allecadeaus.php Line: 3
Function: _error_handler
File:
/home/ubuntu/workspace/application/controllers/AlleCadeausController.php
Line: 11 Function: view
File: /home/ubuntu/workspace/index.php Line: 315 Function:
require_once
My table name is products
You havn't passed the $data variable in your controller. Please follow below code:
$this->load->view('allecadeaus', $data);
So, your controller function should be like below.
public function index()
{
$this->load->model('allecadeaus_model');
$data['cadeaus'] = $this->allecadeaus_model->get_cadeaus();
$this->load->view('allecadeaus', $data);
}
Cheers, you are done.
public function index()
{
$this->load->model('allecadeaus_model');
$data['cadeaus'] = $this->allecadeaus_model->get_cadeaus();
$this->load->view('allecadeaus',$data);
}
try this in you controller
You haven't pass data to the view you just load view.
for passing data to view you have to do
$this->load->view('allecadeaus',$data);
Here are good documentation for passing data from controller to view Contact ME
You should pass the data to the view follow the below code:
public function index()
{
$this->load->model('allecadeaus_model');
$data['cadeaus'] = $this->allecadeaus_model->get_cadeaus();
$this->load->view('allecadeaus, $data');
}
You have to pass $data with the view.
$this->load->view('allecadeaus',$data);
You need pass array of data in second argument.
Please use this :
$this->load->view('allecadeaus',$data);
If you load child view in allecadeaus, then you don't need to pass array of data. You can directly access all variables from array of data.
I haven't used CodeIgniter in over a year. I remember it was useful for quick, simple projects but I seem to have fallen at the first hurdle here. I can't seem to load my default view. Here is the controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends CI_Controller {
private $data = array();
public function __constructor() {
parent::__construct();
}
public function home() {
$this->load->view('header');
$this->load->view('nav');
$this->load->view('home');
$this->load->view('footer');
}
}
This gives me:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Home::$load
Filename: controllers/home.php
Line Number: 13
But I can't figure out why.
In my config I have set 'html', 'url' and 'form' to autoload. And my routes defaults correctly to 'home'. It's kinda frustrating because I know it's something really simple that I'm forgetting here.
Your __constructor is wrong. Use __construct instead of __constructor
public function __construct()
{
parent::__construct();
}
Your method has same name with the view you are calling which is home
I am seeing some odd behavior in my basic PHP codeigniter app.
My output looks like this:
number of rows:
Warning notice
undefined variable: rows
number of rows: 10
What is most peculiar is that it looks like it is trying to double execute my PHP code, but I cannot figure out why. Any insight is appreciated:
Code below:
model:
class foo extends CI_Controller
{
public function index()
{
$this->go();
}
public function go()
{
$this->load->model('model');
$data = array('rows'=> $this->model->count());
$this->load->view('view',$data);
}
}
model:
class model extends CI_Model
{
public function count()
{
$query = "Select count(1) as count from table";
$result = $this->db->query($query);
return $result->result_array();
}
}
view:
<html>
<body>
Number of rows: <?php print_r($rows[0]['COUNT']); ?>
</body>
</html>
it looks like your index function is firing and calling go() before go is called.
I would guess your htaccess rules are written incorrectly.
try changing your index function to this
public function index()
{
redirect('index.php/foo/go');
}
I want to declare a variable in a php class which can be used in all its functions. I am using the codeigniter framework & to have to pass data like base_url etc in each of the functions. Also I need all of such data in a single variable. For example $data['base_url']=base_url(), $data['title']="My_Site_title" etc & then send only a single variable $data (this is a common thing in codeigniter).
As per this question, I modified my code to:
class Search extends CI_Controller {
function Search() {
parent::__construct();
$this->load->model('student_model');
$this->load->helper('form');
$this->load->helper('url');
$this->output->enable_profiler(TRUE);
$this->data['base_url']=base_url(); //gives an error here
}
function index() {
$this->load->view('search_view', $this->data);
}
}
But still it gives an error at the marked place saying Undefined variable: data and Fatal error: Cannot access empty property in ..\search.php on line 16.
Here you go:
class Search extends CI_Controller {
protected $data = array();
function Search() {
parent::__construct();
$this->load->model('student_model');
$this->load->helper('form');
$this->load->helper('url');
$this->output->enable_profiler(TRUE);
$this->data['base_url']=base_url();
}
function index() {
$this->load->view('search_view', $this->data);
}
}
More about class properties in php: http://php.net/manual/en/language.oop5.properties.php
I'm trying to get familiar with CI and I have run into a problem while trying to implement my Model. I get the following error:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Home::$OrderModel
Filename: controllers/home.php
Line Number: 12
My assumption was that I was breaking some convention with the naming of the Model. If
I change the line in question and call the model using all lower case:
$data['query'] = $this->ordermodel->get_all_workorder_names();
Nothing is returned to the view.. blank page; no source, no error.
Here is my model:
<?php
class OrderModel extends Model{
function OrderModel()
{
parent::Model();
$db = $this->load->database();
}
function get_all_workorder_names()
{
$this->db->select('name');
$query = $this->db->get('WorkOrder');
return $query->result();
}
}
?>
This is the calling controller:
<?php
class Home extends Controller{
function Home()
{
parent::Controller();
$this->load->model('ordermodel');
$this->load->helper('url');
}
function index()
{
$data['query'] = $this->OrderModel->get_all_workorder_names();
$this->load->view('Header');
//$this->load->view('Home',$data);
$this->load->view('Footer');
}
}
?>
What am I doing wrong? A side question: Is there a debugger available for PHP in Eclipse?
Use $this->ordermodel. The variable name will match whatever you use with load->model()
For further reading, you can check out CodeIgniter's User Guide entry on loading models here.