How to stay on previous page after signin in codeigniter framework - codeigniter

Hii guys i'm new in stackoverflow , if this question is very long please pardon me, This is a Consumer complain website here is a view page called separate_user_admin_reply2.php in this page i want to apply a functionality, If a user without login click on the replay button then Signin.php view page will appear then after login the user come back to the previous page, the previous page link is : http://localhost/A2Zcomplainboard/index.php/Complain/Separate_single_complain_user/COMP008
So what i can do ..please help me (thanks in advance)
The controller is :
public function Separate_single_complain_user($cid)
{
//$cid = $this->input->get('compid');
$this->load->model('Complain_model');
$res = $this->Complain_model->getSingleComplain($cid);
$this->load->model('Replay_Model');
$res1 = $this->Replay_Model->Separate_single_complain_replay($cid);
if($res){
$this->load->view('separate_user_admin_reply2',['res'=>$res,'res1'=>$res1]);
}
}
The model is:
public function getSingleComplain($cid)
{
$complain = $this->db->query("SELECT comp_id,comp_title,comp_description,post_datetime,status,publish_status,comp_catagory,posted_by,id,first_name,email FROM log_tbl l INNER JOIN comp_tbl c ON l.id = c.posted_by WHERE comp_id = '$cid'");
$res = $complain->result_array();
if($res)
return $res;
else
return false;
}
public function Separate_single_complain_replay($cid){
$rep = $this->db->query("SELECT first_name,rep_id,rep_description,rep_datetime,rep_by,rep_against,rep_flag,rating FROM log_tbl l INNER JOIN reply_tbl r ON l.id = r.rep_by WHERE r.rep_against = '$cid 'AND rep_flag = '1'");
$res = $rep->result_array();
if($res)
return $res;
else
return false;
}
enter image description here
This is the view page image
separate_user_admin_reply2.php
And the signin controller is :
public function index()
{
$email = $this->input->post('email');
$password = $this->input->post('password');
$this->load->model('Login_Model');
$data = $this->Login_Model->isvalidate($email,$password);
if($data)
{
$this->load->library('session');
$this->session->set_userdata('email',$data['email']);
$this->session->set_userdata('type',$data['type']);
$this->session->set_userdata('id',$data['id']);
if($data['type']=='User' ){
return redirect('User/index');
}
else
{
return redirect('Admin/adminDashboard');
}
}else{
echo "<script>alert('Details not matched')</script>";
header("refresh:1");
return redirect('Complain/login');
}
}
model:-
public function isvalidate($email,$password)
{
$q = $this->db->where(['email'=>$email,'password'=>$password])
->get('log_tbl');
if($q->num_rows()){
$x = $q->row()->email;
$y = $q->row()->type;
$z = $q->row()->id;
$userData = array('email' => $x, 'type' => $y, 'id' => $z);
return $userData;
}else{
return false;
}
}
signin view page image:

Store the previous page url in session data. You can find a detailed description of how to do that in the codeigniter documentation. When the user successfully logged in, check if the previous page data is set, if so flush it and redirect to it.

Related

How can I store the web push notification settings after the login in database

I want to send web push notification on the browser. I used this tutorial to
send the notification. This is working fine and show the details.
{
"endpoint":"https://fcm.googleapis.com/fcm/send/ftB1OYn5bJY:APA91bGNcquGDcUXr29JiVV5Zos4Vi7FzmZ_wJQMITEXt8FlVBRBtgrPdLnPR6GALtnCOe9RNPP1cmC_bkv9D1BE1o6_-0cMXQsodpPoRCeOP5EDt6EwqK0ys36MbCi3HNTWf7ZcItVi",
"expirationTime":null,
"keys":{"p256dh":"BLJQqNovnlJ28d5xteX8whwdby6l0BYLvC_iyNtY2nO7YXQSI-EOvdOs1LXy8F_EuH2MZi0FU_HoCO-5GRQYYVQ",
"auth":"tDcEgiy5M5tJ3_vXuuQ9uw"}
}
but I want to integrate with my Laravel API.
After the user login, I want to save the endpoint, public key, and auth
to the database.
Login Controller
public function authenticate(Request $request)
{
$credentials = $request->only('username', 'password');
// return $credentials;
$response = array(
'status' => 'Failed',
'msg' => '',
'is_success' => false,
'data' => ''
);
try {
if (!$token = JWTAuth::attempt($credentials)) {
$response["msg"] = "Wrong Username or Password";
$response["status"] = "Failed";
$response["is_success"] = false;
} else {
if (Auth::user()->is_active == 0) {
$response["msg"] = "Your account has not been activated";
$response["status"] = "Failed";
$response["is_success"] = false;
} else {
$data = array();
$user = User::find(Auth::user()->id);
$data['id'] = $user->id;
$data['fname'] = $user->fname;
$data['lname'] = $user->lname;
$data['email'] = $user->email;
$data['username'] = $user->username;
$response["msg"] = "Login Successfully";
$response["status"] = "Success";
$response["data"] = compact('token');
$response["user"] = Auth::user();
}
}
} catch (\Exception $th) {
$response["msg"] = $th->getMessage();;
$response["status"] = "Failed";
$response["is_success"] = false;
}
return $response;
}
I think the best way to solve this, is to make another model for that data, a one to one relationship between user model and push notification data model. Make a controller for CRUD operations on this new model, and just have another http call from the front end to store the data.

