如何将ajax与codeigniter';s模块

如何将ajax与codeigniter';s模块,ajax,json,codeigniter,jquery,Ajax,Json,Codeigniter,Jquery,我有一个用codeigniter框架编写的页面。 现在我想在页面上添加一个按钮(例如“show more”),该按钮将使用“ajax.php”从数据库中获取数据并在站点上显示它们,但我不希望它单独连接到数据库然后获取结果,只希望能够收集数据(在ajax.php中)以及codeigniter控制器(使用模型) 希望您理解我:)在这里,您只需添加查看更多按钮并调用此js和ajax函数。这是我使用的代码,请查看并根据您的要求使用 $('.more').live("click",function()

我有一个用codeigniter框架编写的页面。 现在我想在页面上添加一个按钮(例如“show more”),该按钮将使用“ajax.php”从数据库中获取数据并在站点上显示它们,但我不希望它单独连接到数据库然后获取结果,只希望能够收集数据(在ajax.php中)以及codeigniter控制器(使用模型)


希望您理解我:)

在这里,您只需添加查看更多按钮并调用此js和ajax函数。这是我使用的代码,请查看并根据您的要求使用

$('.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;
    });
<?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
    }

}

ajax文件中的代码

$('.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;
    });
<?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
    }

}



谢谢你这么快的回复!周一查看并让您知道:)另外,我应该将ajax.php文件放在哪里?在..\application\Controller中?