Javascript Ajax使用Php加载更多/显示更少onClick按钮
下面是使用Jquery/ajax从mysql数据库获取数据的代码 我有加载更多的按钮在它是没有任何问题的工作。 但我想添加显示较少的按钮也我怎么可以添加? (假设每次“加载更多”单击时显示12条记录。当我单击“加载更多”时,它将显示24条记录,并且它还应显示“显示更少”按钮 当用户单击“显示较少”按钮时,剩余的记录数将为12,显示较少的按钮应隐藏。如果用户多次单击“显示较多”按钮,显示较少的按钮将相应工作,则仅当仅剩余记录数等于12时,该按钮才会隐藏。) 有人能帮我在我的代码或更好的代码中添加showless按钮吗 PHPJavascript Ajax使用Php加载更多/显示更少onClick按钮,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,下面是使用Jquery/ajax从mysql数据库获取数据的代码 我有加载更多的按钮在它是没有任何问题的工作。 但我想添加显示较少的按钮也我怎么可以添加? (假设每次“加载更多”单击时显示12条记录。当我单击“加载更多”时,它将显示24条记录,并且它还应显示“显示更少”按钮 当用户单击“显示较少”按钮时,剩余的记录数将为12,显示较少的按钮应隐藏。如果用户多次单击“显示较多”按钮,显示较少的按钮将相应工作,则仅当仅剩余记录数等于12时,该按钮才会隐藏。) 有人能帮我在我的代码或更好的代码中添加s
<div id="alldata">
<?php
$serial = 1;
$query = mysqli_query($dba, "SELECT * FROM mybooklibrary WHERE status = 1 limit 12");
while ($get_info = mysqli_fetch_array($query)) {
$eid = $get_info['id'];
$ename = $get_info['ename'];
$enames = $get_info['enames'];
$aname = $get_info['aname'];
$filename = $get_info['filename'];
//remove brackets or whatever from name of current product
$aReplace = array('(', ')', '.');
$ename_replaces = str_replace($aReplace , '', $ename);
//////////////////////////////////////////////////////////
$url_ename6 = url_making($ename_replaces);
?>
<div class="moreData" id="<?php echo $eid;?>">
<div class="sidebar">
<div class="widget widget-shop">
<div class="product">
<div class="product-image" style="width: 112px;">
<a href="author/<?php echo $url_ename6; ?>">
<img style="box-shadow: 3px 3px 3px #ccc; border-radius: 3%;" alt="" src="<?php echo $filename; ?>">
</a>
<!--<span class="product-new">NEW</span>-->
</div>
<div class="product-description">
<!--<div class="product-category">Man - <?php echo $art_id; ?></div>-->
<div class="product-titlex">
<a href="bookDetails/<?php echo $url_ename6; ?>" style="font-weight: bold; text-shadow: 3px 3px 3px #ccc;">
<?php echo $ename; ?>
</a>
<div style="width: 200%;">
<a href="bookDetails/<?php echo $url_ename6; ?>" style="color: gray;" style="">
<?php echo $enames; ?>
</a>
</div>
<hr style="min-width: 500%;">
<div>
<a href="bookDetails/<?php echo $url_ename6; ?>" class="ur1" style="font-weight: bold; float: right;">
<?php echo $aname; ?></a>
</div>
</div>
</div><hr style="border: 1px solid black; min-width: 300%;">
</div>
<!-- end: Sidebar-->
</div>
</div>
</div>
<?php $serial++; } ?>
</div>
<div id="load<?php echo $eid; ?>">
<a class="load btn btn-block" id="<?php echo $eid; ?>" >Load more</a>
</div>
<div id="alldata">
<?php
$serial = 1;
$query = mysqli_query($dba, "SELECT * FROM mybooklibrary WHERE status = 1 limit 12");
while ($get_info = mysqli_fetch_array($query)) {
$eid = $get_info['id'];
$ename = $get_info['ename'];
$enames = $get_info['enames'];
$aname = $get_info['aname'];
$filename = $get_info['filename'];
//remove brackets or whatever from name of current product
$aReplace = array('(', ')', '.');
$ename_replaces = str_replace($aReplace , '', $ename);
//////////////////////////////////////////////////////////
$url_ename6 = url_making($ename_replaces);
?>
<div class="moreData" id="<?php echo $eid;?>">
<div class="sidebar">
<div class="widget widget-shop">
<div class="product">
<div class="product-image" style="width: 112px;">
<a href="author/<?php echo $url_ename6; ?>">
<img style="box-shadow: 3px 3px 3px #ccc; border-radius: 3%;" alt="" src="<?php echo $filename; ?>">
</a>
<!--<span class="product-new">NEW</span>-->
</div>
<div class="product-description">
<!--<div class="product-category">Man - <?php echo $art_id; ?></div>-->
<div class="product-titlex">
<a href="bookDetails/<?php echo $url_ename6; ?>" style="font-weight: bold; text-shadow: 3px 3px 3px #ccc;">
<?php echo $ename; ?>
</a>
<div style="width: 200%;">
<a href="bookDetails/<?php echo $url_ename6; ?>" style="color: gray;" style="">
<?php echo $enames; ?>
</a>
</div>
<hr style="min-width: 500%;">
<div>
<a href="bookDetails/<?php echo $url_ename6; ?>" class="ur1" style="font-weight: bold; float: right;">
<?php echo $aname; ?></a>
</div>
</div>
</div><hr style="border: 1px solid black; min-width: 300%;">
</div>
<!-- end: Sidebar-->
</div>
</div>
</div>
<?php $serial++; } ?>
</div>
<div id="load<?php echo $eid; ?>">
<a class="load btn btn-block" id="<?php echo $eid; ?>" >Load more</a>
</div>
好的解决方法如下:
我所做的是我制作了两个脚本,一个用于加载更多记录,另一个用于加载更少记录,但是get\u moredata.php
是相同的
在get_moredata.php
中,我做了如下两个查询:
if(isset($_POST['lastids'])) { //this id is for View Less
$lastid_less = mysql_real_escape_string($_POST['lastids']); //this id is for View Less
$query = mysqli_query($dba, "SELECT * FROM mybooklibrary WHERE id <= '".$lastid_less."'-12");
}else if(isset($_POST['lastid'])) { //this id is for View More
$lastid_more = mysql_real_escape_string($_POST['lastid']); //this id is for View More
$query = mysqli_query($dba, "SELECT * FROM mybooklibrary WHERE id > '".$lastid_more."' limit 12");
}
if(isset($\u POST['lastids']){//此id用于无视图
$lastid\u less=mysql\u real\u escape\u string($\u POST['lastids']);//此id用于无视图
$query=mysqli_query($dba,“从mybooklibrary中选择*,其中id为“”。$lastid_更多。“'limit 12”);
}
因此,它会在单击按钮时运行脚本
PHP
<div id="alldata">
<?php
$serial = 1;
$query = mysqli_query($dba, "SELECT * FROM mybooklibrary WHERE status = 1 limit 12");
while ($get_info = mysqli_fetch_array($query)) {
$eid = $get_info['id'];
$ename = $get_info['ename'];
$enames = $get_info['enames'];
$aname = $get_info['aname'];
$filename = $get_info['filename'];
//remove brackets or whatever from name of current product
$aReplace = array('(', ')', '.');
$ename_replaces = str_replace($aReplace , '', $ename);
//////////////////////////////////////////////////////////
$url_ename6 = url_making($ename_replaces);
?>
<div class="moreData" id="<?php echo $eid;?>">
<div class="sidebar">
<div class="widget widget-shop">
<div class="product">
<div class="product-image" style="width: 112px;">
<a href="author/<?php echo $url_ename6; ?>">
<img style="box-shadow: 3px 3px 3px #ccc; border-radius: 3%;" alt="" src="<?php echo $filename; ?>">
</a>
<!--<span class="product-new">NEW</span>-->
</div>
<div class="product-description">
<!--<div class="product-category">Man - <?php echo $art_id; ?></div>-->
<div class="product-titlex">
<a href="bookDetails/<?php echo $url_ename6; ?>" style="font-weight: bold; text-shadow: 3px 3px 3px #ccc;">
<?php echo $ename; ?>
</a>
<div style="width: 200%;">
<a href="bookDetails/<?php echo $url_ename6; ?>" style="color: gray;" style="">
<?php echo $enames; ?>
</a>
</div>
<hr style="min-width: 500%;">
<div>
<a href="bookDetails/<?php echo $url_ename6; ?>" class="ur1" style="font-weight: bold; float: right;">
<?php echo $aname; ?></a>
</div>
</div>
</div><hr style="border: 1px solid black; min-width: 300%;">
</div>
<!-- end: Sidebar-->
</div>
</div>
</div>
<?php $serial++; } ?>
</div>
<div id="load<?php echo $eid; ?>">
<a class="load btn btn-block" id="<?php echo $eid; ?>" >Load more</a>
</div>
<div id="alldata">
<?php
$serial = 1;
$query = mysqli_query($dba, "SELECT * FROM mybooklibrary WHERE status = 1 limit 12");
while ($get_info = mysqli_fetch_array($query)) {
$eid = $get_info['id'];
$ename = $get_info['ename'];
$enames = $get_info['enames'];
$aname = $get_info['aname'];
$filename = $get_info['filename'];
//remove brackets or whatever from name of current product
$aReplace = array('(', ')', '.');
$ename_replaces = str_replace($aReplace , '', $ename);
//////////////////////////////////////////////////////////
$url_ename6 = url_making($ename_replaces);
?>
<div class="moreData" id="<?php echo $eid;?>">
<div class="sidebar">
<div class="widget widget-shop">
<div class="product">
<div class="product-image" style="width: 112px;">
<a href="author/<?php echo $url_ename6; ?>">
<img style="box-shadow: 3px 3px 3px #ccc; border-radius: 3%;" alt="" src="<?php echo $filename; ?>">
</a>
<!--<span class="product-new">NEW</span>-->
</div>
<div class="product-description">
<!--<div class="product-category">Man - <?php echo $art_id; ?></div>-->
<div class="product-titlex">
<a href="bookDetails/<?php echo $url_ename6; ?>" style="font-weight: bold; text-shadow: 3px 3px 3px #ccc;">
<?php echo $ename; ?>
</a>
<div style="width: 200%;">
<a href="bookDetails/<?php echo $url_ename6; ?>" style="color: gray;" style="">
<?php echo $enames; ?>
</a>
</div>
<hr style="min-width: 500%;">
<div>
<a href="bookDetails/<?php echo $url_ename6; ?>" class="ur1" style="font-weight: bold; float: right;">
<?php echo $aname; ?></a>
</div>
</div>
</div><hr style="border: 1px solid black; min-width: 300%;">
</div>
<!-- end: Sidebar-->
</div>
</div>
</div>
<?php $serial++; } ?>
</div>
<div id="load<?php echo $eid; ?>">
<a class="load btn btn-block" id="<?php echo $eid; ?>" >Load more</a>
</div>
<div class="alldata">
<?php
include ("inc/db.php");
include ("inc/functions.php");
if(isset($_POST['lastids'])) {
$lastid_less = mysql_real_escape_string($_POST['lastids']); //this id is for View Less
$query = mysqli_query($dba, "SELECT * FROM mybooklibrary WHERE id <= '".$lastid_less."'-12");
}else if(isset($_POST['lastid'])) {
$lastid_more = mysql_real_escape_string($_POST['lastid']); //this id is for View More
$query = mysqli_query($dba, "SELECT * FROM mybooklibrary WHERE id > '".$lastid_more."' limit 12");
}
while ($get_info = mysqli_fetch_array($query)) {
$eid = $get_info['id'];
$ename = $get_info['ename'];
$enames = $get_info['enames'];
$aname = $get_info['aname'];
//remove brackets or whatever from name of current product
$aReplace = array('(', ')', '.');
$ename_replaces = str_replace($aReplace , '', $ename);
//////////////////////////////////////////////////////////
$url_ename6 = url_making($ename_replaces);
?>
<div class="moreData" id="<?php echo $eid;?>">
<div class="sidebar">
<div class="widget widget-shop">
<div class="product">
<div class="product-image" style="width: 112px;">
<a href="author/<?php echo $url_ename6; ?>">
<?php
$exts4 = array('png','jpg','jpeg','gif');
foreach (array_unique($exts4) as $ext4) {
if (file_exists("images/titles/".$get_info['id'].".".$ext4)) {
$path4 = "images/titles/".$get_info['id'].".".$ext4;
?>
<img style="box-shadow: 3px 3px 3px #ccc; border-radius: 3%;" alt="" src="<?php echo $path4; ?>">
<?php }} ?>
</a>
</div>
<div class="product-description">
<div class="product-titlex">
<a href="bookDetails/<?php echo $url_ename6; ?>" style="font-weight: bold; text-shadow: 3px 3px 3px #ccc;">
<?php echo $ename; ?>
</a>
<div style="width: 200%;">
<a href="bookDetails/<?php echo $url_ename6; ?>" style="color: gray;" style="">
<?php echo $enames; ?>
</a>
</div>
<hr style="min-width: 500%;">
<div>
<a href="bookDetails/<?php echo $url_ename6; ?>" class="ur1" style="font-weight: bold; float: right;">
<?php echo $aname; ?></a>
</div>
</div>
</div><hr style="border: 1px solid black; min-width: 300%;">
</div>
<!-- end: Sidebar-->
</div>
</div>
</div>
<?php } ?>
<?php
if (isset($lastid_less)) {
//count if clicked on lastid_less
$queryCount = mysqli_query($dba, "SELECT * FROM mybooklibrary WHERE id <= '$lastid_less'");
echo $get_Count = mysqli_num_rows($queryCount)-12;
}else{
//count if clicked on lastid_more
$queryCount = mysqli_query($dba, "SELECT * FROM mybooklibrary WHERE id <= '$lastid_more'");
echo $get_Count = mysqli_num_rows($queryCount)+12;
}
if ($get_Count <= 12) {
?>
<!-- show this button only if books are shown are less than equal to 12 -->
<!-- because my default is 12 -->
<div id="load<?php echo $eid; ?>" class="sidebar">
<hr>
<a class="load btn btn-block" id="<?php echo $eid; ?>" >View More</a>
</div>
<?php }else{ ?>
<!-- show both buttons if books are shown greater than 12 -->
<div id="load<?php echo $eid; ?>" class="sidebar">
<hr>
<a class="load btn btn-block" id="<?php echo $eid; ?>" >View More</a>
</div>
<div id="loads<?php echo $eid; ?>" class="sidebar">
<hr>
<a class="loads btn btn-block" id="<?php echo $eid; ?>" >View Less</a>
</div>
<?php } ?>
</div>
<script>
//this query is for Load More Records
$(document).ready(function(){
$(document).on("click",".load",function(){
var ids= $(this).attr("id");
$.ajax({
type: 'POST',
url: 'get_moredata.php',
cache:false,
data: {'lastid':ids},
success: function(response){
//appending the result get_moredata page result with div id alldata
$('#alldata').append(response);
//remove old load more button
$('#load'+ids).remove();
if(!response) {
$('.moreData').text('No more record to load');
}
}
});
});
});
</script>
<script>
//this query is for Load Less Records
$(document).ready(function(){
$(document).on("click",".loads",function(){
var ids= $(this).attr("id");
$.ajax({
type: 'POST',
url: 'get_moredata.php',
cache:false,
data: {'lastids':ids},
success: function(response){
//appending the result get_moredata page result with div id alldata
$('#alldata').append(response);
//remove old load more button
$('#loads'+ids).remove();
if(!response) {
$('.moreData').text('No more record to load');
}
}
});
});
});
</script>