drfkl66684 2011-03-24 00:08
浏览 59
已采纳

AJAX加载更多的帖子脚本输出什么都没有,没有警告,没有通知,没有数据

Can anyone tell me why this is outputting nothing.. at all? No errors, no notices. Nada. I really need help with this, theres nothing wrong with the SQL details. The codes purpose is too display Mysql data with an AJAX div that loads more posts. If theres anything that could be causing this please post below. Thanks!:) Source code:

 <?php
    /* grab stuff */
    function get_posts($start = 0, $number_of_posts = 5) {
        /* connect to the db */
        $connection = mysql_connect('localhost','root','');
        mysql_select_db('draft2',$connection) or die(mysql_error()); 
        $posts = array();
        /* get the posts */
        $query = "SELECT item_id, username, item_content FROM updates ORDER BY update_time DESC LIMIT $start,$number_of_posts" or die(mysql_error()); 

        $result = mysql_query($query);
        while($row = mysql_fetch_assoc($result)) {
            preg_match("/<p>(.*)<\/p>/",$row['item_content'],$matches);
            $row['item_content'] = strip_tags($matches[1]);
            $posts[] = $row;
        }
        /* return the posts in the JSON format */
        return json_encode($posts);
    }

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"  dir="ltr">
<head>



<style type="text/css">
   .style1 {
                                font-family: "Trebuchet MS", "Lucida Sans Unicode", "Lucida Grande", "Lucida Sans", Arial, sans-serif;
                                background-image: url('images/banner.gif');
                        }
                        .style2 {
                                font-size: x-small;
                                color: #62D7D7;
                        }
                        .style3 {
                                text-align: center;
                        }
                        .style5 {
                                font-size: 12px;
                                font-family: "lucida grande", "tahoma", "verdana", "arial", "sans-serif"
                        }
                .style6 {
    font-family: "lucida grande", "tahoma", "verdana", "arial", "sans-serif";
}
.style7 {
    text-align: left;
    font-size: x-small;
    font-family: "Trebuchet MS", "Lucida Sans Unicode", "Lucida Grande", "Lucida Sans", Arial, sans-serif;
    color: #0070B8;
}

        #load-more                  { background-color:#eee; color:#999; font-weight:bold; text-align:center; padding:10px 0; cursor:pointer; }
        #load-more:hover            { color:#666; }
        .activate                   { background:url(loadmorespinner.gif) 140px 9px no-repeat #eee; }

                </style>




    <script type="text/javascript" src="jquery-1.3.2.min.js"></script>
    <script type="text/javascript" src="jquery.scrollTo-1.4.2.js"></script>
    <script type="text/javascript">
        //when the DOM is ready
        $(document).ready(function(){
            //settings on top
            var initialPosts = <?php echo get_posts(0,$_SESSION['posts_start']); ?>;
            //function that creates posts
            var postHandler = function(postsJSON) {
                $.each(postsJSON,function(i,post) {
                    //post url
                    var postURL = '' + domain + post.post_name;
                    var id = 'post-' + post.ID;
                    //create the HTML
                    $('<div></div>')
                    .addClass('post')
                    .attr('id',id)
                    //generate the HTML
                    .html('<a href="' + postURL + '" class="username">' + post.username + '</a><p class="item_content">' + post.item_content + '<br /><a href="' + postURL + '" class="post-more">Read more...</a></p>')
                    .click(function() {
                        window.location = postURL;
                    })
                    //inject into the container
                    .appendTo($('#posts'))
                    .hide()
                    .slideDown(250,function() {
                        if(i == 0) {
                            $.scrollTo($('div#' + id));
                        }
                    });
                }); 
            };
            //place the initial posts in the page
            postHandler(initialPosts);
            //first, take care of the "load more"
            //when someone clicks on the "load more" DIV
            var start = <?php echo $_SESSION['posts_start']; ?>;
            var desiredPosts = <?php echo $number_of_posts; ?>;
            var loadMore = $('#load-more');
            //load event / ajax
            loadMore.click(function(){
                //add the activate class and change the message
                loadMore.addClass('activate').text('Loading...');
                //begin the ajax attempt
                $.ajax({
                    url: 'jquery-version.php',
                    data: {
                        'start': start,
                        'desiredPosts': desiredPosts
                    },
                    type: 'get',
                    dataType: 'json',
                    cache: false,
                    success: function(responseJSON) {
                        //reset the message
                        loadMore.text('Load More');
                        //increment the current status
                        start += desiredPosts;
                        //add in the new posts
                        postHandler(responseJSON);
                    },
                    //failure class
                    error: function() {
                        //reset the message
                        loadMore.text('Oops! Try Again.');
                    },
                    //complete event
                    complete: function() {
                        //remove the spinner
                        loadMore.removeClass('activate');
                    }
                });
            });
        });
    </script>
</head>
<body>

    <!-- Widget XHTML Starts Here -->
    <div id="posts-container">
        <!-- Posts go inside this DIV -->
        <div id="posts"></div>
        <!-- Load More "Link" -->
        <div id="load-more">Load More</div>
    </div>
    <!-- Widget XHTML Ends Here -->

</body>
</html>

I actually have posted this problem before, i know that will get you all angry. But i tried fixing it for a while but im just not as experienced as the people on here so I return. Please dont hate me.

  • 写回答

2条回答 默认 最新

  • dongnue2071 2011-03-24 06:55
    关注

    @user663049: I think your problem is this line --

    $query = "SELECT item_id, username, item_content FROM updates ORDER BY update_time DESC LIMIT $start,$number_of_posts" or die(mysql_error()); 
    

    It should be --

    $query = "SELECT item_id, username, item_content FROM updates ORDER BY update_time DESC LIMIT " . $start . ", " . $number_of_posts;
    
    $result = mysql_query($query) or die(mysql_error());
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 MATLAB怎么通过柱坐标变换画开口是圆形的旋转抛物面?
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