0

大家好,我有一个简单的问题,我想:

我在代码点火器中创建了一个博客,这就是我的代码的外观:

这是我的主页控制器:

public function index()
    {
        $data['posts'] = $this->Model_cats->getLivePosts(7);
        $data['cats'] = $this->Model_cats->getTopCategories(); 
        $data['title'] = 'Welcome';
        $data['main'] = 'public_home';
        $data['main2'] = 'public_home_loadpost';
        $this->load->vars($data);
        $this->load->view('template', $data);
    }

我使用这个 jquery 来加载:

$(window).scroll(function())
{
    if( $(window).scrollTop() == $(document).height() - $(window).height() ){
        $('div#loadMoreComments').show();

        $.ajax({
            url: "WHAT PAGE PUTHERE.php?lastComment=" + $(".postedComment:last").attr("id"),
            success: function(html){
                if(html)
                {
                    $("#postedComments").append(html);
                    $('div#loadMoreComments').hide();

                }
                else
                {
                    $('div#loadMoreComments').replaceWith("Finished Loading the comments");
                }
            }
        });
    }

我真的不知道我可以在 jquery 文件中的 url 中放入哪个页面......该页面将为我获得更多帖子。请帮忙

我的观点看起来像这样

<?php

    if ( count($posts) )
    {
        foreach ($posts as $key => $list)
        {
            echo "<div class='postedComment'>";
            echo '<h2>'.$list['title'].'</h2>';
            echo auto_typography( word_limiter($list['body'], 200) );
            echo anchor('welcome/post/'.$list['id'],'read more >>');
            echo "</div>";
        }

        echo '<br/><br/>';
    }

?>

<div id='loadMoreComments' style="display:none;">hello</div>
4

1 回答 1

0

您需要在控制器中创建另一个视图。该视图的 url 将由 ajax 函数调用,并以 JSON 格式或您想要的任何格式返回数据。

这是一个使用ajax的简单方法。

一个非常简单的 ajax 视图示例(在控制器中):

public function ajax_get_current_date_time() {
    die(date('d-m-Y H:i:s'));
}

HTML:

<div>The current date and time: <b id="current_time"></b></div>

然后在前面的js中:

$.ajax({
    url: "/controller/ajax_get_current_date_time",
    success: function(data){
        $('#current_time').html(data);
    }
});

对于您的情况,您可以更好地使用 JSON。这是使用 JSON 进行 ajax 调用的示例。

$.ajax({
    url: "/controller/ajax_get_post",
    type: 'GET',
    data: 'lastComment=' + $(".postedComment:last").attr("id"),
    dataType: 'JSON',
    success: function(data){
        // trigger function that will transform the JSON data into HTML
        add_post(data);
    }
});

见 JSON 部分,请求的 url 必须返回 JSON。有关 JSON 的更多信息:点击!

于 2013-02-15T10:24:40.413 回答