What to Show some data On my view but there is a Error In Laravel

Here I am trying to add, Add Comment feature and it's causing a problem to show data.
public function store(Request $request)
{
$chatterreply = new Chatterreply;
$chatterreply->reply = $request->body;
$chatterreply->chatter_post_id = $request->chatter_post_id;
$chatterreply->chatter_discussion_id = $request->chatter_discussion_id;
$chatterreply->save();
$chatterreplies = Chatterreply::where('chatter_post_id',$request->chatter_post_id)->get();
$chatter_editor = config('chatter.editor');
if ($chatter_editor == 'simplemde') {
// Dynamically register markdown service provider
\App::register('GrahamCampbell\Markdown\MarkdownServiceProvider');
}
echo "<pre>"; print_r($chatterreplies); die;
return view('chatter::discussion', compact('chatterreplies','chatter_editor'))->with('chatter_alert','Add Comment Successfully');
}
And Here Is Where I am passing the variable
$chatter = session()->pull('chatter');
return view('chatter::discussion', compact('discussion', 'posts', 'chatter_editor'))->with('chatterreplies',$chatter);

Explain the functionality or meaning of this : $data['row']

I have this doubt.
// This is my controller
public function profile()
{
$session_data = $this->session->userdata('logged_in');
$uid= $session_data['id'];
$pic = $this->user_model->getImage($uid);
$data['profile_pic']= $pic ;
$data['row'] = $this->user_model->get_user_data($uid);
$test = $this->load->view('profile_view',$data,TRUE);
echo $test;exit;
}
Based on name it seems to be:
public function profile()
{
$session_data = $this->session->userdata('logged_in');//checking user login or not
$uid= $session_data['id'];//getting id from session
$pic = $this->user_model->getImage($uid);//geting user image
$data['profile_pic']= $pic ;//storing image to data array
$data['row'] = $this->user_model->get_user_data($uid);//passing $uid to model and store in data array what row model returns
$test = $this->load->view('profile_view',$data,TRUE);//passing data array to view
echo $test;exit;
}
//model
i am passing user id only
public function get_user_data($uid){
//echo $uid;exit;
$data = array();
$this->db->select('*');
$this->db->from('user');
$this->db->where('id',$uid);
$query = $this->db->get();//checking that $uid data in database
if($query->num_rows() > 0){
$get_user_data=$query->result();
//print_r($get_user_data);exit;
//print_r($items);exit;
return $get_user_data; // if find, return data to controller
}
else{
return FALSE;
}
//return $query->row();
}

links in pagination in codeigniter

