我使用CodeIgniter 2.2.
每次访问页面时,我必须更新数据库1.代码工作,但它每次增加2.
示例:如果总视图= 2,一旦页面被单击,总视图应为3,但数据库中的值为4.
我确信我在控制器中只调用一次add_one_to_view_image模型.
调节器
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); } }
模型
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'); }
我在模型中添加了一个echo语句.它只打印一次..
视图
<!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,' '); ?></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,' '); ?></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,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>