Codeigniter 'Message: Undefined index: site_lang' issue at first load - php

I have developed a website and it is working fine in my localhost.
But I wanted to test it in a local area network before going live.
There is an issue I am facing which is at first load in a browser or any other machine it shows below error until I change the language once. When I change the language once then the issue is never happening again.
I am new and this is my first project so can anyone help me in this regard.
Thanks in advance
A PHP Error was encountered
Severity: Notice
Message: Undefined index: site_lang
Filename: models/companies.php
Line Number: 15
Backtrace:
File: C:\xampp\htdocs\fp\application\models\companies.php
Line: 15
Function: _error_handler
File: C:\xampp\htdocs\fp\application\controllers\home.php
Line: 19
Function: get_companies
File: C:\xampp\htdocs\fp\index.php
Line: 315
Function: require_once
Here is my Model:
//---- Table Companies ---
public function get_companies()
{
// Retrieve titles for all languages
$sql = "SELECT * FROM `ci_companies` WHERE `co_id` = 1";
// Retrieve appropriate title according to the chosen language in the system
$sql = "SELECT `co_id`, `co_name`, SUBSTRING(`co_detail`,1,100) AS `co_detail`, `co_img`, `co_img2` FROM `ci_companies` WHERE `co_lang` = '".$_SESSION['site_lang']."'";
$query = $this->db->query($sql);
return $query->result();
}
And here is my controller:
function switchLang($language = "") {
$language = ($language != "") ? $language : "english";
$this->session->set_userdata('site_lang', $language);
redirect($_SERVER['HTTP_REFERER']);
}

Try like this.error due to wrong use of single quotes.
$language = $this->session->userdata('site_lang');//or $_SESSION['site_lang']
$sql = "SELECT `co_id`, `co_name`, SUBSTRING(`co_detail`,1,100) AS `co_detail`, `co_img`, `co_img2` FROM `ci_companies` WHERE `co_lang` = '{$language}'";

Related

Error after making virtual host

I have a project developed in Codeigniter. It works fine when I try to access it on the browser at localhost using localhost/bmlivenew. But after making its virtual host, when I access it on the browser using www.bmlivenew.com , it displays the website but at the top of it, it shows an error
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: designer_images
Filename: controllers/Designs.php
Line Number: 42
Backtrace:
File: /var/www/html/bmlivenew/application/controllers/Designs.php
Line: 42
Function: _error_handler
File: /var/www/html/bmlivenew/index.php
Line: 324
Function: require_once
Whereas, the variable $designer_images is properly defined and it does not throw such an error if I try to access the website using localhost/bmlivenew.
Bunch of codes where I have defined the $designer_images in the Designs controller.
$designers = $this->interior_images_m->get_by($cond);
foreach ($designers as $key => $designer) {
$designer_images = $this->interior_new_m->get_by(array('id'=>$designer->name));
$designers[$key]->images = $designer_images;
}
$this->data['names'] = $designer_images;
$this->data['designers'] = $designers;
// Load View
$this->load->view("frontend/ourdesign",$this->data);
Any help is welcome.
Hope this will help you :
initialise $designer_images first, above foreach loop
Should be like this :
$designers = $this->interior_images_m->get_by($cond);
$designer_images = NULL;
foreach ($designers as $key => $designer)
{
$designer_images = $this->interior_new_m->get_by(array('id' => $designer->name));
$designers[$key]->images = $designer_images;
}
$this->data['names'] = $designer_images;
$this->data['designers'] = $designers;

When registering a new account on my site this error pops up. Everything else works fine

A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: models/Email_model.php
Line Number: 187
Backtrace:
File: /public_html/application/models/Email_model.php Line: 187
Function: _error_handler
File: public_html/application/controllers/Home.php Line: 1409
Function: send_code_sms
File: /public_html/index.php Line: 292 Function: require_once
Controller:
if ($this->input->post('password1') == $this->input->post('password2')) {
$password = $this->input->post('password1');
$data['password'] = sha1($password);
$this->db->insert('vendor', $data);
$this->email_model->account_opening('vendor', $data['email'], $password);
$id = $this->db->insert_id();
*$this->email_model->send_code_sms('vendor', $id);*
echo 'done';
}
Model:
//email_verify
function send_code_sms($type,$id){
$code = $this->db->get_where($type,array($type.'_id' => $para2))->row()->sms_code;
*$phone = $this->db->get_where($type,array($type.'_id' => $para2))->row()->phone;*
$text = translate('your_verification_code_is')." : ".$code;
$this->do_sms($phone,$text);
}
View:
require_once BASEPATH.'core/CodeIgniter.php';
I am having this error on my site. I Sent an email to my developer to fix the errors causing my customers to be unable to register accounts but I have not received any replies in 5 days. The sms verification feature is not supposed to used yet because of no api. So the only thing supposed to be working is email verification. Whenever someone tries to register, the error above appears.
Need Help. Thanks
(There is an asterisk to indicate the line with the error)

Codeigniter undefined property on core/Model.php