in my view page,in the 1st form i have 2 select drop down...in the 1st drop down i am populating the value default from the controller...when u onchange the value in the 1st drop down,the selected value is passed to controller via javascript..in the controller,i get that 1st drop down value and load model and get value from model for the 2nd drop down and post it to view page...when u select the 2nd drop down value and click submit,the both drop down values are posted to controller after form validation and load model and get user information from the database and post it back again to view page ...this is the scenario for my view page..so,u can change the above both select drop down to get informations in the 2nd form in the same view page..now when i did pagination for my 2nd form user information,i am getting the links and data perfectly according to the limits and offsets..but,i am unable to retrieve the information when i click 2nd,3rd,4th..and rest on links while the informations are being posted from controller..so what can i do now?,,here is my code after i get both drop down values in the controller..
in my controller..
public function get_form_dept()
{
$this->load->helper(array('form', 'url'));
$this->load->library("pagination");
$this->load->library('form_validation');
$this->form_validation->set_rules('formation','Formation','required|required');
if($this->form_validation->run()== false) {
$this->viewstudent();
} else {
$config['base_url'] = base_url() . 'Incite/get_form_dept';
if($this->input->post('formation') == 1 && $this->input->post('department') == 1){
$config['total_rows'] = $this->db->get('user')->num_rows();
} else {
//$this->db->select('list_formation');
$query = $this->db->get_where('formation',array('id' => $this->input->post('formation')));
// echo $this->db->get_where('formation',array('id' => $this->input->post('formation')));
$row = $query->result();
foreach($row as $key) {
$get_formation = $key->list_formation;
echo $get_formation ."<br>";
}
$query1 = $this->db->get_where('department',array('id' => $this->input->post('department')));
$row1=$query1->result();
foreach($row1 as $key) {
$get_dept=$key->list_department;
echo $get_dept . "<br>";
}
//$array = array('formation' => $get_formation, 'department' => $get_dept);
//$config['total_rows']=$this->db->get('user',$array)->num_rows();
$query = $this->db->query("SELECT * FROM user where formation='$get_formation' and department='$get_dept'");
echo "SELECT * FROM user where formation='$get_formation' and department='$get_dept'" . "<br>";
$config['total_rows']=$query->num_rows();
//echo $row ."<br>";
//echo $row=$this->db->get('user',$array)->num_rows();
}
$config['per_page'] = 5;
//$config['uri_segment'] = 3;
//$choice = $config['total_rows'] / $config['per_page'];
// $config['num_links'] = round($choice);
$config['num_links'] = 2;
//$config['use_page_numbers'] = TRUE;
$config['suffix']= '?' . http_build_query($_GET, '', "&");
$this->pagination->initialize($config);
if($this->input->post('formation')== 1 && $this->input->post('department') == 1) {
// $this->db->limit($limit, $start);
$query_result = $this->db->get('user',$config['per_page'], $this->uri->segment(3));
$data['result']= $query_result->result();
} else {
$query = $this->db->get_where('formation',array('id' => $this->input->post('formation')));
$row = $query->result();
foreach($row as $key) {
$get_formation = $key->list_formation;
}
$this->db->where('id', $this->input->post('department'));
$query1 = $this->db->get('department');
$row1=$query1->result();
foreach($row1 as $key) {
$get_dept=$key->list_department;
//echo $get_dept;
}
$array = array('formation' => $get_formation, 'department' => $get_dept);
//$this->db->limit($limit, $start);
//$query = $this->db->get_where('user',$array);
$query_result=$this->db->get_where('user',$array,$config['per_page'], $this->uri->segment(3));
$data['result'] = $query_result->result();
}
$this->load->model('model_select_formation');
$data['formation'] = $this->model_select_formation->modelselectformation();
// query to fetch department
$data['dept']=$this->model_select_formation->get_department($data['formation_id']);
$data['formationid'] = $this->input->post('formation');
$data['departmentid'] = $this->input->post('department');
$this->load->view('viewstudent',$data);
}
}

How to prevent code duplication for CodeIgniter form validation?

This is sample of function in the Staff controller for this question
function newStaff()
{
$data = array();
$data['departmentList'] = $this->department_model->list_department();
$data['branchList'] = $this->branch_model->list_branch();
$data['companyList'] = $this->company_model->list_company();
$this->load->view('staff/newstaff', $data);
}
function add_newStaff()
{
//when user submit the form, it will call this function
//if form validation false
if ($this->validation->run() == FALSE)
{
$data = array();
$data['departmentList'] = $this->department_model->list_department();
$data['branchList'] = $this->branch_model->list_branch();
$data['companyList'] = $this->company_model->list_company();
$this->load->view('staff/newstaff', $data);
}
else
{
//submit data into DB
}
}
From the function add_newStaff(), i need to load back all the data from database if the form validation return false. This can be troublesome since I need to maintain two copy of codes. Any tips that I can use to prevent this?
Thanks.
Whats preventing you from doing the following
function newStaff()
{
$data = $this->_getData();
$this->load->view('staff/newstaff', $data);
}
function add_newStaff()
{
//when user submit the form, it will call this function
//if form validation false
if ($this->validation->run() == FALSE)
{
$data = $this->_getData();
$this->load->view('staff/newstaff', $data);
}
else
{
//submit data into DB
}
}
private function _getData()
{
$data = array();
$data['departmentList'] = $this->department_model->list_department();
$data['branchList'] = $this->branch_model->list_branch();
$data['companyList'] = $this->company_model->list_company();
return $data;
}
Alternately you change the action your form submits to so that it points to the same service you use for the initial form request with something like the following. This would also mean that you'd have the POST values retained between page-loads if you wanted to retain any of the submitted values in your form.
function newStaff()
{
// validation rules
if ($this->validation->run() == TRUE)
{
//submit data into DB
}
else
{
$data = array();
$data['departmentList'] = $this->department_model->list_department();
$data['branchList'] = $this->branch_model->list_branch();
$data['companyList'] = $this->company_model->list_company();
$this->load->view('staff/newstaff', $data);
}
}

Resources