簡體   English   中英

將一個變量除以另一個變量?

[英]divide one variable to another?

我有一個查詢變量:

$countPage="select count(id) from ` admin_crmf_poc_event_history` ";

它計算表中的行數。 我想將count(id)與另一個變量分開:

$recordPerPage= '30';

在一頁上顯示30條記錄,以便我希望發生這種情況:

$sumPages=$countPage/$recordPerPage;

解決方案可能是錯誤的,這就是我要問的原因。 我想將$sumPages變量分配給$totalPages變量。 請幫忙?

$ countpage還不是一個真正的變量,您只是編寫了查詢,但是還沒有發送任何內容給mysql驅動程序。 然后,您仍然需要獲取count(id)變量。

只有這樣,你才能做到這一點-

$sumPages=$countPage/$recordPerPage;

請注意,如果您說40條記錄,它將產生1.33 [3 ...]來修復您將需要使用ceil函數witch將其設置為2的情況,因為沒有這樣的東西1.3頁。

像這樣 -

$ sumPages = ceil($ countPage / $ recordPerPage);

在查詢中嘗試以下操作

$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}";

你必須這樣嘗試

$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;

照顧自己 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM