CodeIgniter更新查询被执行两次 - php

我正在使用CodeIgniter 2.2。

每次访问页面时,我都必须用+1更新数据库。代码可以工作,但是每次都会增加+2。

示例:如果是total views=2,则在单击页面后total views应该是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, 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>

参考方案

我知道这完全是不合逻辑的,但是请检查this。

很久以前,我也遇到过同样的问题,但是经过1星期的头痛,我才知道这种解决方案。

我从this链接获得了解决方案,请查看第二个答案。最初,我也没有认真对待该解决方案,但最终它对我有用:)

解决方案:

当您的代码可以运行两次时,请确保您的网站周围没有标记为src =“”属性为空的标记,也没有任何引用空URL的css样式(例如background:url();)。

故障排除“警告:session_start():无法发送会话高速缓存限制器-标头已发送” - php

我收到警告:session_start()[function.session-start]:无法发送会话缓存限制器-标头已发送(错误输出开始如果我将表单数据提交到其他文件进行处理,则可以正常工作。但是,如果我将表单数据提交到同一页面,则会出现此错误。请建议<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0…

通过php表单修改我的xml文件 - php

这是我的xml文件和下面的php代码。我输入了一个输入类型,它将按名字搜索学生。然后将显示有关特定学生的信息,并且将显示另一个按钮更新。问题是我想在那之后修改信息。如何通过标签名称获取元素,以便可以修改有关特定学生的信息?<students> <student> <firstname>John</firstname&…

Div单击与单选按钮相同吗? - php

有没有一种方法可以使div上的click事件与表单环境中的单选按钮相同?我只希望下面的div提交值,单选按钮很丑代码输出如下:<input id="radio-2011-06-08" value="2011-06-08" type="radio" name="radio_date&#…

PHP-复选框组 - php

我有一个需要发布的表单复选框组。<input type="checkbox" value="true" checked name="chk0[]"> <input type="checkbox" value="false" name=…

使用PHP包含时的淡入淡出过渡 - php

我正在尝试使用jQuery或CSS(或其他方式!)在DIV中包含的php页面上创建淡入淡出或滑动过渡。我四处搜寻,发现了很多淡入淡出过渡的示例,这些实例彼此淡入淡出div或隐藏内容淡入淡出,但是这种情况略有不同。我有一个DIV,其内容由导航栏控制。选中后,每个页面都可以使用PHP成功地包含在div中,但我想使内容淡入和淡出。关于如何在页面更改之间进行漂亮过渡…