Hi there!
Been struggling with this for days now! I'd really appreciate it if someone can spot what's wrong with my code below. It's intended to call up information from my database, based on two variables - Area and Keywords - which users enter on a previous page. This particular section is where I count how many results are returned so that I can use it later on.
The funny thing is, the code below works fine if the user only types in Keywords (to try it go to www.hotelheaven.co.uk and type in, say, Buckingham Palace).... However, when you type London into the Area box and Buckingham Palace into Keywords, it all goes to pot....
Also, I've tested all the individual queries in phpmyadmin and they all work fine.
Anyway, here's the code:
class Pager
{
function getPagerData($numHits, $limit, $page)
{
$numHits = (int) $numHits;
$limit = max((int) $limit, 1);
$page = (int) $page;
$numPages = ceil($numHits / $limit);
$page = max($page, 1);
$page = min($page, $numPages);
$offset = ($page - 1) * $limit;
$ret = new stdClass;
$ret->offset = $offset;
$ret->limit = $limit;
$ret->numPages = $numPages;
$ret->page = $page;
return $ret;
}
}
$keywords = $_POST['keywords'];
// add + to the first keyword
$keywords = '+' . $keywords;
// replace ' ' with ' +' so you add a + to every keyword
$keywords = str_replace(' ', ' +', $keywords);
$area = $_POST['area'];
// get the pager input values
$page = $_GET['page'];
$limit = 10;
if (!empty($area)) {$a = 1;} else {$a = 0;};
if (!empty($keywords)) {$k = 1;} else {$k = 0;};
$flags = $a . "-" . $k;
switch ($flags) {
case ("1-1"):
$result = mysql_query("SELECT count(*)
FROM
hotel h
LEFT JOIN
amenities a
ON h.Hotel_Id=a.Hotel_Id
WHERE (MATCH ((h.Town,h.DescriptionEnglish,a.Amenities)
AGAINST ('$keywords' IN BOOLEAN MODE)))
AND (h.Town LIKE '%" . $area . "%'
OR h.County LIKE '%" . $area . "%'
OR h.Region LIKE '%" . $area . "%'
OR h.Postcode LIKE '%" . $area . "%'
OR h.GeoVillage LIKE '%" . $area . "%'
OR h.GeoBorough LIKE '%" . $area . "%') ");
break;
case ("1-0"):
$result = mysql_query("SELECT count(*)
FROM
hotel
WHERE
Town LIKE '%" . $area . "%'
OR County LIKE '%" . $area . "%'
OR Region LIKE '%" . $area . "%'
OR Postcode LIKE '%" . $area . "%'
OR GeoVillage LIKE '%" . $area . "%'
OR GeoBorough LIKE '%" . $area . "%' ");
break;
case ("0-1"):
$result = mysql_query("SELECT count(*)
FROM
hotel h
LEFT JOIN
amenities a
ON
h.Hotel_Id=a.Hotel_Id
WHERE MATCH
(h.Town,h.DescriptionEnglish,a.Amenities)
AGAINST
('$keywords' IN BOOLEAN MODE) ");
break;
default:
exit;
break;
}
$total = mysql_result($result, 0, 0);
// work out the pager values
$pager = Pager::getPagerData($total, $limit, $page);
$offset = $pager->offset;
$limit = $pager->limit;
$page = $pager->page;
// CREATE A START VARIABLE FOR USE WITH YOUR LIMIT LATER ON
$start = ($page-1) * $limit;
// THE STARTING NUMBER IS 0, SO I ADDED 1
$starting_no = $start + 1;
if ($total - $start < $limit) {
$end_count = $total;
} elseif ($total - $start >= $limit) {
$end_count = $start + $limit;
}
//for the 'sort by' stuff
$defaultSortMethod = 'Hotel_Id';
$sort_by = array ( "1" => "RoomsFrom ASC", "2" => "RoomsFrom DESC", "3" => "Hotel_Id" );
$sortBy = isset($sort_by[$_GET['s']]) ? $sort_by[$_GET['s']] : $defaultSortMethod;
I'd be extremely grateful if anyone can spot where I'm going wrong! I don't think it's anything to do with the Pager class stuff as I'm using this on plenty of other pages with no problem, and also it's working fine here with the Keywords at least. But I just don't know why the Area variables won't work.... please help!!!
LizzyD