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.
Related
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 am simply updating my records in a database. I am passing Id and status to the controller and want to update to database but I am not able to do it.
Controller name is Examtest, model name is crud_model, view name is categories.
This code is of View:
foreach ($user_data as $key => $val) {
$status = $val['status'];
if ($status == "Active") {
$st = '<p class="text-success">ACTIVE</p>';
$stl = 'Make Inactive';
}else{
$st = '<p class="text-danger">INACTIVE</p>';
$stl = 'Make Active';
}
This code is of Controller, I have created one function :
public function update_status()
{
$this->load->model('crud_model');
$data['user_data']=$this->crud_model->update_status();
if (isset($_REQUEST['sta'])) {
if ($data=="Active") {
$this->session->set_flashdata('msg',"Successfully");
$this->session->set_flashdata('msg_clas','alert-success');
}
else{
$this->session->set_flashdata('msg',"Not Successfully");
$this->session->set_flashdata('msg_clas','alert-danger');
}
}
return $this->load->view('backend/teacher/categories');
}
This code is of model, I had created one function :
function update_status(){
$this->load->database();
$id=$_REQUEST['id'];
$saval=$_REQUEST['sta'];
if ($saval="Active") {
$status= "Inactive";
}
else
{
$status = "Active";
}
$data=array('status'=>$status);
$this->db->where('category_id',$id);
return $this->db->update('tbl_categories',$data);
}
Where am I going wrong? I had just created some record in that I had given Action button, if someone click on select option it will show them a select option if someone choose first option it will update my records in database too.
Please use last_query() method, to print current update query.
To print last query please use -
echo $this->db->last_query();die;
Update query --
$data=array('status'=>$status);
$this->db->where('category_id',$id);
$this->db->update('tbl_categories',$data);
echo $this->db->last_query();die;
return $this->db->affected_rows();
Please try this.
I am building a website with CodeIgniter, and I ran into a problem:
When I load the view of the song page I also want to update in database the view count by one, but it is updating by two. I don't understand why, this happens, because when I try the same method when the download button is pressed to update the download count, that one works fine.
Here is the code:
function view($slug) {
// Get the song from database by slug
$data['song'] = $this->mdl_song->get_where_row("slug", $slug);
// if thre is a song, continue
if ($data['song']) {
// Set data to send to the view file
$data['module'] = "song";
$data['view_file'] = "single";
$data['page_title'] = "Download {$data['song']->name}";
// echo the template that display the view
echo Modules::run('template/two_col', $data);
// Update the song hits
$this->mdl_song->_update($data['song']->id, array('hits' => $data['song']->hits + 1));
} else {
show_404();
}
}
function _update($id, $data) {
$table = $this->get_table();
$this->db->set($data);
$this->db->where('id', $id);
$this->db->update($table);
}
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
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.