
请高手帮助我解释一下下面这段代码,我学习的是jsp,对于PHP是菜鸟,求高手指点,感激不尽!
请高手帮助我解释一下下面这段代码,我学习的是jsp,对于PHP是菜鸟,求高手指点,感激不尽!(每个步骤详细)<?php$page=isset($_POST['page']...
请高手帮助我解释一下下面这段代码,我学习的是jsp,对于PHP是菜鸟,求高手指点,感激不尽!
(每个步骤详细)
<?php
$page = isset($_POST['page']) ? intval($_POST['page']) : 1;
$rows = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
$offset = ($page-1)*$rows;
$result = array();
$conn = mysql_connect('127.0.0.1','root','root');
mysql_select_db('mydb',$conn);
$rs = mysql_query("select count(*) from item");
$row = mysql_fetch_row($rs);
$result["total"] = $row[0];
$rs = mysql_query("select * from item limit $offset,$rows");
$rows = array();
while($row = mysql_fetch_object($rs)){
array_push($rows, $row);
}
$result["rows"] = $rows;
echo json_encode($result);
?> 展开
(每个步骤详细)
<?php
$page = isset($_POST['page']) ? intval($_POST['page']) : 1;
$rows = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
$offset = ($page-1)*$rows;
$result = array();
$conn = mysql_connect('127.0.0.1','root','root');
mysql_select_db('mydb',$conn);
$rs = mysql_query("select count(*) from item");
$row = mysql_fetch_row($rs);
$result["total"] = $row[0];
$rs = mysql_query("select * from item limit $offset,$rows");
$rows = array();
while($row = mysql_fetch_object($rs)){
array_push($rows, $row);
}
$result["rows"] = $rows;
echo json_encode($result);
?> 展开
2个回答
展开全部
<?php
// ? : 三元操作符 如果$_POST['page']已赋值(Request URL中包含page=xxx),将$_POST['page'])的值转换成int赋值给page,如果未赋值则$page=1,这种情况很常见,没有翻页之前可以不包含$page参数
$page = isset($_POST['page']) ? intval($_POST['page']) : 1;
//同上,应该是每页的记录数量
$rows = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
//计算当前页所处位置,用于limit查询,这个是游标的起始位置,MySQL中 limit 0,10 的意思就是从第0条开始,选择10条数据,相当于sqlserver中的 select top 10 * from xxx
$offset = ($page-1)*$rows;
$result = array(); //定义$result数组
//连接本机MySQL数据库,账号root,密码root
$conn = mysql_connect('127.0.0.1','root','root');
//选择mydb库
mysql_select_db('mydb',$conn);
//下面三行为获取记录总行数,用于计算分页后的总页数
$rs = mysql_query("select count(*) from item");
$row = mysql_fetch_row($rs); //获取游标
$result["total"] = $row[0]; //获取count(*)的值
//选择当前分页
$rs = mysql_query("select * from item limit $offset,$rows");
$rows = array();
//游标遍历,获取数据,直至mysql_fetch_object返回空值等于false时跳出循环
while($row = mysql_fetch_object($rs)){
//提取数据存入数组
array_push($rows, $row);
}
//数组存入更大的数组
$result["rows"] = $rows;
//json编码
echo json_encode($result);
?>
// ? : 三元操作符 如果$_POST['page']已赋值(Request URL中包含page=xxx),将$_POST['page'])的值转换成int赋值给page,如果未赋值则$page=1,这种情况很常见,没有翻页之前可以不包含$page参数
$page = isset($_POST['page']) ? intval($_POST['page']) : 1;
//同上,应该是每页的记录数量
$rows = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
//计算当前页所处位置,用于limit查询,这个是游标的起始位置,MySQL中 limit 0,10 的意思就是从第0条开始,选择10条数据,相当于sqlserver中的 select top 10 * from xxx
$offset = ($page-1)*$rows;
$result = array(); //定义$result数组
//连接本机MySQL数据库,账号root,密码root
$conn = mysql_connect('127.0.0.1','root','root');
//选择mydb库
mysql_select_db('mydb',$conn);
//下面三行为获取记录总行数,用于计算分页后的总页数
$rs = mysql_query("select count(*) from item");
$row = mysql_fetch_row($rs); //获取游标
$result["total"] = $row[0]; //获取count(*)的值
//选择当前分页
$rs = mysql_query("select * from item limit $offset,$rows");
$rows = array();
//游标遍历,获取数据,直至mysql_fetch_object返回空值等于false时跳出循环
while($row = mysql_fetch_object($rs)){
//提取数据存入数组
array_push($rows, $row);
}
//数组存入更大的数组
$result["rows"] = $rows;
//json编码
echo json_encode($result);
?>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询