Displaying Flash data in php codeigniter - php

I am struggling with flash data in codeigniter. My edit function is working properly. But problem is with delete function.My controller is as follows
public function rmRole()
{
$data = array();
$roleId = $this->uri->segment(4,0);
if ($this->_delete($roleId)) {
$this->session->set_flashdata('flash_message' ,'data_deleted');
redirect('adminroles');
}
}
My model includes
public function _delete($id) {
$table = $this->get_table();
$this->db->where('id', $id);
$this->db->delete($table);
}
My data is got deleted. But it is redirecting to a blank page with the url. What shall i do to display a flash data like data deleted successfully and redirect to adminroles module?

try this in model
function delete($id) {
if ($id)
{
if ($this->db->where("id", $id)->delete("table name")){
return TRUE;
}
else{
return FALSE;
}
}else{
return FALSE;
}
}

Related

Laravel 5 return redirect is not working as it should

I'm having a controller with some functions. In every function I get user data by sharing it from the Contoller.php
In controller.php
public function share_user_data() {
$user = Auth::user();
$this->checkValidation($user);
$this->user = $user;
View::share('user', $user);
}
public function checkValidation($user){
if($user->email_activated == 0){
var_dump($user->email_activated); // I get: int(0)
return redirect('/verifyEmail');
}
}
In the other controller
public function viewCategory(Category $category){
$this->share_user_data(); // here's the check
$this->get_site_lang();
$products = $category->products;
return view('category', compact('category','products'));
}
But I get the category view and not redirected to the verifyEmail route. How to fix this and why it's happening?
The controller function called by the route is the one responsible for the response. I guess it is viewCategory() in your example?
Your viewCategory() function is always returning view(). It must return redirect() instead. I think the main function should be responsible for picking the output of the request.
private function checkValidation($user) {
return $user->email_activated == 0;
}
public function viewCategory(Category $category) {
$user = Auth::user();
/* ... call to share_user_data() or whatever ... */
if ($this->checkValidation($user)) {
return redirect('/verifyEmail');
}
return view('category', compact('category','products'));
}

UserId is not displaying after successfull login in codeigniter

