I am trying to load model in my view file but problem is model is not loading and given error Message: Undefined property: CI_Loader::$CoupanCatModel
Following is my code
Controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class CoupanCategory extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('coupans/CoupanCatModel');
//$this->load->model('ForeignData_model');
}
public function init(){
$logoUrls = 'no-image.jpg';
$webUrls = 'no-image.jpg';
$mobileUrls = 'no-image.jpg';
$this->load->library('upload');
$config['upload_path'] = './assets/images/coupans/category/';
$config['allowed_types'] = 'gif|jpg|png';
$config['encrypt_name'] = TRUE;
$config['max_size'] = 20000;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('ccImage')){
$error = array('error' => $this->upload->display_errors());
}
else{
$data = $this->upload->data();
$logoUrls = $data['file_name'];
}
$is_init = $this->CoupanCatModel->init($logoUrls);
if ($is_init) {
redirect(base_url().'rech/'.ACCESS_KEY.'/coupans/coupan_category?create=success&success=New coupan category create','refresh');
}
else
{
redirect(base_url().'rech/'.ACCESS_KEY.'/coupans/coupan_category?create=failed&error=something want wrong','refresh');
}
}
}
Model
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class CoupanCatModel extends CI_Model {
public function init($logoUrls)
{
new_id:
$sc_id = 'SC'.mt_rand(1000,9999);
if ($this->check_id($sc_id))
goto new_id;
$sc_title = (isset($_POST['coupan_cat_name']) && $_POST['coupan_cat_name'] != '') ? trim($_POST['coupan_cat_name']):'';
$sc_logo = $logoUrls;
// $sc_web_image = $webUrls;
//$sc_mobile_image = $mobileUrls;
$active_flag =1;
$this->db->set('oc_cat_id',$sc_id);
$this->db->set('oc_cate_name',$sc_title);
//$this->db->set('sc_description',$sc_description);
// $this->db->set('sc_logo',$sc_logo);
$this->db->set('oc_web_image',$sc_logo);
//$this->db->set('sc_mobile_image',$sc_mobile_image);
$this->db->set('active_flag',$active_flag);
$this->db->insert('tbl_oc_category');
return true;
}
public function get_data($oc_cat_id = false)
{
if ($oc_cat_id != false) {
$this->db->where('oc_cat_id', $sc_id);
$query = $this->db->get('tbl_oc_category');
return $query->row_array();
}
$query = $this->db->get('tbl_oc_category');
return $query->result_array();
}
}
View i just load model like
<?php
$this->load->model('coupans/CoupanCatModel');
$scData = $this->CoupanCatModel->get_data();
echo '<pre>ddsd';
print_r($records);
die();
?>
But problem is model is not loading and given error
A PHP Error was encountered
Severity: Notice
Message: Undefined property: CI_Loader::$CoupanCatModel
Filename: coupans/coupan_category.php
Line Number: 139
For your own reference, you might want to read the Codeigniter Style Guide for filenames etc.
The code you have provided appears to be "dubious" in the fact there is no where in your code where you are loading the alleged "view".
So based upon what you have provided, I've come up with some Demo/Debug code.
Just note that your use of calling models inside views isn't quite correct in the scheme of MVC, nor is it entirely illegal.
So I've set this up on a Linux machine where filenames etc are case sensitive.
controllers/coupans/CoupanCategory.php
class CoupanCategory extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('coupans/CoupanCatModel');
}
public function index() {
echo "I am the controller ".__CLASS__;
$this->CoupanCatModel->init();
$this->load->view('coupans/coupan_category');
}
}
models/coupans/CoupanCatModel.php
class CoupanCatModel extends CI_Model{
public function init($logoUrls = '')
{
echo "<br>I am the ".__METHOD__;
}
public function get_data($oc_cat_id = false)
{
return "<br>I am the ".__METHOD__;
}
}
views/coupans/coupan_category.php
<?php
$this->load->model('coupans/CoupanCatModel');
$scData = $this->CoupanCatModel->get_data();
var_dump($scData);
And from out of all that you should get...
I am the controller CoupanCategory
I am the CoupanCatModel::init
string '<br>I am the CoupanCatModel::get_data' (length=37)
That should give you something to help solve your issue.
Related
<?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.
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
}
?>
This is my parent class code: \core\controller\controlmaster
<?php
namespace core\controller;
class controlmaster{
private $model_base = null;
//public function __construct(){
//always start session for any loaded controller
//session_start();
//}
public function _loadModels($models){
$file = dirname(dirname(__DIR__)).DIRECTORY_SEPARATOR.$this->model_base.$models.".php";
$file_alloc = str_replace("\\","/",$file);
if(file_exists($file_alloc)){
require($file_alloc);
$models_name = $this->model_base.$models;
return new $models_name();
}
}
public static function _setModelBase($model_location){
$this->model_base = $model_location;
}
}
?>
and this is my controller page for application : \applications\controller\index
<?php
namespace applications\controllers;
use core\configuration\configloader as config;
class index extends \core\controller\controlmaster{
public $config;
public function __construct(){
$this->config = new config;
$this->config->_parsePHP("j3mp_setting.php");
parent::_setModelBase($this->config->settings['app_model_base']); // Error doesn't appear when i comment this function
echo "This is main page and this is config for model app base : {$this->config->settings['base_url']}";
}
}
?>
This is core\configuration\configloader:
<?php
namespace core\configuration;
class configloader{
//Contains setting informations
public $inisettings;
public $settings;
public function _parseINI($file,$section){
$section = empty($section) ? false : true;
$file = __DIR__.DIRECTORY_SEPARATOR.$file;
if(file_exists($file)){
$parse = parse_ini_file($file,$section);
$this->inisettings = $parse;
}else{
throw new core\errorhandler\exception("File {$file} is not found in our system. Please contact administrator for details.","configloader");
}
}
public function _parsePHP($file){
$file = __DIR__.DIRECTORY_SEPARATOR.$file;
if(file_exists($file)){
$settings = array();
include($file);
$this->settings = $settings;
}else{
throw new core\errorhandler\exception("File {$file} is not found in our system. Please contact administrator for details.","configloader");
}
}
}
?>
When i comment "parent::_setModelBase(...)" code, the error disappear and the browser successfully print "This is main page and this is config for model app base : http://iosv3.net/". I think the error come from \core\controller\controlmaster, but I don't know how to fix that? I always try to edit the file (\core\controller\controlmaster) but notting happens... If error occured, the message ("This is main page ...") doesn't come out... Could you show me where is the error come from? Thanks...
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.
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.