incrementing with two instead of one - php

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);
}

Related

my values on one column wont update immediately after inserting data, but after refresh the values shows up

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;
}

Php, Mvc how to do a overlay?

I'm working in PHP, model view controller.
I run a action that is called getallmypictures where the result is shown on a view. When I click the result picture I run another action that get's all my related products to this picture, and that result it shown on another view file.
How can I get the second action result to be shown in a overlay on the same page as my pictures views?
This is my first action getallmypictures() when its execute:
public function GetAllPicturesAction(){
//if (isset($_GET['showAll'])) {
//if (isset($_SESSION['status']) == "customers_inloggad") {
$db = new PDO("mysql:host=localhost;dbname=fashionable", "root", "");
$stm1 = $db->prepare("MYSQL");
if ($stm1->execute()) {
$res = $stm1->fetchAll();
require_once './views/pictureInspo.php';
}
else {
header("location:../start/index?msgWrong=empty");
}
}
and the other action getthispicture() is required to this PHP file to show the result:
public function GetThisPictureAction(){
if(isset($_GET['getPictureValue'])){
// if (isset($_SESSION['status']) == "customers_inloggad") {
$db = new PDO("mysql:host=localhost;dbname=fashionable", "root","");
$stm2 = $db->prepare("MYSQL");
$stm=$db->prepare("MYSQL");
$stm->bindParam(":picture_inspo_id", $_GET['getPictureValue']);
$stm2->bindParam(":picture_inspo_id",$_GET['getPictureValue']);
if ($stm2->execute()) {
$result = $stm2->fetchAll();
$stm->execute();
$result1 = $stm->fetchAll();
require_once './views/pictureInspo_overlay.php';
} }//}
else {
header("location:../start/index?msgWrong=empty");
}
}
instead of showing to actions on 2 views I would like to have a second view like an overlay but still keep them in different actions.
The routing im using is /controller/action

Redirect where no profile data in database

Controller
function view_profil(){
$id = $this->session->userdata('idtabelX');
$profil[''] = $this->class_model->function_model($id);
$this->load->view('view_profil', $profil);
}
Model
function function_model($id){
$this->db->select('*');
$this->db->from('tabelA a');
$this->db->join('tabelB b','b.idtabelB=a.ididtabelB');
$this->db->join('tabelC c','c.idtabelC=a.idtabelC');
$this->db->where('idtabelX', $id);
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->row_array();
}
}
If the profile is clicked, it will show the data user profile stored in database, but if the user does not have the data profile in database, I want to redirect the user to a insert page to populate the profile data.
Thank you very much for help me to resolve this problem.
In your controller -
function view_profil(){
$id = $this->session->userdata('idtabelX');
$profil[''] = $this->class_model->function_model($id);
//add this bit of code
if($profil[''] == NULL)
{
redirect('your_controller/your_add_method');
}
else
{
$this->load->view('view_profil', $profil);
}
}
You need to use the redirect() function and pass the url of your controller method for insertion as parameters.

Sending $data Variable From Model To View

Okay, so I have this snippet of code in a controller. However, it's all DB driven and should really be in model - I get that. However, as you can see in the IF statement, I need to pass along $data to my view. Based on the outcome. I tried pasting this chuck of coding in a method in my model (calling the model method via controller), however the $data[update_prompt] string is not getting called by the view...
How would I translate this code into a model - sending the $data values back to my controller to embed in my view?
// show appropriate upgrade message if user has free account
$id = $this->session->userdata('user_id');
$this->db->select('subscription'); // select the subscription column
$this->db->where('id', $id); //find id in table that matches session id
$query = $this->db->get("subscriptions"); // connect to this database
$subscribe = $query->result_array(); //returns the result of the above
if($subscribe[0]['subscription'] == 'freebie') // if subscription column equals 'freebie' in the $subscribe array, do this:
{
$data['update_prompt'] = $this -> load -> view('shared/upgrade_subscription', '', TRUE); // adds view within view, $update_prompt
}
else
{
$data['update_prompt'] = '';
}
You would add a function in your model, like so:
public function myModelFunction($id) {
//we return row as we are looking up by primary key and are guaranteed only one row
return $this->db->select('subscription')
->where('id', $id)
->get('subscriptions')
->row();
}
Then, in your controller:
public function myControllerFunction() {
$subscribe = $this->my_model->myModelFunction($this->session->userdata('id'));
if($subscribe->subscription == 'freebie') // if subscription column equals 'freebie' in the $subscribe array, do this:
{
$data['update_prompt'] = $this -> load -> view('shared/upgrade_subscription', '', TRUE); // adds view within view, $update_prompt
}
else
{
$data['update_prompt'] = '';
}
}

Delete Images -> codeigniter

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.

Categories