我有一个有查询的变量:
$countPage="select count(id) from ` admin_crmf_poc_event_history` ";
计算表中的行数。 我想将count(id)除以另一个变量:
$recordPerPage= '30';
在一个页面中显示30条记录 这样我想要发生这种情况:
$sumPages=$countPage/$recordPerPage;
解决方案可能是错误的,这就是我要问的原因。我想将$sumPages
变量分配给$totalPages
变量。请帮忙吗?
答案 0 :(得分:0)
只有这样你才能做到这一点 -
$sumPages=$countPage/$recordPerPage;
请注意,如果你有40个记录可以产生1.33 [3 ...]来修复你将需要使用ceil函数,因为没有这样的东西会使它成为2个页面。
像这样 -$ sumPages =小区($ countPage / $ recordPerPage);
答案 1 :(得分:0)
在查询中尝试以下内容
$recordPerPage= '30';
$countPage="select count(id), (count(id)/{$recordPerPage}) from ` admin_crmf_poc_event_history` ";
$conn = mysqli_connect($server, $username, $password, $database) or die(mysqli_error($conn));
$result = mysqli_query($conn, $countPage) or die(mysqli_error($conn));
if($result) {
$row = mysqli_fetch_row($result) or die(mysqli_error($conn));
echo 'There are ' . $row[0] . ' records with a total of ' . ceil($row[1]) . ' pages.';
mysqli_free_result($result);
} else {
echo 'No records found';
}
mysqli_close($conn);
$sql = "SELECT *,(SELECT COUNT(id) FROM admin_crmf_poc_event_history) records FROM admin_crmf_poc_event_history` where $condition order by event_date asc LIMIT {$startPoint},{$recordPerPage}";
答案 2 :(得分:0)
你必须尝试这样
$pages=mysql_query("select count(id) from ` admin_crmf_poc_event_history` ");
$countPage = mysql_fetch_object($pages);
$recordPerPage= 30;
if($countPage >= $recorPerPage) /*Incase your count page is less then 30 U'll get incorrect value*/
$sumPages=ceil($countPage/$recordPerPage);
else
$sumPages=$countPage;
小心:)