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.
Related
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();
I want to load a function from another controller. This is my structure:
- modules
--orderpages
---controllers
----WebshopCore.php
----WebshopController.php
My function insertItemInCart in WebshopController.php is called. But when i want to execute a function from another controller it crashes.
class WebshopController extends MX_Controller {
public function __construct() {
parent::__construct();
$this->load->module('orderPages/WebshopCore');
}
function insertItemInCart(){
$partId = $this->input->post('partId');
$quantity = $this->input->post('quantity');
$output = $this->WebshopCore->getPickLocations($partId,$quantity);
}
}
My WebshopCore:
class WebshopCore extends MX_Controller {
public function __construct() {
parent::__construct();
}
public function getPickLocations($partId,$amount){
$result = "test";
return $result;
}
}
What goes wrong? I don't get it
The solution:
$output = modules::load('orderPages/WebshopCore/')->getPickLocations($partId,$quantity);
You should write a library in that case.
In application/libraries create Cart.php (or whatever you want)
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Cart
{
protected $ci;
public function __construct()
{
$this->ci =& get_instance();
}
public function getPickLocations($partId, $qty)
{
//Your stuff
}
}
And then in your controllers :
$this->load->library("cart");
$data = $this->cart->getPickLocations($this->input->post('partId'), $this->input->post('quantity'));
When I load a library in CI controller the class constructor is activated automatically, even if I didn't create an object just yet.
This is weird. can this be fixed via config or something? didn't find anything on the web.
My Controller:
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index()
{
$this->load->library('users',false);
$user = new Users();
$this->load->model('welcome_model');
$this->load->view('welcome_message');
}
}
My Class:
defined('BASEPATH') OR exit('No direct script access allowed');
class Users {
public function __construct($uname, $fname, $lname) {
print "Hello World!";
}
public function some_method() {
}
}
The code will output "Hello World" twice.
$this->load->library('myLib'); instanciate the library, so you don't need to use the new keyword.
$params_for_the_constructor = array(
'id' => 1,
'firstname' => 'Jemmy',
'lastname' => 'Neutron'
);
$this->load->library('users', $params_for_the_constructor);
$this->users->some_method();
class Users {
private $id;
private $firstname;
private $lastname;
public function __construct(array $params = array()) {
if (count($params) > 0) {
$this->initialize($params);
}
log_message('debug', "Users Class Initialized");
}
public function some_method() {
}
public function initialize(array $params = array()) {
if (count($params) > 0) {
foreach($params as $key => $val) {
if (isset($this->{$key}))
$this->{$key} = $val;
}
}
return $this;
}
}
https://ellislab.com/codeigniter/user-guide/general/creating_libraries.html
in Codeigniter when you call library or call helper or call a controller or a model or anything of these its just equivalent to the new key word.
i mean if you want to make a new object of user you just do the following.
just store the instance of your first user in a varaible.
//creating first instance
$user = $this->load->library('users', $first_user_parameters);
//creating second instance
$user2 = $this->load->library('users', $second_user_parameters);
// $this->load->library('users', $param) === $user = new User($param);
in codeigniter they convert the load to new keyword by the ci_loader
So... I pushed some code live the other day (that worked 100% fine on my local machine) but killed the server - no Codeigniter Logs, no Apache Logs, die('msg') and exit() did not work - I have never experienced this before in 5+ years of PHP development.
After 50+ commits to my repo I narrowed the problem down to one statement that works when split apart, but not together.
System Info:
PHP Version: 5.4.13
Codeigniter Version: define('CI_VERSION', '2.1.3');
These lines work (being called in a Codeigniter MY_Controller function):
dump($this->get_val('order_id'));
$tmp = $this->get_val('order_id');
dump($tmp);
dump(empty($tmp));
dump(!empty($tmp));
But when I add this following line the above described crash happens:
!empty($this->get_val('order_id'))
This seems like a PHP bug?
Structure:
Main.php
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Main extends Administration {
function index() {
// if (!empty($in['order_id'])) { /* BROKEN */
dump($this->get_val('order_id'));
$tmp = $this->get_val('order_id');
dump($tmp);
dump(empty($tmp));
dump(!empty($tmp));
// dump(!empty($this->get_val('order_id'))); /* BROKEN */
// if (!empty($this->get_val('order_id'))) { /* BROKEN */
// dump(true);
// } else {
// dump(false);
// }
}
}
adminstration.php
<?php
class Administration {
/**
*
* #var MY_Controller;
*/
public $ci;
public $p;
function __construct() {
$this->ci = & get_instance();
$this->ci->load->model('user/admin/user_admin_model');
$this->p = $this->ci->uri->uri_to_assoc(4);
}
protected function get_val($name = '') {
$pst = (array) $this->ci->input->post();
$gt = (array) $this->ci->input->get();
if (empty($name)) {
return array_merge($pst, $gt, $this->p);
}
if (!empty($this->p[$name]))
return $this->p[$name];
if (!empty($pst[$name]))
return $pst[$name];
if (!empty($gt[$name]))
return $gt[$name];
return array();
}
}
?>
My_Controller.php
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class MY_Controller extends CI_Controller {
protected $p;
function __construct() {
parent::__construct();
$this->p = $this->uri->uri_to_assoc();
}
function get_val($name = '') {
dump("I am get_val in MY_controller");
$pst = (array) $this->input->post();
$gt = (array) $this->input->get();
if (empty($name)) {
return array_merge($pst, $gt, $this->p);
}
if (!empty($this->p[$name]))
return $this->p[$name];
if (!empty($pst[$name]))
return $pst[$name];
if (!empty($gt[$name]))
return $gt[$name];
return array();
}
}
Prior to PHP version 5.5.0, empty only worked on variables, not on the returned value from a function or directly on the result of an expression
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.