I am using CodeIgniter 2.2.
I have to update database with +1 every time a page is visited. Code works but it get incremented by +2 every time.
Example: if total views=2
, once the page is clicked total views
should be 3
, but the value in the database is 4
.
I am sure that I am calling the model add_one_to_view_image
only once in my controller.
controller
function view(){
$view_id = $this->uri->segment(3);
if($view_id){
$this->property->add_one_to_view_image($view_id);
$prop['prop_detail'] = $this->property->get_single_property_for_view($view_id);
$prop['prop_imgs'] = $this->property->get_single_property_images($view_id);
$prop['amenities'] = $this->property->get_single_property_amenities($view_id);
$prop['latest'] = $this->home->get_latest_properties();
$size = sizeof($prop['latest']);
for($k=0; $k< $size; $k++){
$Image_name = $this->property->get_property_first_image($prop['latest'][$k]->property_id);
if($Image_name){
$prop['latest'][$k]->image=$Image_name[0]->name;
}else {
$prop['latest'][$k]->image="";
}
}
$this->load->view('property_view', $prop);
}
}
Model
function add_one_to_view_image($id){
echo "We in add one to views";
$this->db->where('property_id', $id);
$this->db->set('views', 'views+1', FALSE);
$this->db->update('property_views');
}
I added a echo statement inside the model. and it prints only once..
View
<!DOCTYPE html>
<html lang="en-US">
<head>
<?php $this->load->view('header_links'); ?>
<title><?php echo $prop_detail[0]->title; ?> | lanka Property</title>
<meta name="author" content="Sathyabaman - lankaproperty.com">
<meta name="description" content="<?php echo $prop_detail[0]->full_description; ?>">
<meta name="keywords" content="home, land, apartment, house, room, Commercial Building, buy, sell, rent, lease, sri lanka">
</head>
<body>
<div id="wrapper-outer" >
<div id="wrapper">
<div id="wrapper-inner">
<?php $this->load->view('header_top_user_bar'); ?>
<?php $this->load->view('header_logo'); ?>
<?php $this->load->view('navigation'); ?>
<!-- CONTENT -->
<div id="content"><div class="container">
<div id="main">
<div class="row">
<div class="span9">
<h1 class="page-header"><?php echo $prop_detail[0]->title; ?></h1>
<div class="carousel property">
<div class="preview">
<li class="active">
<img src="<?php echo base_url(); ?>/upload_images/<?php echo $prop_imgs[0]->name; ?>" alt="">
</li>
</div><!-- /.preview -->
<div class="content">
<a class="carousel-prev" href="#">Previous</a>
<a class="carousel-next" href="#">Next</a>
<ul>
<?php foreach ($prop_imgs as $img) : ?>
<li class="active">
<img src="<?php echo base_url(); ?>/upload_images/<?php echo $img->name; ?>" alt="">
</li>
<?php endforeach; ?>
</ul>
</div>
<!-- /.content -->
</div>
<!-- /.carousel -->
<div class="property-detail">
<div class="pull-left overview">
<div class="row">
<div class="span3">
<h2>Overview</h2>
<?php foreach ($prop_detail as $dtl) : ?>
<table>
<tr>
<?php if($dtl->status == 0){?>
<td style="font-size: 120%;"><strong>Under Approval</strong></td>
<?php } if($dtl->status == 1){?>
<td style="color:green; font-size: 200%;"><strong>Available!</strong></td>
<?php } if($dtl->status == 2){?>
<td style="color:red; font-size: 200%;"><strong>Expired!</strong></td>
<?php } if($dtl->status == 3){?>
<td style="color:red; font-size: 200%;"><strong>Deleted!</strong></td>
<?php } if($dtl->status == 4){?>
<td style="color:red; font-size: 200%;"><strong>Suspended!</strong></td>
<?php } ?>
</tr>
<tr>
<th></th>
<td><br/></td>
</tr>
<tr>
<th>Property ID :</th>
<td> PPTID<?php echo $dtl->property_id; ?></td>
</tr>
<tr>
<th>Price :</th>
<td><?php if($dtl->price == 0){ echo "negotiable"; }
else {
$price_text = (string)$dtl->price; // convert into a string
$arr = str_split($price_text, "3"); // break string in 3 character sets
$price_new_text = implode(",", $arr); // implode array with comma
echo 'Rs. '.$price_new_text.'/=';
} ?>
</td>
</tr>
<tr>
<th>Property type :</th>
<td><?php echo $dtl->type_name; ?></td>
</tr>
<tr>
<th>Property size :</th>
<td><?php echo $dtl->size; ?> <?php echo $dtl->size_type; ?></td>
</tr>
<tr>
<th>Contract type :</th>
<td><?php echo $dtl->contract_type; ?></td>
</tr>
<?php if($dtl->bed){ ?>
<tr>
<th>Bedrooms :</th>
<td><?php echo $dtl->bed; ?></td>
</tr>
<?php } if($dtl->bath){ ?>
<tr>
<th>Bathrooms :</th>
<td><?php echo $dtl->bath; ?></td>
</tr>
<?php } if($dtl->address){?>
<tr>
<th>address :</th>
<td><?php echo $dtl->address; ?></td>
</tr>
<?php } ?>
<tr>
<th>Area :</th>
<td><?php echo $dtl->area; ?></td>
</tr>
<tr>
<th>City :</th>
<td><?php echo $dtl->city; ?></td>
</tr>
<?php if($dtl->Contact_name){ ?>
<tr>
<th>Contact Name :</th>
<td><?php echo $dtl->Contact_name; ?></td>
</tr>
<?php } ?>
<tr>
<th>Contact No 1 :</th>
<td><strong><?php echo chunk_split($dtl->phone_home, 3, ' '); ?></strong></td>
</tr>
<?php if($dtl->phone_hand){ ?>
<tr>
<th>Contact No 2 :</th>
<td><strong><?php echo chunk_split($dtl->phone_hand, 3, ' '); ?></strong></td>
</tr>
<?php } ?>
<tr><td colspan="2">
<br/>
<a class="btn btn-primary btn-large list-your-property">
Contact : <?php echo chunk_split($dtl->phone_home, 3, ' '); ?></a>
</td></tr>
</table>
</div>
<!-- /.span2 -->
</div>
<!-- /.row -->
</div>
<p><?php echo nl2br($dtl->full_description); ?>.</p>
<?php endforeach; ?>
<br/><br/>
<h2>General amenities</h2>
<div class="row">
<ul class="span2">
<li class="<?php if($amenities[0]->Gym==1){echo 'checked'; }else{echo 'plain';} ?>">
Gym
</li>
<li class="<?php if($amenities[0]->Air_conditioning==1){echo 'checked'; }else{echo 'plain';} ?>">
Air Conditioning
</li>
<li class="<?php if($amenities[0]->internet==1){echo 'checked'; }else{echo 'plain';} ?>">
Internet
</li>
<li class="<?php if($amenities[0]->Wifi==1){echo 'checked'; }else{echo 'plain';} ?>">
Wifi
</li>
<li class="<?php if($amenities[0]->Lift==1){echo 'checked'; }else{echo 'plain';} ?>">
Lift
</li>
<li class="<?php if($amenities[0]->Furnished==1){echo 'checked'; }else{echo 'plain';} ?>">
Furnished
</li>
</ul>
<ul class="span2">
<li class="<?php if($amenities[0]->Television==1){echo 'checked'; }else{echo 'plain';} ?>">
Television
</li>
<li class="<?php if($amenities[0]->Hot_Water==1){echo 'checked'; }else{echo 'plain';} ?>">
Hot Water
</li>
<li class="<?php if($amenities[0]->Smoking_allowed==1){echo 'checked'; }else{echo 'plain';} ?>">
Smoking Allowed
</li>
<li class="<?php if($amenities[0]->Pets_allowed==1){echo 'checked'; }else{echo 'plain';} ?>">
Pets Allowed
</li>
<li class="<?php if($amenities[0]->Garage==1){echo 'checked'; }else{echo 'plain';} ?>">
Garage
</li>
<li class="<?php if($amenities[0]->Secure_parking==1){echo 'checked'; }else{echo 'plain';} ?>">
Secure parking
</li>
</ul>
<ul class="span2">
<li class="<?php if($amenities[0]->Swimming_Pool==1){echo 'checked'; }else{echo 'plain';} ?>">
Swimming Pool
</li>
<li class="<?php if($amenities[0]->Tennis_court==1){echo 'checked'; }else{echo 'plain';} ?>">
Tennis court
</li>
<li class="<?php if($amenities[0]->Balcony==1){echo 'checked'; }else{echo 'plain';} ?>">
Balcony
</li>
<li class="<?php if($amenities[0]->Fenced==1){echo 'checked'; }else{echo 'plain';} ?>">
Fenced
</li>
<li class="<?php if($amenities[0]->Back_up_Generator==1){echo 'checked'; }else{echo 'plain';} ?>">
Back Up Generator
</li>
<li class="<?php if($amenities[0]->Garden==1){echo 'checked'; }else{echo 'plain';} ?>">
Garden
</li>
</ul>
<ul class="span2">
<li class="<?php if($amenities[0]->Waterfront==1){echo 'checked'; }else{echo 'plain';} ?>">
Waterfront
</li>
<li class="<?php if($amenities[0]->Ocean_View==1){echo 'checked'; }else{echo 'plain';} ?>">
Ocean View
</li>
<li class="<?php if($amenities[0]->Security==1){echo 'checked'; }else{echo 'plain';} ?>">
Security
</li>
<li class="<?php if($amenities[0]->Water_Tank==1){echo 'checked'; }else{echo 'plain';} ?>">
Water Tank
</li>
</ul>
</div>
<!--
<h2>Map</h2>
<div id="property-map"></div> -->
</div>
</div>
<div class="sidebar span3">
<div class="widget contact">
<div class="title">
<h2 class="block-title">Contact Owner</h2>
</div><!-- /.title -->
<div class="content">
<form method="post" id="frm_contact_owner">
<div class="alert alert-success" id="co_sucess_message" style="display: none">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>Message successfully send to owner!. <br/>Thank you!</strong>
</div>
<input type="hidden" name="co_property_id" value="<?php echo $dtl->property_id; ?>">
<div class="control-group">
<label class="control-label" >
Name
<span class="form-required" title="This field is required.">*</span>
<span class="form-required" id="co_name_error" title="This field is required."></span>
</label>
<div class="controls">
<input type="text" name="co_name" id="co_name">
</div><!-- /.controls -->
</div><!-- /.control-group -->
<div class="control-group">
<label class="control-label" >
Email
<span class="form-required" title="This field is required.">*</span>
<span class="form-required" id="co_email_error" title="This field is required."></span>
</label>
<div class="controls">
<input type="text" name="co_email" id="co_email">
</div><!-- /.controls -->
</div><!-- /.control-group -->
<div class="control-group">
<label class="control-label" >
Message
<span class="form-required" title="This field is required.">*</span>
<span class="form-required" id="co_message_error" title="This field is required."></span>
</label>
<div class="controls">
<textarea name="co_message" id="co_message"></textarea>
</div><!-- /.controls -->
</div><!-- /.control-group -->
<div class="form-actions">
<input type="submit" id="co_send" class="btn btn-primary arrow-right" value="Send">
<img id="co_ajax_image" src="<?php echo base_url(); ?>html/assets/img/ajax_loader.gif" alt="" style="float: left; display:none">
</div><!-- /.form-actions -->
</form>
</div><!-- /.content -->
</div><!-- /.widget -->
<div class="widget properties last">
<div class="title">
<h2>Latest Properties</h2>
</div><!-- /.title -->
<div class="content">
<?php foreach ($latest as $latest_pro) :?>
<div class="property">
<div class="image">
<?php
if($latest_pro->image){
$image_name = base_url().'upload_images/'.$latest_pro->image;
}else{
$image_name = base_url().'html/assets/img/tmp/property-small-5.png';
}
?>
<a href="<?php echo base_url(); ?>property/view/<?php echo $latest_pro->property_id; ?>/<?php echo str_replace(' ', '_', $latest_pro->title); ?>"></a>
<img src="<?php echo $image_name; ?>" alt="">
</div><!-- /.image -->
<div class="wrapper">
<div class="title">
<h3>
<a href="<?php echo base_url(); ?>property/view/<?php echo $latest_pro->property_id; ?>/<?php echo str_replace(' ', '_', $latest_pro->title); ?>"><?php echo substr($latest_pro->title, 0, 16); ?>..</a>
</h3>
</div><!-- /.title -->
<div class="location"><?php echo $latest_pro->area; ?>, <?php echo $latest_pro->city; ?></div><!-- /.location -->
<div class="price">
<?php if($latest_pro->price == 0){ echo "negotiable"; }
else {
$price_text = (string)$latest_pro->price; // convert into a string
$arr = str_split($price_text, "3"); // break string in 3 character sets
$price_new_text = implode(",", $arr); // implode array with comma
echo 'Rs. '.$price_new_text.'/=';
} ?>
</div><!-- /.price -->
</div><!-- /.wrapper -->
</div><!-- /.property -->
<?php endforeach; ?>
</div><!-- /.content -->
</div><!-- /.properties -->
</div>
</div>
</div>
</div>
</div><!-- /#content -->
</div><!-- /#wrapper-inner -->
<?php $this->load->view('footer'); ?>
</div><!-- /#wrapper -->
</div><!-- /#wrapper-outer -->
<?php $this->load->view('footer_setting_palette'); ?>
<?php //$this->load->view('property_filter_ajax'); ?>
<?php $this->load->view('say_hello_ajax'); ?>
<?php $this->load->view('property_contact_owner'); ?>
</body>
</html>
I know it's completly illlogical but check this out....
I had also got the same issue long back but after 1 week of headache I come to know about this solution.
I got the solution from this link, check out the 2nd answer. Initially I also didn't take that solution seriously but in the end it worked for me :)
Solution :
Make sure you don't have tag with empty src="" attribute or any css style refering to empty url (like background: url();) on your site around the place when you have your code that runs twice.
try this solution, i have also done this same in my project and didn't face any problem. if i see logically there is no problem with your code but just try row() instead of result() function and than see if it have the same impact. and make sure default value of the column view is 0
function add_one_to_view_image($id){
$this->db->select('views');
$this->db->from('property_views');
$this->db->where('property_id', $id);
$pag = $this->db->get();
$resultset = $pag->row();
$newvalue = ++$resultset->views;
$data = array('views' => $newvalue);
$this->db->where('property_id', $id);
$this->db->update('property_views', $data);
}
when you are going to get 1 row is bettery to get it by row rather than getting it as a result.
Regards
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With