Controller
public function editItem(){
$this->load->helper('form');
$this->load->model('ItemModel');
$data['items'] = $this->ItemModel->itemlist();
$item_details = $this->ItemModel->edititem($this->input->get('id'));
$data2['item_name'] = $item_details->name; //THIS IS LINE 28
$data2['item_description']= $item_details->description;
$data2['item_price'] = $item_details->price;
$this->load->view('item/item_edit',$data2);
}
There's an error to my view, and I don't know why
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Item::$name
Filename: core/Model.php
Line Number: 77
Backtrace:
File: C:\xampp\htdocs\itwa213\application\controllers\Item.php
Line: 28
Function: __get
File: C:\xampp\htdocs\itwa213\index.php
Line: 292
Function: require_once
I already checked my autoload on config and it's properly configured with
$autoload['libraries'] = array('database');
You could try this
public function editItem() {
$this->load->helper('form');
$this->load->model('ItemModel');
$data['items'] = $this->ItemModel->itemlist();
$item_details = $this->ItemModel->edititem($this->input->get('id'));
$data2['item_name'] = $item_details['name']; //THIS IS LINE 28
$data2['item_description'] = $item_details['description'];
$data2['item_price'] = $item_details['price'];
$this->load->view('item/item_edit',$data2);
}
Your controller function change to like this
public function editItem(){
$this->load->helper('form');
$this->load->model('ItemModel');
$data['items'] = $this->ItemModel->itemlist();
$data2['item_details'] =$this->ItemModel->edititem($this->input->get('id'));
$this->load->view('item/item_edit',$data2);
}
and in your view page
echo $item_details->name;
You should look at your code for the get function you use. try to replace the $this->input->get('id') to existing id number first. check whether it success or not. If success by using existed id number, so it should your get function not working or something wrong. i'm seldom use $this->input->get() but $this->input->post().
on your edit link, you can use:
edit.
so the id is the third segment uri. use $this->uri->segment(3); to get the third segment and store into $item_id variable into your controller. so, no need to use $this->input->get(); to get the item_id value.

Calling model from codeigniter library shows error

Hello I have created a library Auth.php in application/libraries to authenticate if the current user is logged in or not. So my controller is :
$session_data = $this->session->userdata('logged_in');
$this->load->library('Auth');
$auth = new Auth;
$user_id = $auth->authenticate($session_data);
And the library :
class Auth{
function authenticate($vars){
$CI =&get_instance();
$CI->load->model('adminmodels/login_model');
$username = $vars['username'];
$password = $vars['password'];
$user_id = $vars['user_id'];;
$user_type = $vars['user_type'];
$check_login = $this->login_model->login($username,$password); //Line 14
if($check_login){
$user_id = $user_id;
}else{
$user_id = 0;
}
return $user_id;
}
}
But it is showing error like :
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Auth::$login_model
Filename: libraries/Auth.php
Line Number: 14
Whats wrong I am doing ??
While correct generally, in CI you should call the library like this, not with the new keyword:
$this->load->library('Auth');
$user_id = $this->auth->authenticate($session_data);
Also, since you assigned the global CI object to a variable, you can't use $this to refer to it:
$check_login = $this->login_model->login($username,$password); //Line 14
should be:
$check_login = $CI->login_model->login($username,$password); //Line 14
(since you loaded the model there: $CI->load->model('adminmodels/login_model'); )
Inside Codeigniter library file, you cannot load any model, config, or any external library using $this by pass without CI instance object/variable
For your case in Auth.php at line 14, you are wrong :
$check_login = $this->login_model->login($username,$password); //Line 14
It should be like this (if access from internal function only) :
$check_login = $CI->login_model->login($username,$password); //Line 14
Or, if access from any functions in the Auth class :
$check_login = $this->CI->login_model->login($username,$password); //Line 14
But first, you must make CI intance object in your construct function like this :
$this->CI =&get_instance();
$this->CI->load->model('adminmodels/login_model');
before you action in second alternative above.
May help you...
Thanks

Issue with loading user class

I have a small problem with the following php code. On the input for the name, I'm trying to show their current username, by $user->username, only it gives me the following error:
Notice: Undefined variable: user in /home//domains//public_html/dev/edit_account.php on line 36
Notice: Trying to get property of non-object in /home//domains//public_html/dev/edit_account.php on line 36
in the game_header.php file I have
$user = new User($_SESSION['id']);
And thought it would work, but sadly it does not.
I have also tried
$user = new User($_SESSION['id']);
on the edit_account.php page, but I was getting the same error.
Here's the edit_account.php code.
Does anyone know what I might be doing wrong here?
include "game_header.php";
$_GET['type'] = isset($_GET['type']) && ctype_alpha($_GET['type']) ? trim($_GET['type']) : '0';
switch($_GET['type']) {
case 'profileoptions' : profile_options(); break;
default : profile_options();
}
function profile_options() {
echo '
';
include 'game_footer.php';
}
When encased into a function you must do the following:
global $user ; //Bring a global variable to the current scope.
echo $user->username ; //Then you can access it and its properties.
So it must start with:
function profile_options() {
global $user ;
//The rest of code
}
However, I recommend you to create a parameter:
function profile_options(User $user){
//Much code
}
Then call it where $user is accessible:
profile_options($user) ;

Categories