So here's what I want to do. I want to check if the userid in segment(3) exist or else it will redirect somewhere instead of still loading the view with an error.
Here's the example url
http://localhost/ems/edit_user/edit_user_main/1001
Now if I try to edit the userid in segment(3) and intentionally put an invalid userid, it still loads the view and i don't know why
Here's my function
public function edit_user_main(){
$id = $this->uri->segment(3);
$check = $this->get_data->check_if_exist($id);
if($check) {
$data['title'] = 'Edit User';
$data['id'] = $this->session->userdata('usertoedit');
$this->load->model('accounts/get_data');
$item = $this->get_data->get_user($id);
$data['user'] = $item[0];
$data['main_content'] = 'edit_user/edit_user_main';
$this->load->view('includes/template', $data);
} else {
redirect('admin/adminuser');
}
}
Here's the model
public function check_if_exist($id){
$query = $this->db->get_where('accounts',array('user_id'=>$id));
if($query) {
return TRUE;
} else {
return FALSE;
}
}
There is no problem with the fetching of data.
The problem is even if the userid doesn't exist, the view is still loading but with an error coz there's no data for that userID. It's not redirecting,
I tried using print_r and it working fine, the value of the $check is 1 when there's a valid userID.
Hope someone can help me with this. Thank you
With your function it will always return true because the statement
$this->db->get_where('accounts',array('user_id'=>$id));
will always execute,So you need to check query is returning any result row or not with the statement
$query->num_rows().
public function check_if_exist($id){
$query = $this->db->get_where('accounts',array('user_id'=>$id));
if($query->num_rows() > 0){ //change made here
return TRUE;
}
else{
return FALSE;
}
}
Try this..
With the function it will always return true because the following statement
$this->db->get_where('accounts',array('user_id'=>$id));
will always be execute, So need to check query is returning any result row or not
$query->num_rows().
public function check_if_exist($id){
$query = $this->db->get_where('accounts',array('user_id'=>$id));
if($query->num_rows() > 0){ //change made here
return TRUE;
}
else{
return FALSE;
}
}
And load heper as:-
$this->load->helper('url');
before the redirection
Related
I'm trying to delete from two tables using one function.
Controller code:
public function userdelete()
{
$u_id = $this->uri->segment(3);
$lr_id = $this->uri->segment(3);
$returndata = $this->user_model->user_delete($u_id, $lr_id);
if($returndata) {
$this->session->set_flashdata('successmessage', 'user deleted successfully..');
redirect('users');
} else {
$this->session->set_flashdata('warningmessage', 'Something went wrong..Try again');
redirect('users');
}
}
Modle code:
public function user_delete($lr_id, $u_id ) {
return $this->db->delete('login_roles',['lr_id'=>$lr_id]);
return $this->db->delete('login',['u_id'=>$u_id]);
}
I'm able to delete only from the first table but not the other one. this is working :
return $this->db->delete('login_roles',['lr_id'=>$lr_id]); but not return $this->db->delete('login',['u_id'=>$u_id]);.
As said in the comment you have to remove the first return.
You should compute the two results :
public function user_delete($lr_id, $u_id ) {
$delete1Response = $this->db->delete('login_roles',['lr_id'=>$lr_id]);
$delete2Response = $this->db->delete('login',['u_id'=>$u_id]);
return ($delete1Response AND $delete2Response);
}
It will returns true only if both are deleted
You even can go further and :
public function user_delete($lr_id, $u_id ) {
$delete1Response = $this->db->delete('login_roles',['lr_id'=>$lr_id]);
$delete2Response = $this->db->delete('login',['u_id'=>$u_id]);
return (object)array('role' => $delete1Response, 'user' => $delete2Response);
}
Then you can access to data like that :
$response = user_delete(...);
if ($response->role AND $response->user) {
// All fine
} else {
// One or both failed.
// Display error or do something
}
It never reaches the second $this->db->delete since its returns after executing the first one. Try:
public function user_delete($lr_id, $u_id ) {
if($this->db->delete('login_roles',['lr_id'=>$lr_id])){
//success, try the next one
return $this->db->delete('login',['u_id'=>$u_id]);
}
//failed
return false;
}
after I inserting data, what shows on the column table "Jumlah Hasil Perah" shows '0'. but after refreshing the browser, the value shows up the result.
here's the code
Model (m_hasilperah):
public function jumlahPerahSapi($id)
{
$this->db->select('hasilPerahPagi, hasilPerahSore');
$this->db->where('idSapi', $id);
$cek = $this->db->get('tb_hasilperah');
if ($cek) {
$this->db->set('jumlahPerah', "hasilPerahPagi + hasilPerahSore", FALSE);
$this->db->where('idSapi', $id);
$this->db->update('tb_hasilperah');
}
return false;
}
Controller
public function tambahHasilPerah(){
$idSapi = $this->input->post('idSapi');
$tglPerah = $this->input->post('tglPerah');
$hasilPerahPagi = $this->input->post('hasilPerahPagi');
$hasilPerahSore = $this->input->post('hasilPerahSore');
$jumlahPerah = $this->m_hasilperah->jumlahPerahSapi($idSapi);;
$data =
[
'idSapi' => $idSapi,
'tglPerah' => $tglPerah,
'hasilPerahPagi' => $hasilPerahPagi,
'hasilPerahSore' => $hasilPerahSore,
'jumlahPerah' => $jumlahPerah
];
$insert = $this->m_hasilperah->tambahHasilPerahModel($data, $idSapi);
if ($insert) {
redirect('C_hasilperah/tampilHasilPerah/' . $idSapi, 'refresh');
} else {
echo 'gagal';
}
}
Screenshot:
View after insert(before manual refresh)
View after manual refresh
Your Model method jumlahPerahSapi() every time returned false. Even if after the data update. Please look into this.
Your Model function jumlahPerahSapi($id) is always return false even inserted successfully in DB.
So This is not run redirect('C_hasilperah/tampilHasilPerah/' . $idSapi, 'refresh');
Every time run echo 'gagal';
Modify the model function as follows.
public function jumlahPerahSapi($id)
{
$this->db->select('hasilPerahPagi, hasilPerahSore');
$this->db->where('idSapi', $id);
$cek = $this->db->get('tb_hasilperah');
if ($cek) {
$this->db->set('jumlahPerah', "hasilPerahPagi + hasilPerahSore", FALSE);
$this->db->where('idSapi', $id);
$this->db->update('tb_hasilperah');
return true;
}
return false;
}
I have a model which fetch the data from database is below
public function counselor() {
$inst_id = $this->session->userdata('user_id');
$submission_key=$this->session->userdata('submission_key');
$query = $this->db->query("SELECT * FROM counselor where USER_ID = $inst_id AND submission_key= $submission_key");
$data = $query->num_rows();
if ($data > 0) {
return $data;
} else {
return false;
}
}
I have tested the $inst_id and $submission_key by printing it and its set.
$inst_id=2 and $submission_key=2016-8 .BUT though I have one record in database with those two field set its not returning the data. What is the case. I have tried with codeigniter get() and where() method too. Still not giving me the result.
Just write your query using active record function. It will help you in escaping string
$this->db->select('*',FALSE);
$this->db->where('USER_ID',$inst_id);
$this->db->where('submission_key',$submission_key);
$query=$this->db->get('counselor');
$data = $query->num_rows();
if ($data > 0) {
return $data;
} else {
return false;
}
I created a helper for returning a user's username if the user's unique id is known.
if ( ! function_exists('get_username'))
{
function get_username($user_id)
{
$ci=& get_instance();
$ci->load->database();
if (empty($user_id))
{
return FALSE;
}
$ci->db->select('username');
$ci->db->where('id', $user_id);
$ci->db->where('activated', 1);
$ci->db->where('banned', 0);
$ci->db->limit(1);
$query = $ci->db->get('users');
if ($query->num_rows() > 0) //if user exists
{
$row = $query->row();
return $row->username;
}
else
{
return FALSE;
}
}
}
This works in my view if for instance I try:
echo get_username($this->uri->segment(3)); //uri segment 3 is a user id.
However want to send the username to my view via controller. I tried the following in my controller:
function write_message($user_id = '') //function parameter is 3rd uri segment
{
$data['username'] = get_username($user_id);
$this->load->view('my_view', $data);
}
Then in my view I have
echo $username which echoes array instead of the username. What am I doing wrong here?
Your criteria should be clear, and the usrname should be unique i think, so...
if ($query->num_rows() == 1) //if user exists, and unique
{
$res = $query->result_array();
return $res[0]['username'];
}
else
{
return FALSE;
}
Upon using <pre>print_r($username)</pre> in my view (as suggested by Alfonso) it was easy to spot the issue, being an identical variable name in my view which was another array. Correct answer goes to anyone that gives a good suggestion/input for my helper or Alfonso if he submits his post as answer.
I have reworded my question to help gain my specific answer, sorry if my last question was confusing:
I have an issue with trying to delete a row from the database but also incorporate how to unlink the to images from different locations. I cannot get the row to delete or the images to unlink.
Here is my -> db structure
My image locations are:
fullpath = /includes/uploads/gallery/
thumbpath = /includes/uploads/gallery/thumbs/
Ideal Situation:
What my ideal situation would be is when the delete link is clicked that it will run the function within the index function and then reload the same page with a success message -> I have not successfully been able to get them to work.
My code:
Model:
function deleteImage($id,$path){
$this->db->delete('images', array('id' => $id));
if($this->db->affected_rows() >= 1) {
if(unlink($path)
return TRUE;
} else{
return FALSE;
}
}
View:
<?php if(is_array($get_images)): ?>
<?php foreach($get_images as $image): ?>
<img src ="<?=base_url()?>includes/uploads/gallery/thumbs/<?=$image->thumbpath?>" alt="<?= $image->description?>"> Delete
<?php endforeach; ?>
<?php endif; ?>
Controller:
function index() {
if(!$this->session->userdata('logged_in')) {
redirect('admin/home');
}
// Main Page Data
$page['get_images'] = $this->image_model->getImages();
$data['cms_pages'] = $this->navigation_model->getCMSPages();
$data['title'] = 'Delete Gallery Image';
$data['content'] = $this->load->view('admin/deleteimage',$page,TRUE);
$this->load->view('admintemplate', $data);
}
function delete_data($record_id)
{
$query = $this->db->get_where('projukti_committee',array('record_id' => $record_id));
if( $query->num_rows() > 0 )
{
$row = $query->row();
$picture = $row->picture;
unlink(realpath('assets/photo/'.$picture));
$this->db->delete('projukti_committee', array('record_id' => $record_id));
return true;
}
return false;
}
What you're looking for if you're using active record is:
$this->db->delete();
Now from memory this will only return false if an error has occurred, so will constantly return true. But it's ok we can use another function to check whether the row was deleted.
Oh and you will also want to pass into the function the path of the file you want deleting.
function deleteImage($id,$path) {
$this->db->delete('table', array('id' => $id));
if($this->db->affected_rows() >= 1){
if(unlink($path))
return TRUE;
} else {
return FALSE;
}
}
Presumably you have an image ID of some sort.
Append the ID to the deleteimage/delete URI.
Create a method that deletes the ID from your table, and use PHP's unlink($path) function to actually delete the image.