繁体   English   中英

在php mysql和jquery中加载更多

[英]Load more in php mysql and jquery

我试图提供选项,以加载我的学校项目的PHP网页上的更多帖子。

这是一些PHP代码。

    <?php
        $prj= mysql_query("select * from campaign where uid=$uid order by Closing_Date DESC Limit 0,1");
            $record = array();
            while($row = mysql_fetch_assoc($prj)){
            $record[] = $row;
            }
     foreach($record as $rec){

                <div class="col-sm-1 col-md-4" id="<?php echo $rec['cid'];?>">
                    <div class="well">
                        <div class="media services-wrap55 wow fadeInDown">

                                <h4 class="media-heading"><?php echo $rec['Project_Name'];?></h4></a>
                                <p> by <i><?php echo $rec['user_name'];?></i></p>
                                            <p> <?php echo $rec['short_dis'];?></p>
                        </div>
                    </div>
                </div>

    ?>

<?php } ?>

<div class="col-sm-1 col-md-12">
    <div class="media services-wrap55 wow fadeInDown"> 
        <center><h3 class="media-heading"><div class="more" id="more">Load More Post</div></h3></center>
    </div>
</div>

这是javascript代码。

$(document).ready(function(){   
    $('#more').click(function(){

        var get_last_post_display=$('li:last').attr('id'); //get ip last <li>

        $('#more').html('<img src="ajax-loader.gif");
            $.ajax({                
                type: "POST",
                url: "more_prj.php",
                data: "last_id_post="+get_last_post_display, //send id ip last <li> to more_post.php
                cache: false,
                success: function(html){

                    $('ul').append(html);

                    $('#more').text('Load More Project'); //add text "Load More Post" to button again

                    if(!html){

                    $('#more').text('No more Project to load'); // when last record add text "No more posts to load" to button.
                    }                   
                }
                });
        });                 
});

在代码下面配置javascript时遇到问题。 var get_last_post_display = $('li:last')。attr('id'); 它配置为li ,我想为div配置它。 我尝试了多次,但没有运气,因为我不善于使用javascript。

你能告诉我如何配置它吗?

谢谢。

尝试使用类名

var get_last_post_display=$('.col-sm-1.col-md-4:last').attr('id');

更新您可以使用json,因为有时您可以获得404页面或其他内容,这将导致问题,您可以确定json,您获得了所需的内容。

more_prj.php

<?php
$prj = mysql_query("select * from campaign where uid=$uid order by Closing_Date DESC Limit 0,1");
$result = '';
while($row = mysql_fetch_assoc($prj)) {
    $result .= '<div class="col-sm-1 col-md-4 unique-class" id="'.$row['cid'].'">
        <div class="well">
            <div class="media services-wrap55 wow fadeInDown">
                <h4 class="media-heading">'.$rec['Project_Name'].'</h4></a>
                <p> by <i>'.$rec['user_name'].'</i></p>
                <p>'.$rec['short_dis'].'</p>
            </div>
        </div>
    </div>';
}

//if($result) maybe some condition to check if you have any data?
$result .= 
'<div class="col-sm-1 col-md-12">
    <div class="media services-wrap55 wow fadeInDown"> 
        <center><h3 class="media-heading"><div class="more" id="more">Load More Post</div></h3></center>
    </div>
</div>';

echo json_encode($result);

js脚本

$(document).ready(function() {
    $('#more').click(function() {
        var get_last_post_display = $('.unique-class:last').attr('id'); //get ip last <li>
        $('#more').html('<img src="ajax-loader.gif"');
        $.post('more_prj.php', 'last_id_post='+get_last_post_display, function(html) {
            if(html) {
                $('div').append(html);//$('.main-div') ?
                $('#more').text('Load More Project'); //add text "Load More Post" to button again
            } else {
                $('#more').text('No more Project to load'); // when last record add text "No more posts to load" to button.
            }
        }, 'json');
    });
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM