0

I have a problem to get total rows counting with condition. This is my code

$pagesize = 10;
$recordstart = (int)$_GET['recordstart'];
$recordstart = (isset($_GET['recordstart'])) ? $recordstart : 0;

$town = $_GET['label_town'];
$sub = ucfirst($_GET['label_sub']);

Here is my selecting condition. I also use this condition to count totalrows

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);
}

Selecting and displaying data works fine but problem is with SELECT COUNT. Always count totalrows no mater what condition is. Where I making wrong?

Thanks!

1
  • 4
    You need to pass $where into totalrows() - it's out of scope. Commented Apr 12, 2013 at 14:57

1 Answer 1

3

Your COUNT is within function definition, where $where isn't defined. You need to pass it as a parameter or move query outside the function.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.