Whats wrong? Message: Undefined property - php

This is the error:
Severity: Notice Message: Undefined property: Products::$product_model Filename: controllers/Products.php Line
Number: 29
This is my Controller code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Products extends CI_Controller {
function __construct()
{
parent::__construct();
if(!$this->session_data()):
redirect(base_url());
endif;
$this->load->model('User_model');
$this->load->model('Admin_model');
$this->load->model('product_model','products');
}
function session_data()
{
return $this->session->userdata('ADMIN_SESSION');
}
function show($page,$data = array(),$str = '')
{
$userdata = $this->session_data();
$data['setting'] = $str;
$data['admin'] = $this->Admin_model->get_id($userdata['empcom_id']);
$data['cat'] = $this->product_model->get_cat();
$this->load->view($page,$data);
}
function index()
{
$this->show('template/header');
$this->show('admin/admin_menus');
$this->show('product_view');
$this->show('template/footer');
}

If you would like your model assigned to a different object name you can specify it via the second parameter of the loading method:
$this->load->model('model_name', 'foobar');
$this->foobar->method();
refernce : https://www.codeigniter.com/userguide3/general/models.html#loading-a-model
in your method you have assigned a different name for your product_model
$this->load->model('product_model','products');
$data['cat'] = $this->product_model->get_cat();
change that to
$data['cat'] = $this->products->get_cat();

Related

How to display headlines in marquee in codeigniter php

I am new in codeigniter framework of PHP. I want display the news headlines in scrolling marquee I tried this
view page
<marquee bgcolor="black"><font color="white">
<?php
foreach($news as $row)
{
$heading=$row->st_head;
echo $heading;
}
?>
</font ></marquee>
controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Headlines extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('Headlines_model');
}
public function index()
{
$where_cond = "flg_is_active = 1";
$select_cond = "st_head,in_news_id";
$data['news'] = $this->Headlines_model->select_records($where_cond,$select_cond );
$this->load->view('Headlines',$data);
}
}
*****Model **************
<?php
class Headlines_model extends CI_Model
{
function __construct()
{
parent::__construct();
$this->load->database();
$this->table_name = 'tbl_news';
}
public function select_records($where=NULL,$select=NULL,$jointable=NULL,$joinkey=NULL,$per_page=NULL,$limit=NULL,$order_by=NULL, $count = false)
{
if($where!=NULL)
$this->db->where($where);
if($select!=NULL)
$this->db->select($select);
if($jointable!=NULL && $joinkey!=NULL)
$this->db->join($jointable, $joinkey);
if($limit!=NULL || $per_page!=NULL)
$this->db->limit($limit, $per_page);
if($order_by!=NULL)
$this->db->order_by($order_by);
$query = $this->db->get($this->table_name);
if($count==true)
{
return $query->num_rows();
}
else
{
return $query->result();
}
}
}
?>
if I display without marquee the show the data properly. But when I use marquee then it gives following error
PHP Error was encountered
Severity: Notice
Message: Undefined variable: news
Filename: views/Headlines.php
Line Number: 2

A PHP Error was encountered Severity: Notice Message: Undefined index: questions

<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class checklist extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->helper('form');
$this->load->model('checklist_model');
}
public function index()
{
$data['check_list'] = $this->checklist_model->get_all_details();
$this->load->view('show_checklist', $data);
}
public function form_dropdown()
{
$this->load->view('insert_checklist');
}
public function data_submitted()
{
$data = array('dropdown_single' => $this->input->post('Technology'));
$this->load->model('checklist_model');
$this->checklist_model->insert_in_db($data);
$this->load->model("checklist_model");
$result = $this->checklist_model->read_from_db($data);
$data['result'] = $result[0];
$this->load->view('insert_checklist', $data);
}
public function add_form()
{
$this->load->view('insert_checklist');
}
public function edit()
{
$id = $this->uri->segment(3);
$data['details'] = $this->checklist_model->getById($id);
$this->load->view('edit', $data);
}
public function delete($id)
{
$this->checklist_model->delete_a_detail($id);
$this->index();
}
public function insert_new_details()
{
$udata['technology_name'] = $this->input->post('technology_name');
$udata['questions'] = $this->input->post('questions');
$udata['status'] = $this->input->post('status');
$udata['comments'] = $this->input->post('comments');
$res = $this->checklist_model->insert_details_to_db($udata);
if($res)
{
header('location:'.base_url()."/index.php/checklist/".$this->index());
}
}
public function update()
{
$mdata['technology_name']=$_POST['technology_name'];
$mdata['questions']=$_POST['questions'];
$mdata['status']=$_POST['status'];
$mdata['comments']=$_POST['comments'];
$res=$this->checklist_model->update_info($mdata, $_POST['id']);
if($res)
{
header('location:'.base_url()."/index.php/checklist/".$this->index());
}
}
}
?>
it means you call for an array variable's index 'question', and that index does not exist in that array. Maybe in function update(), $_POST['question']. Not sure. You should provide the full error msg, like file and line.
Also: the view if that's the case. And make this sound more like a question.

