2013-05-30 123 views
0

我有一個用codeigniter框架編寫的頁面。 現在我想添加一個按鈕到頁面(例如'show more'),它將從數據庫中獲取'ajax.php'的數據並將它們顯示在網站上,但我不希望它單獨連接到數據庫,然後得到的結果,只是希望能夠收集數據(ajax.php),以及在笨控制器(使用機型)...如何在codeigniter模塊中使用ajax

希望你能理解我:)

回答

1

在這裏你去只需添加查看更多按鈕,並調用這個js和Ajax function..This是代碼我已經使用,請看到它並使用它按您的需求量的

$('.more').live("click",function() 
    { 
     var this_tag = $(this); 
     var ID = $(this).attr("id"); 
     if(ID) 
     { 
      $("ol#updates").addClass('tiny-loader'); 
      this_tag.html('Loading.....'); 
      $.post(siteUrl+"ajax/ajax_more",{lastmsg:ID,restid:$(this_tag).data("restid")},function(html){ 
      $("ol#updates").removeClass('tiny-loader'); 
      $("ol#updates").append(html); 
      $("#more"+ID).remove();// removing old view-more button 

      }); 
     } 
     else 
     { 
      this_tag.fadeOut('slow');// no results 
     } 
     return false; 
    }); 

代碼在AJAX文件

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 

class Ajax_more extends CI_Controller { 

    public function __construct() 
    { 
     parent::__construct(); 
     $this->load->model('general_model'); 
     $this->limit =REVIEW_DETAIL; 
    } 

    public function index($offset = 0) 
    { 
     $lastmsg=$this->input->post('lastmsg'); 
     $rest_id=$this->input->post('restid'); 
     $value=('reviews.*,usermaster.Name as User'); 
     $joins = array 
     (
       array 
       (
        'table' => 'tk_usermaster', 
        'condition' => 'usermaster.Id = reviews.UserId', 
        'jointype' => 'leftouter' 
       ), 
     ); 
     $this->results = $this->general_model->get_joinlist('reviews',$value,$joins,array('reviews.Status'=>'Enable','reviews.RestaurantId'=>$rest_id,'reviews.Id <'=>$lastmsg),'reviews.Id','desc',$this->limit,$offset); 
     $data['list_review']= $this->results['results']; 
     ?> 
     <?php foreach ($data['list_review'] as $row): ?> 
     <div class="user_reviews_data" > 
         <div class="width-20 float-left"> 
          <span class="padleft-10"><?php echo $row->User; ?> </span> 
          <br/> 
          <span class="padleft-10 "><div class="rateit" data-rateit-value="<?php echo $row->Rating;?>" data-rateit-ispreset="true" data-rateit-readonly="true"></div></span> 
          <div class="muted padleft-10 float-left"><small><?php echo date('dS M Y' ,strtotime($row->CreatedDate)); ?></small></div> 
         </div> 
         <div class="width-80 float-left"><?php echo $row->Feedback;?></div> 
         <span class="report_span"><a href="<?php echo site_url('report_form/index/review/'.$rest_id.'/'.$row->Id);?>" class="pop-up" data-element_id="<?php echo $row->Id; ?>">Report this Feedback <img src="<?php echo base_url();?>themes/images/FLAG_GREY.png"></a></span>  
        </div> 
     <?php 
     $msg_id = $row->Id; 
     endforeach; ?> 
     </div> 
       <div id="more<?php echo @$msg_id; ?>" class="btn-container center_text morebox"> 
       <a href="javascript:;" class="more btn orange_signup" id="<?php echo @$msg_id; ?>" data-restid="<?php echo $rest_id; ?>" title="view more">View More</a> 
       </div> 

     <?php 
    } 

} 

+0

感謝這樣一個快速反應!星期一檢查,讓你知道:) p.s.在哪裏我應該把ajax.php文件?在.. \應用程序\控制器? – breq

+0

好吧...你可以把它放在應用程序/控制器/ ajax –

+0

oK,我剛剛測試過它。工作就像一個魅力:)謝謝你! – breq