get an php fatal error in codeigniter controller - php

Here it is my code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->database(); /* This function it's used to connect to database */
$this->load->model('User','user'); /* This call the model to retrieve data from db */
}
public function index()
{
if(!file_exists('application/views/_login.php'))
{
show_404();
}
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<h4 style="text-align:center;">','</h4>');
$this->form_validation->set_rules('username','username','trim|required|xss_clean');
$this->form_validation->set_rules('password','password','trim|required|xss_clean|callback_pass_check');
if($this->form_validation->run() == FALSE)
{
$data['title'] = "User Access";
$data['author'] = "Salvatore Mazzarino";
$this->load->view('templates/_header',$data);
$this->load->view('_login',$data);
$this->load->view('templates/_footer',$data);
}
else
{
redirect('home', 'refresh');
}
}
public function pass_check($pass)
{
$result = $this->user->find_user($this->input->post('username'),$pass);
if($result == 1)
{
$session_array = array();
foreach ($result as $row)
{
$session_array = array('id'=> $row->id,'username'=> $row->username); /* Create a session passing user data */
$this->session->set_userdata('logged_in',$session_array);
}
return TRUE;
}
else
{
$this->form_validation->set_message('pass_check',"Invalid username or password!</br>Try again, please!");
return FALSE;
}
}
}
Here it is the line
$this->session->set_userdata('logged_in',$session_array);
that cause the error:
PHP Fatal error: Call to a member function set_userdata() on a non-object
Someone could say me the reason?

Make sure the session library is loaded.
You can add it in application/config/autoload.php to be loaded automatically.
http://ellislab.com/codeigniter/user_guide/libraries/sessions.html

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.

How do i properly create a function in a helper file in codeigniter

I am trying to learn code igniter and creating helper files. I have this as my functions_helper.php file located in my applications/helpers folder:
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
function __construct()
{
parent::__construct();
$this->load->model('user','',TRUE);
}
if (!function_exists('check_status')){
function check_status()
{
$CI = & get_instance();
$result=$CI->user->get_status(); //this is line 14!!
if($result)
{
$status_array=array();
foreach($result as $row)
{
$status_array=array(
'source' => $row->status,
'current' => $row->id
);
if ($status_array['current'] == 1) {
$status_array['current'] = "Live";
} else {
$status_array['current'] = "Amazon is Down";
}
$CI->session->set_userdata('status',$status_array);
}
return TRUE;
}
else
{
return false;
}
} // END OF CHECK_STATUS FUNCTION
}
From everything i can find, I am doing it correctly, yet I am getting this error:
PHP Fatal error: Call to a member function get_status() on a non-object in function_helper.php on line 14. exactly what am i doing wrong? Is this the best way to call a function? Ultimately I am trying to get information returned from a db query.
My get_status function is:
//FUNCTION TO SEE THE STATUS OF AMAZON
function get_status() {
$query = $this->db->query("select * from amz where active = 1");
if($query->num_rows()==1) {
return $query->row_array();
}else {
return false;
}
} //END OF GET_STATUS FUNCTION
Remove this code
function __construct()
{
parent::__construct();
$this->load->model('user','',TRUE);
}
There is no OO in helper files. You do not have a class -__construct() are used to initialize classes.
Then move
$this->load->model('user','',TRUE); to the check_status function
$CI =& get_instance();
$CI->load->model('user','','TRUE');

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.

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');
}
}

Categories