老师好,上个问题的代码
index.php
<?php
$link=mysqli_connect("localhost","root","");
mysqli_select_db($link,"imoocmall");
mysqli_set_charset($link,"utf8");
$sql="SELECT id,name,price FROM im_goods LIMIT 4";
$obj=mysqli_query($link,$sql);
$result=mysqli_fetch_all($obj);
//var_dump($result);die;
mysqli_close($link);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>
<title>AJAX练习</title>
</head>
<body>
<div style="text-align: center">
<table align="center" border="1px">
<tr>
<th>编号</th>
<th>名字</th>
<th>价格</th>
</tr>
<?php foreach ($result as $v):?>
<tr>
<td><?php echo $v[0]?></td>
<td><?php echo $v[1]?></td>
<td><?php echo $v[2]?></td>
</tr>
<?php endforeach;?>
</table>
<button id="bid">加载更多</button>
<script>
$('#bid').click(function () {
var num=$('tr').length-1;
$.post('admin.php',{number:num},function (data) {
if(data.code==100){
$('#bid').remove();
}
if(data.code==200){
$('table').append(data.msg);
}
if(data.code==300){
$('table').append(data.msg);
$('#bid').remove();
}
},'json');
})
</script>
</div>
</body>
</html>
admin.php
<?php
$num=isset($_POST["number"]) ? $_POST["number"] : 4;
$link=mysqli_connect("localhost","root","");
mysqli_select_db($link,"imoocmall");
mysqli_set_charset($link,"utf8");
$sql="SELECT COUNT(*) AS tbNum FROM im_goods";
$obj=mysqli_query($link,$sql);
$result=mysqli_fetch_assoc($obj);
$tbNum=$result["tbNum"];
$msg='';
//var_dump($tbNum);die;
if($num>=$tbNum){
die(json_encode(array('msg'=>$msg,'code'=>100)));
}else{
$sql="SELECT id,name,price FROM im_goods LIMIT {$num},4";
$obj=mysqli_query($link,$sql);
$msg='';
while ($result=mysqli_fetch_assoc($obj)){
$msg.="<tr><td>{$result['id']}</td><td>{$result['name']}</td><td>{$result['price']}</td></tr>";
}
if($num+4>=$tbNum){
die(json_encode(array('msg'=>$msg,'code'=>100)));
}else{
die(json_encode(array('msg'=>$msg,'code'=>300)));
}
}
正在回答 回答被采纳积分+1
- 参与学习 人
- 提交作业 225 份
- 解答问题 3372 个
掌握用PHP开发互联网网站的必备功能,掌握当下主流的Linux系统开发,并熟练使用热门框架ThinkPhp开发电商团购项目,是通向PHP工程师必经之路。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星