Store model function return to controller function

I have a model which returns a username of the person that has logged into the website to a controller. I am trying to save the username into a variable which i can user to then insert back into another table, however i am having no luck saving the data. Below is my model and controller classes.
Model:
function is_loggedin()
{
$session_id = $this->session->userdata('session_id');
$res = $this->db->get_where('logins',array('session_id' => $session_id));
if ($res->num_rows() == 1) {
$row = $res->row_array();
return $row['name'];
}
else {
return false;
}
}
Part of my Controller:
public function index()
{
$loggedin = $this->authlib->is_loggedin();
if ($loggedin === false)
$this->load->view('login_view',array('errmsg' => ''));
else
{
$this->load->view('postquestion_view',array('username' => $loggedin));
$user = $loggedin['username'];
}
}
public function askquestion()
{
$qtitle = $this->input->post('title');
$qdetails = $this->input->post('details');
$qtags = $this->input->post('tags');
$qcategory = $this->input->post('category');
$quser = $user;
Error:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: user
Filename: controllers/postq.php
Line Number: 47
Here the error message is very clear. The variable $user in the last line of the function -action- askquestion() snippet is not defined. Basically, you have to read more about variables scope.
In your current situation, the code of index action should be in constructor and the variable user should be an object property. i.e it should defined globally in your controller's class and then takes its value from the constructor something like the following general demo:
<?php
class Blog extends CI_Controller {
public $user = false;
public function __construct()
{
parent::__construct();
// Your own constructor code
}
public function askquestion()
{
$qtitle = $this->input->post('title');
$qdetails = $this->input->post('details');
$qtags = $this->input->post('tags');
$qcategory = $this->input->post('category');
$quser = $this->user; //NOTICE THIS LINE
}
?>

Codeigniter: Load model and controller result in Helper

No idea what is the right way. But what I am trying to do is to make one helper function for positions list or dropdown (will set parameter for this) so that's how position list/dropdown can available to the entire system by writing single code `get_positions($type)'
The problem is not loading Model/Controller variables.
Error message:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: positions
Filename: helpers/content_helper.php
Line Number: 14
Helper
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// Position List
if ( ! function_exists('get_positions'))
{
function get_positions()
{
$CI =& get_instance();
$CI->load->model('admin/hr/hr_model');
if(count($positions)):
echo form_open();
$array = array();
foreach($positions as $position):
$label[] = $position->label;
$pos[] = $position->position;
endforeach;
echo form_dropdown('positions', array_combine($pos, $label));
echo form_close();
endif;
}
}
Model
<?php (defined('BASEPATH')) OR exit('No direct script access allowed');
class HR_Model extends MY_Model
{
protected $_table_name = 'positions';
protected $_order_by = 'label ASC';
public $rules = array();
public function get_new()
{
$position = new stdClass();
$position->position = '';
$position->label = '';
return $position;
}
public function get_positions($id = NULL, $single = FALSE)
{
$this->db->get($this->_table_name);
return parent::get($id, $single);
}
}
Controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class HR extends MX_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('admin/hr/hr_model');
}
public function index()
{
// Fetch all pages
$this->data['positions'] = $this->hr_model->get_positions();
// Load view
$this->load->view('hr/index', $this->data);
}
}
Eventually what I am looking is to create helper function which available to entire system by calling get_positions($type)
You have to define the $positions variable before if (count($positions)):. You should call the get_positions method from the Model.

Model class not found in not found in C:\wamp\www\redirect\system\core\Loader.php on line 303

I am getting error when calling the model from controller.
Error:- SCREAM: Error suppression ignored for
( ! ) Fatal error: Class 'Redirection_model' not found in C:\wamp\www\redirect\system\core\Loader.php on line 30
My controller redirection.php
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class redirection extends CI_Controller {
function __construct()
{
parent::__construct();
//$this->layout->setLayout('layouts/main');
}
public function redirection_url()
{
if($_GET){
$get_array = $_GET;
$targetId = $get_array['tagid'];
$this->load->model('redirection_model', 'redirection');
$dynamicurl = $this->redirection->getDynamicUrl($targetId);
if($dynamicurl){
$url=$dynamicurl['url'];
redirect($url,'refresh');
}
else{
echo "Invalid Request";
}
}
else{
echo "please pass the tagid";
// $this->layout->view('redirection/redirection_url');
}
}
}
redirection_model.php
<?
class redirection_model extends CI_Model {
function __construct()
{
parent::__construct();
}
function getDynamicUrl($targetId){
$dynamicUrl= array();
$query = $this->db->get_where('dynamic_url', array('tagid' => $targetId))->row();
if($query){
$dynamicUrl['url'] = $query->url;
$dynamicUrl['userid'] = $query->userid;
return $dynamicUrl;
}
else{
return false;
}
}
}
?>
you have not linked your view in controller, insert it.
And Also check you route file which is in your config folder, there you have to make entry for for your functions and route for view.

Categories