Assigning object data to a view in Codeigniter gives weird behaviour - codeigniter

I've got a problem when assigning data to an object variable. In all my time, i haven't seen such weird behaviour.
I have a class:
class Main extends CI_Controller {
public $data = array();
public function __construct(){
parent::__construct();
/**
* Load Models
*/
$this->load->model('rss');
$this->load->model('model_product');
/**
* Default loaded Data
*/
$this->data['newsfeed'] = $this->rss->load($this->config->item('newsfeed'));
}
}
You see that i want to add the newsfeed to the $this->data object. It will held in a key named newsfeed. But now, i want this newsfeed on different pages. It is useless and inefficient to load this on every page, so i extended my main controller.
class Shop extends Main {
public function __construct(){
parent::__construct();
}
public function index()
{
$this->data['content'] = 'shop/default';
$this->load->view('template/index', $this->data);
}
public function product($id){
$this->data['product'] = $this->model_product->getProducts(null, $id);
if($this->data['product']->categoryid != $this->config->item('gamecategory')){
redirect('');
}
var_dump($this->data['product']);
$this->data['content'] = 'shop/product';
$this->load->view('template/index', $this->data);
}
}
The expected behaviour would be that $this->data would hold an array of objects (in each key ) but when i var_dump in the shop controller it gives me the entire $data object as return data, instead of the provided key. This totally blows my mind and i cant seem to understand this weird behaviour.
Expected var_dump ($this->data['product']):
Array(x){
key: value,
key: value,
.....
}
actual result:
Object{
newsfeed:
- data
product:
- data
....
....
}
Model rss:
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Rss extends CI_Model{
public function __construct(){
parent::__construct();
}
public function load($item){
return ($xml = simplexml_load_string(file_get_contents($item))) ? $xml : false;
}
}

Related

call another controller from dashboard using template pages

I working on Codeigniter project, I create page template to load header left menu and footer, everything working good, when I try the open link in the menu I want to open another controller. I do it but when the controller view open the variable inside to load database table for each row not working.. but the controller who load the database when I open it without my template its working fine
The Dashboard controller
class Dashboard extends CI_Controller{
protected $data = array();
function __construct()
{
parent::__construct();
$this->data['pagetitle'] = 'Invoices Manager';
}
protected function render($the_view)
{
$this->data['the_view'] = (is_null($the_view)) ? '' : $this->load->view($the_view,$this->data, TRUE);
$this->load->view('templates/master_page', $this->data);
}
public function home() {
// $this->load->view('templates/master_page', $this->data);
$this->render( 'templates/homepage_view');
}
public function dashboard() {
// $this->load->view('templates/master_page', $this->data);
$this->render( 'dashboard/home');
}
public function purchaselist(){
$this->render('purchase/index');
}
}
The purchase controller that working good alone
class Purchase extends CI_Controller{
protected $data = array();
protected $mydata = array();
function __Construct()
{
parent::__Construct ();
$this->load->database(); // load database
$this->load->model('Purchase_model'); // load model
$this->mydata['purchase']=null;
}
public function index()
{
$query = $this->Purchase_model->getPurchaselist();
if($query)
{
$mydata['purchase'] = $query;
}
$this->load->view('purchase/index', $mydata);
// $this->render( 'purchase/index');
}
}
when I call dashboard/purchaselist they say the
Message: Undefined variable: purchase
Filename: purchase/index.php
Line Number: 17
its should load database table inside the template
try using dashboard/index.php/purchaselist or load your modal $this->load->model('Purchase_model'); as global
Please pass null or empty array in purchase if $query is empty
public function index()
{
$query = $this->Purchase_model->getPurchaselist();
if(!empty($query)){
$mydata['purchase'] = $query;
}else{
$mydata['purchase'] = array();
}
$this->load->view('purchase/index', $mydata);
// $this->render( 'purchase/index');
}

Codeigniter global variable in view

I don't know if the title above is an correct title about my question.
Is there a way to declare a variable which we can access it from anywhere in view without need to redefine it again in each function in controller?
for example in controller file Students.php contains many function that handle the views, take a look below :
public function __construct() {
parent::__construct();
$data['count_student'] = $this->m_data->allStudent(); // count all students
}
public function index() {
$data['content'] = 'view-students';
$this->load->view('frontend/header' , $data);
}
public function showDetails() {
$data['content'] = 'view-detail-students';
$this->load->view('frontend/header' , $data);
}
I expected we can access $count_student in both view-students.php and view-detail-student.php without to define $data['count_student'] = $this->m_data->allStudent(); on each function that handle the view.
Is there possible ways to do that?
In the application/core/ create MY_Controller.php
<?php
class MY_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('m_data');
$this->data['count_student'] = $this->m_data->allStudent();
}
}
Controller use $this->data
class Students extends MY_Controller {
public function __construct() {
parent::__construct();
}
public function index() {
$this->data['content'] = 'view-students';
$this->load->view('frontend/header', $this->data);
}
public function showDetails() {
$this->data['content'] = 'view-detail-students';
$this->load->view('frontend/header', $this->data);
}
}
Now that you have extended the controller you shoudld be able to just go like
<?php echo $count_student;?>
On the view
I noticed that you can access variables in views without passing them if you declare them in the controller with $this->. Probably because they default to public visibility.
public function __construct() {
parent::__construct();
// $data['count_student'] = $this->m_data->allStudent(); // count all students
//
$this->count_all_the_students = $this->m_data->allStudents();
}
public function index() {
$data['content'] = 'view-students';
$this->load->view('frontend/header' , $data);
}
And then in the view you can use $this->count_all_the_students without putting it in the $data array.
<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
<?php
echo 'I have misled ' . $this->count_all_the_students . ' students with my MVC breaking suggestions.';
If you are using the same variable in every controller function then you have to declare it as follows
class Example extends CI_Controller{
protected $data = array();
public function __construct() {
parent::__construct();
$this->data['count_student'] = $this->m_data->allStudent();
}
public function index() {
$this->data['content'] = 'view-students'; //variables are same
$this->load->view('frontend/header' , $this->data);
}
public function showDetails() {
$this->data['content'] = 'view-detail-students'; //variables are same
$this->load->view('frontend/header' , $this->data);
}
}
This applies only if you have common variables for all views in this controller.
If you are using different variables then use the following code.
class Example extends CI_Controller{
protected $count_student= "";
public function __construct() {
parent::__construct();
$this->count_student = $this->m_data->allStudent();
}
public function index() {
$data['content'] = 'view-students'; //Variable is different
$data['count_student'] = $this->count_students;
$this->load->view('frontend/header' , $data);
}
public function showDetails() {
$data['details'] = 'view-detail-students'; // Variable is different
$data['count_student'] = $this->count_students;
$this->load->view('frontend/header' , $data);
}
}

`CI` return wrong value

I have no idea why my CI return wrong value from phpMyAdmin
Model :
<?php
class Post extends CI_Model
{
function __construct()
{
parent::__construct();
}
function getallpost()
{
return $this->db->get('post');
}
}
?>
And
Controller :
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Postc extends CI_Controller {
function index()
{
$this->load->model('post');
$posts=$this->post->getallpost();
echo '<pre>';
print_r($posts);
}
}
?>
enter image description here
You code is not giving you any error, it is doing exactly what you are asking it to do. Perhaps you need to be specific about the information you want to get. You see in your model
function getallpost()
{
return $this->db->get('post');
}
The above function will return you an object. If you want to get an array you need to write
return $this->db->get('post')->result_array();
And make sure you have some data in your post table to print For learning more about query database you should read Codeigniter's Query Builder Class
Change your Model Code
<?php
class Post extends CI_Model
{
function __construct()
{
parent::__construct();
}
function getallpost()
{
$query = $this->db->get('post');
if($query->num_rows() > 0)
{
return $query->result();
}else{
return false;
}
}
}
If you want to get return your result as as array then use
return $this->db->get('post')->result_array();
And if you want to get return your result as object then use
return $this->db->get('post')->result();

creating a model and controller to get category list from db using codeigniter

1.models/calists.php // My model file
Model file here i get the category list from database to the controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Catlists extends CI_Model
{
public function __construct()
{
$this->load->database(); //load database
}
public function getCategories()
{
$query = $this->db->get_where('category',array('status'=>'Enable'));
if ($query->num_rows() > 0)
{
return $query->result();
}
else
{
return array();
}
}
}
?>
2.controllers/catlist.php // controller file
Controller to get the data from model
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Catlist extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('catlists');
}
public function catlist()
{
$data['catlist'] = $this->catlists->getCategories();
$this->load->view('home', $data);
}
}
In header am printng the categories list
print_r($data['catlist']);
I am not sure what error you are getting here, but when you are loading model in controller, first letter should be capital.
$this->load->model('Catlists');
$data['catlist'] = $this->Catlists->getCategories();
In header you have to call
print_r($catlist);
The file name must match the class name.
For example
if your class is this:
class User_model extends CI_Model {
function __construct()
{
parent::__construct();
}
}
Your file will be this:
application/models/User_model.php
Here in you case your model name is
models/Calists.php
So your model file be
Model Calists.php
class Calists extends CI_Model {
function __construct()
{
parent::__construct();
}
public function getCategories()
{
$query = $this->db->get_where('category',array('status'=>'Enable'));
if ($query->num_rows() > 0)
{
return $query->result();
}
else
{
return array();
}
}
}
And you call this model file in you controller like this
Controller
public function __construct()
{
parent::__construct();
$this->load->model('calists');
}
CodeIgniter anatomy of model.

Codeigniter get specific record in table

I practicing CI and loop through all records in a view and produces each link as below:
sitename.com/products/2
where 2 is id of certain product, I want to click on the link and get this product information in product page, how can I create a model to retrieve specific record?
Model:
class Db_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function getProduct($id) {
$query = $this->db->get_where('tbl_product', array('id' => $id));
return $query->result();
}
}
Controller:
class Products extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('db_model');
}
public function index()
{
$data['rows'] = $this->db_model->getProduct($id);
$this->load->view('product', $data);
}
}
Thanks for help.
Your index function doesn't take $id as a parameter, So you need to modify your index function, Use the following index function, which I've edited.
public function index($id)
{
$data['rows'] = $this->db_model->getProduct($id);
$this->load->view('product', $data);
}

Resources