My CodeIgniter project does not work in server - php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller
{
function __costruct(){
parent::__costruct();
$login = $this->session->userdata('login');
if(!empty($login)){
if($login!='valid'){
} else {
redirect('login/index');
}
} else{
redirect('login/index');
}
}
protected $template = array();
public function layout($arg = array()) {
$this->template['header'] = $this->load->view('theme/header_theme', $arg, true);
$this->template['header_menu'] = $this->load->view('theme/header_menu_theme', $arg, true);
$this->template['sidebar'] = $this->load->view('theme/sidebar_theme',$arg, true);
$this->template['content'] = $this->load->view($this->content, $arg, true);
$this->template['footer'] = $this->load->view('theme/footer_theme',$arg, true);
$this->load->view('theme/index_theme', $this->template);
}

If I am reading it right, and if that is the code used, the mistake is a simple spelling mistake.
function __costruct(){
parent::__costruct();
should have been
function __construct(){
parent::__construct();
Anyways, it is always better to use the server in development mode to check the same!!!

Related

How to call a custom sibling method of custom helper in codeigniter and use database

EDIT
<?php defined('BASEPATH') OR exit('No direct script access allowed');
function getCrud($table_name, $subject)
{
$CI =& get_instance();
$CI->load->database();
$crud = new grocery_Crud();
$crud->set_table($table_name);
$crud->set_subject($subject);
$crud->unset_read();
$crud->unset_clone();
$crud->where(array($table_name.'.flag' => '1'));
$crud->callback_delete(function ($primary_key) use ($table_name) {
return $CI->db->update($table_name, array('flag'=>'0'), array('id'=>$primary_key));
});
return $crud;
}
function oneToMany($table_name, $subject, $rel_table,$field='name')
{
$crud = getGrocreyCrud($table_name, $subject);
$crud->set_relation($rel_table.'_id', $rel_table, 'name', array('flag' => '1'));
$output = $crud->render();
return $output;
}
I can not call the above function getCrud using the ci get instance.
Are there any other methods ??
return $CI->db->update($table_name, array('flag'=>'0'),
array('id'=>$primary_key));
when using these line of codes in controller ($CI will be $this in controller) I am able to set the flag to 0 but here in helper its not happening
Probably following should work
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
if(!function_exists('getCrud'))
{
function getCrud($table_name, $subject)
{
// some code
}
}
if(!function_exists('oneToMany'))
{
function oneToMany()
{
$CI =&get_instance();
$crud = getCrud($table_name, $subject);
return $crud ;
}
}
class Gc_script
{
public $CI;
public function __construct()
{
$this->CI =& get_instance();
$this->CI->config->item('base_url');
}
public function getGrocreyCrud($table_name, $read=null)
{
$crud = new grocery_Crud();
$crud->set_table($table_name);
$crud->set_subject(ucwords(str_replace('_', ' ', $table_name)));
$crud->callback_delete(function ($primary_key) use ($table_name) {
return $this->CI->db->update($table_name, array('flag'=>'0'), array('id'=>$primary_key));
});
return $crud;
}
}
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
function getCrud($table_name, $subject)
{
// some code
}
function oneToMany()
{
$CI =&get_instance();
$crud = $CI->getCrud($table_name, $subject);
return $crud ;
}
try this

CI HMVC: How to retrieve params from Modules::run()

How do I retrieve (and process) the params I have being passed in from this...
Modules::load('MembersList', $this->input->get(NULL, TRUE);
... into the module its being passed into?
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class MemberList extends MX_Controller {
function __construct()
{
parent::__construct();
$this->load->model('memberslist_model');
}
public function index()
{
// $params = how_do_i_retrieve it?
if ( ! isset($list) || $list == 'individuals')
{
$data['module'] = 'Individuals';
$data['results'] = $this->memberslist_model->list_individuals();
$data['count'] = count($data['results']);
$data['view'] = $this->load->view('list_individuals', $data, TRUE);
}
if ( $list == 'families')
{
$data['module'] = 'Families';
}
return $this->load->view('memberslist', $data, TRUE);
}
public function settings()
{
$data['settings'] = $this->memberslist_model->settings();
return $this->load->view('settings', $data, TRUE);
}
}
I understand that it is being passed as URI segments like codeigniter but I've tried everything and I can't get it to work.
I'm not sure which version you use, but you can try this:
$whatEver = Modules::run('MembersList/index', $this->input->get(NULL, TRUE));
class MemberList extends MX_Controller {
function __construct()
{
parent::__construct();
$this->load->model('memberslist_model');
}
public function index($params)
{
print_r($params);
// $params = how_do_i_retrieve it?
if ( ! isset($list) || $list == 'individuals')
{
$data['module'] = 'Individuals';
$data['results'] = $this->memberslist_model->list_individuals();
$data['count'] = count($data['results']);
$data['view'] = $this->load->view('list_individuals', $data, TRUE);
}
if ( $list == 'families')
{
$data['module'] = 'Families';
}
return $this->load->view('memberslist', $data, TRUE);
}
public function settings()
{
$data['settings'] = $this->memberslist_model->settings();
return $this->load->view('settings', $data, TRUE);
}
}

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.

Auth Component's user function not working in case multiple routing

I have routed my site as admin moderator. Means When I use Auth component my sessions are created as Auth->Admin->id & Auth->Moderator->id And Auth->User->id this way.
Now I tried function $this->Auth->user its returning null why??
As I know $this->Auth->user returns session.
I know the traditional way of $this->Session->reads('Auth.Admin') and else but I want to use Auth->user function why its not working
Please use this to get user session data.
public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
$this->redirect($this->Auth->redirect());
} else {
$this->Session->setFlash(__('Invalid username or password, try again'));
}
}
}
public function index() {
pr($this->Auth->User());
}
auth_helper:<?php
defined('BASEPATH') OR exit('No direct script access allowed');
if ( ! function_exists('auth'))
{
function auth()
{
$CI =& get_instance();
$CI->load->model('Auth_model');
return $CI->Auth_model;
}
function Adminauth($admin_id)
{
if($admin_id == false)
{
redirect('login');
}
}
}
auth_model:
<?php
if (!defined('BASEPATH')) {
exit('No direct script access allowed');
}
class Auth_model extends CI_Model {
private $user = null;
private $ci = null;
public function __construct() {
parent::__construct();
$CI =& get_instance();
$this->ci = $CI;
}
public function get($name, $default = null){
if ($this->ci->session->has_userdata('auth.Adminuser_id')) {
$user_id = $this->ci->session->userdata('auth.Adminuser_id');
$this->user = $this->db->query("SELECT * FROM members WHERE u_id=? LIMIT 1", array($user_id))->row();
}
return !empty($this->user) ? $this->user->u_name : $default;
}
public function is_logged(){
return $this->ci->session->has_userdata('auth.Adminuser_id');
}
public function login($user_id){
return $this->ci->session->set_userdata('auth.Adminuser_id', $user_id);
}
public function logout(){
return $this->ci->session->unset_userdata('auth.Adminuser_id');
}
}

PHP Web Counter

I'm trying to do a web counter, that count number who visit my website.
I use a txtfile as a counter, however when I start loading my web there was an error encounter.
Message: file_put_contents() [function.file-put-contents]: Filename cannot be empty
<?php if(! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends CI_Controller{
function _construct(){
parent::__construct();
}
function index(){
//Function Web Counter (count visitor)
$filename = 'counterePOD.txt';
function inc_count() {
global $filename;
if (file_exists($filename)){
$current_value = file_get_contents($filename);
}else{
$current_value = 0;
}
file_put_contents($filename, ++$current_value);
}
inc_count();
}
$this->load->view('login_view');
}
}
?>
Somebody can tell what is the exact meaning of the error msg: "Filename cannot be empty"??
You should use a more OOP approach and pass the filename to the inc_count function:
<?php if(! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends CI_Controller{
function _construct(){
parent::__construct();
}
function inc_count($filename) {
if (file_exists($filename)){
$current_value = file_get_contents($filename);
}else{
$current_value = 0;
}
file_put_contents($filename, ++$current_value);
}
function index(){
//Function Web Counter (count visitor)
$this->inc_count('counterePOD.txt');
$this->load->view('login_view');
}
}
?>
Pass the filename as an argument of the function like this. using global variable is not a good practice..
//Function definition
function inc_count($filename) {
if (file_exists($filename)){
$current_value = file_get_contents($filename);
}else{
$current_value = 0;
}
file_put_contents($filename, ++$current_value);
}
//Function call
$filename = 'counterePOD.txt';
inc_count($filename);

Categories