我有一个问题来计算有条件的总行数。这是我的代码
$pagesize = 10;
$recordstart = (int)$_GET['recordstart'];
$recordstart = (isset($_GET['recordstart'])) ? $recordstart : 0;
$town = $_GET['label_town'];
$sub = ucfirst($_GET['label_sub']);
这是我的选择条件。我也使用这个条件来计算总行数
if (isset($_GET['label_town']) === true && isset($_GET['label_sub']) === true) {
$where = "WHERE p.label_town = '$town' AND p.label_sub = '$sub' AND `visible` = 1";
} else if (isset($_GET['label_town']) === true && isset($_GET['label_sub']) === false) {
$where = "WHERE p.label_town = '$town' AND `visible` = 1";
} else {
$where = "WHERE `visible` = 1";
}
// Selecting data
$all_page_index = mysql_query("SELECT p.page_id, p.timestamp, p.label_town, p.label_sub, p.ime_nekretnine, p.mjesto, p.cijena_noc, p.krevet_apart, p.broj_apart, p.min_nocenja, p.description, p.visits, i.image_id, i.page_id, i.ext
FROM data_page AS p
LEFT JOIN (
SELECT MAX( image_id ) AS max, page_id
FROM images
GROUP BY page_id
) AS n ON p.page_id = n.page_id
LEFT JOIN images AS i ON i.image_id = n.max
$where
ORDER BY p.page_id DESC
LIMIT $recordstart, $pagesize");
// Counting rows with conditions
function totalrows() {
return mysql_result(mysql_query("SELECT COUNT(p.page_id) FROM data_page AS p $where"), 0);
}
选择和显示数据工作正常,但问题在于 SELECT COUNT。无论条件如何,始终计算总行数。我在哪里做错了?
谢谢!