Foreach loop in CodeIgniter - php

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.

Related

parser error: syntax error, unexpected $data (T_VARIABLE) in codeIgniter

I am newbie in CodeIgniter. When I try to data passing with controller and view and model, I have got this:
Syntax error: Unexpected '$data'(T_VARIABLE) in c:\xampp\htdocs\ci\application\controllers\users.php on line 10
Here is my controller users.php code:
<?php
class Users extends CI_Controller
{
public function show()
{
$data['results']=$this->user_model->get_users();
$this->load->view('user_view',$data);
}
}
?>
At views folder user_view.php code:
<body>
<?php
foreach($results as $object){
echo $object->username;
}
?>
</body>
At model folder user_model.php code
<?php
class User_model extends CI_Model
{
public function get_users()
{
$query=$this->db->get('users');
return $query->result();
}
}
?>
How can I solve this error?
Why do your error says c:\xampp\htdocs\ci\application\users.php shouldn't it have been c:\xampp\htdocs\ci\application\controller\users.php`
In your controller
public function show()
{
$data['results']=$this->user_model->get_users();
$this->load->view('user_view',$data);
}
In your model
public function get_users(){
$query=$this->db->get('users');
$result = $query->result_array(); //added
return $result; //added
}
In view
<body>
<?php
foreach($results as $new_results) //changed
{
echo $new_results['username']; //changed
}
?>
</body>
You are trying to access wrong ARRAY in your view.
In your controller array is $data['results'] not $data['result']. So you need to access $results in foreach loop.
In your view Change from
foreach($result as $object){
To
foreach($results as $object){
Use $results in foreach loop and check
First of all, you have to load your model as:
$this->load->model('User_model','user_model');
Then you can use the function defined in that model as :
$this->user_model->get_users();
You have one more error in your code i.e. you have not loaded the model in the function
update your code to add the following line in first line of show() function
$this->load->model('user_model');

Getting error in loading model in controller in codeigniter Message: Undefined property: Cart::$load

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();
}

Message: Undefined variable: in View page

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.

Error when trying to load view in my_controller

Consider my code:
<?php
class MY_Controller extends Controller {
public function __construct()
{
parent::Controller();
}
function _displayPage($page, $data = array()) {
$this->load->view('structure/header', $data);
$this->load->view($page, $data);
$this->load->view('structure/footer', $data);
}
}
?>
page.php
<?php
class Page extends MY_Controller {
function __construct() {
parent::__construct();
}
function index() {
$data['content'] = array('title'=>'hello world');
$this->_displayPage('home', $data);
}
}
?>
Upon loading my page on my browser, I get this error:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Page::$view
Filename: libraries/MY_Controller.php
Line Number: 11
Does anyone know what I'm doing wrong?
Thanks
In your library My_Controller you should be using the parent keyword instead of $this.
your code should look like so:
class MY_Controller extends Controller
{
public function __construct()
{
parent::Controller();
}
function _displayPage($page, $data = array())
{
parent::load->view('structure/header', $data);
parent::load->view($page, $data);
parent::load->view('structure/footer', $data);
}
}
If I understand what you're trying to accomplish correctly, you're wanting to setup a template that includes your header view and footer view, but without calling header and footer views for each controller you use throughout your application. Here's what I've done to accomplish this.
First, create a new folder under your views, for this example we'll call it 'includes'. Inside the newly created includes folder, create three files, header.php, footer.php and template.php. Setup your header and footer appropriately and then edit your template.php to look as follows:
<?php echo $this->load->view('includes/univ_header'); ?>
<?php echo $this->load->view($main_content); ?>
<?php echo $this->load->view('includes/univ_footer'); ?>
Now, from your controller you can define what view you would like to set as your 'main_content'. For example, if you have home.php in your views folder and you want to wrap it with your header and footer you would do so in your controller as follows:
function index() {
$data['content'] = array('title'=>'hello world');
$data['main_content'] = 'home';
$this->load->view('includes/template', $data);
}
Hope this helps!

How do I access my Models in CodeIgniter?

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.

Categories