I am new to codeigniter I Have create a login form and i am able to login succesfully fine and after login i am getting the login username also fine.but when i try to get the userid it is showing empty.I am displaying userid just like username displaying .But it is displaying empty why.Please any help would be appreciated thanks in Advance.
This is my model
class Login_Model extends CI_Model{
public function checklogin($uname,$pass){
$this->db->select('id','username','password');
$this->db->get('login');
$this->db->where('username',$uname);
$this->db->where('password',$pass);
$query=$this->db->get('login');
if($query->num_rows()==1){
return true;
}else{
return false;
}
}
This is mycontroller
public function verifyuser(){
$name=$this->input->post('username');
$password=$this->input->post('password');
$this->load->model('Login_Model');
if($this->Login_Model->checklogin($name,$password)){
$this->Login_Model->checklogin($name,$password);
$this->session->set_userdata('username', $name);
$this->session->set_userdata('id', $id);
return true;
}else{
$this->form_validation->set_message('verifyuser','username or password is invalid');
return false;
}
}
This view
$id= $this->session->userdata('id');
echo "login id is".$id;
in View page i am accessing data like this but it giving empty for id but it is username fine.
It looks like you are just checking if the username and password match in the database in the checklogin function.
You are not returning the id and storing it in $id variable.
Hence since $id is undefined, no value is printed.
Changes required
Model
Fetch the id from the database and return it instead of a boolean.
class Login_Model extends CI_Model{
public function checklogin($uname,$pass){
$this->db->select('id','username','password');
$this->db->get('login');
$this->db->where('username',$uname);
$this->db->where('password',$pass);
$query=$this->db->get('login');
if($query->num_rows()==1){
$res = $query->row_array();
return $res['id'];
} else {
return false;
}
}
}
Controller
Fetch the id and use it to set the userdata.
public function verifyuser(){
$name=$this->input->post('username');
$password=$this->input->post('password');
$this->load->model('Login_Model');
$id = $this->Login_Model->checklogin($name,$password);
if($id){
$this->session->set_userdata('username', $name);
$this->session->set_userdata('id', $id);
return true;
} else {
$this->form_validation->set_message('verifyuser','username or password is invalid');
return false;
}
}
Your are getting userid but not storing it in session so you will not get this id
make model code like this
public function checklogin($uname,$password)
{
$this->db->select('user_id','user_email','user_password');
$this->db->from('users');
$this->db->where('user_email',$uname);
$this->db->where('user_password',$password);
$res = $this->db->get()->result_array();
if(!empty($res))
{
return $res[0]['user_id'];
}
else
{
return 0;
}
}
and controller code like this so you will get id
$name=$this->input->post('username');
$password=$this->input->post('password');
$user_id=$this->Login_model->checklogin($name,$password);
if($user_id > 0)
{
$this->session->set_userdata('username', $name);
$this->session->set_userdata('id', $user_id);
return true;
}
else
{
$this->form_validation->set_message('verifyuser','username or password is invalid');
return false;
}
You can make the following Changes in your Model.
class Login_Model extends CI_Model{
public function checklogin($uname,$pass){
$query = $this->db->select('id','username','password');
->where(['username'=>$uname,'password'=>$pass]);
->get('login');
return $query->row()->id;
}

Data doesn't display in Codeigniter

I am new learner of CI.
I want to display data from database. but it doesn't work.
Code in controller :
function __construct()
{
parent::__construct();
$this->load->database();
$this->load->model("user_model");
$this->lang->load('basic', $this->config->item('language'));
// redirect if not loggedin
if(!$this->session->userdata('logged_in')){
redirect('login');
}
$logged_in=$this->session->userdata('logged_in');
if($logged_in['base_url'] != base_url()){
$this->session->unset_userdata('logged_in');
redirect('login');
}
}
public function index($limit='0')
{
$logged_in=$this->session->userdata('logged_in');
if($logged_in['su']!='1'){
exit($this->lang->line('permission_denied'));
}
$data['limit']=$limit;
$data['title']=$this->lang->line('userlist');
// fetching user list
$data['result']=$this->user_model->user_list($limit);
$this->load->view('header',$data);
$this->load->view('user_list',$data);
$this->load->view('footer',$data);
}
public function teachercontrol()
{
$logged_in=$this->session->userdata('logged_in');
$sut='2';
$this->db->where('su',$sut);
// fetching user list
$data['teacher']=$this->user_model->teachermodel();
$this->load->view('header',$data);
$this->load->view('user_list',$data);
$this->load->view('footer',$data);
}
my model :
function teachermodel(){
$this->db->order_by('savsoft_users.uid','desc');
$this -> db -> join('savsoft_group', 'savsoft_users.gid=savsoft_group.gid');
$query=$this->db->get('savsoft_users');
return $query->result_array();
}
I try tro print in view, but it doesn't show any data :
<?php print_r($teacher)?>
What is the mistake?
while, the index function in controller (above the teachercontroller function) works well
Thanks
Put where condition in modal from teacher controller:
public function teachercontrol()
{
$logged_in=$this->session->userdata('logged_in');
$sut='2';
// fetching user list
$data['teacher']=$this->user_model->teachermodel($sut);
$this->load->view('header',$data);
$this->load->view('user_list',$data);
$this->load->view('footer',$data);
}
my model :
function teachermodel($sut = ""){
if($sut != "") {
$this->db->where('savsoft_users.su',$sut);
}
$this->db->join('savsoft_group', 'savsoft_users.gid=savsoft_group.gid', 'left');
$this->db->order_by('savsoft_users.uid','desc');
$query=$this->db->get('savsoft_users');
return $query->result_array();
}

how to send a receive array value to another model in codeigniter

public function user_delete($email){
//echo $email;
$this->load->model('hradvalid');
$user_id = $this->hradvalid->user_id($email);
$user_email=$user_id[0]->email;
$this->hradvalid->user_delete($email,$user_email);
if($this->hradvalid->user_delete()){
$this->session->set_flashdata("alert","your data deleted successfully");
}else{
$this->session->set_flashdata("alert","error comming sorry!");
}
return redirect('admin/dashboard');
}
model code
public function user_id($value)
{
$q = $this->db->select('email')
->from('user')
->where('id',$value)
->get();
return $q->result();
}
public function user_delete($value,$email)
{
$user_delete=$this->db->delete('user',array('id'=>$value));
$user_delete=$this->db->delete('acuser',array('username'=>$email));
return $user_delete;
}
above code is my controller I receive a value from hradvaild model and method user_id getting one value this is working now I want to send this value to hradvaild model in user_delete method here I am getting an error offset[0] error please help me and also $user_email undefined
Try this
In Controller
public function user_delete($email)
{
$this->load->model('hradvalid');
$user_id = $this->hradvalid->user_id($email);
$user_email = $user_id[0]['email'];
if(!$this->hradvalid->user_delete($email,$user_email)){ # Changed
$this->session->set_flashdata("alert","error comming sorry!"); # Changed
}else{
$this->session->set_flashdata("alert","your data deleted successfully"); # Changed
}
return redirect('admin/dashboard');
}
In Model
public function user_id($value)
{
$query = $this->db->select('email')
->from('user')
->where('id',$value)
->get();
return $query->result_array(); # Change this
}
public function user_delete($value,$email)
{
$user_delete=$this->db->delete('user',array('id'=>$value));
$user_delete=$this->db->delete('acuser',array('username'=>$email));
}

Not able to clear the variables in codeigniter

this is my controller:-
public function home() {
if($this->session->userdata('id')) {
$this->header();
$this->load->model('admincon');
$e = $this->admincon->select();
$data['e'] = $e;
$this->load->view('admin/home',$data);
} else {
$this->index();
}
}
in the view page of function home() there is a form which submitted through function subques() the function is here:-
public function subques() {
if($this->session->userdata('id')) {
$tp=$this->input->post('box1',TRUE);
$s=$this->input->post('box',TRUE);
$t=$this->input->post('t',TRUE);
$a=$this->input->post('a',TRUE);
$b=$this->input->post('b',TRUE);
$c=$this->input->post('c',TRUE);
$d=$this->input->post('d',TRUE);
$n=$this->input->post('n',TRUE);
$this->load->model('admincon');
$this->admincon->subque($s,$t,$a,$b,$c,$d,$n,$tp);
$this->home();
} else {
$this->index();
}
}
but after submitting the values to the database when user redirected to the page if they click on refresh button the previous data store into the database another time. how to solve this problem.
i mean how to clear the variables after use them.
User redirect() instead of $this->home()
public function subques()
{
if($this->session->userdata('id'))
{
$tp=$this->input->post('box1',TRUE);
$s=$this->input->post('box',TRUE);
$t=$this->input->post('t',TRUE);
$a=$this->input->post('a',TRUE);
$b=$this->input->post('b',TRUE);
$c=$this->input->post('c',TRUE);
$d=$this->input->post('d',TRUE);
$n=$this->input->post('n',TRUE);
$this->load->model('admincon');
$this->admincon->subque($s,$t,$a,$b,$c,$d,$n,$tp);
redirect('controller_name/home');
}
else
{
redirect('controller_name/index');
}
}
without this $this->home(); use
redirect(base_url() . 'controller_name/home');

Categories