I have the following generated query below which when using this code, works fine
$r->execute();
$numrows = $r->fetch();
$numrows = $numrows[0];
WITH LIMIT AS ( SELECT *, ROW_NUMBER() OVER (ORDER BY ID) AS 'RowNumber' FROM Intranet WHERE Contract_Number like :keyword) select COUNT(*) from LIMIT WHERE Contract_Number like :keywordB
But the user has a multiple choice system and then when they use the second choice it does make things more difficult but I feel that I have managed it pretty well and seems to make sense.
So this is now what the query looks like with the second choice added
WITH LIMIT AS ( SELECT *, ROW_NUMBER() OVER (ORDER BY ID) AS 'RowNumber' FROM Intranet WHERE Contract_Number like :keyword AND Contract_Status = :keyword1) select COUNT(*) from LIMIT WHERE Contract_Number like :keywordB AND Contract_Status = :keywordB1
And here is the whole code that builds up to the count part not working.
$query = "WITH LIMIT AS ( SELECT *, ROW_NUMBER() OVER (ORDER BY ID) AS 'RowNumber' FROM Intranet WHERE {limit clause})
select COUNT(*) from LIMIT WHERE {limit clause b}";
if ($i === 0) {
$limitClause .= " " . $field_name . " " . $operator . " :keyword";
$limitClauseB .= " " . $field_name . " " . (${'operator' . 'B'} = $operator) . " :keywordB";
} else {
$limitClause .= " AND " . $field_name . " " . (${'operator' . $i} = $operator) . " :keyword".$i;
$limitClauseB .= " AND " . $field_name . " " . (${'operatorB' . $i} = $operator) . " :keywordB".$i;
}
$query = str_replace('{limit clause}', $limitClause, $query);
$query = str_replace('{limit clause b}', $limitClauseB, $query);
$r=$conn->prepare($query);
if (strpos($query, ":keyword") !== false) {
if ($operator == 'like') {
$keywordb = $keyword.'%';
$r->bindParam(':keyword', $keywordb);
} else {
$r->bindParam(':keyword', $keyword);
}
}
if (strpos($query, ":keyword1") !== false) {
if ($operator1 == 'like') {
$keywordb = $keyword.'%';
$r->bindParam(':keyword1', $keywordb);
} else {
$r->bindParam(':keyword1', $keyword);
}
}
if (strpos($query, ":keywordB") !== false) {
if ($operatorB == 'like') {
$keywordb = $keyword.'%';
$r->bindParam(':keywordB', $keywordb);
} else {
$r->bindParam(':keywordB', $keyword);
}
}
if (strpos($query, ":keywordB1") !== false) {
if ($operatorB1 == 'like') {
$keywordb = $keyword.'%';
$r->bindParam(':keywordB1', $keywordb);
} else {
$r->bindParam(':keywordB1', $keyword);
}
}
if (strpos($query, ":contractLike") !== false) {$r->bindParam(':contractLike', $contractLike); }
$r->execute();
//echo ($r->queryString);
$numrows = $r->fetch();
$numrows = $numrows[0];
So like I say its all good when only one choice is made but when the user adds to the search if you like building the query up, it seems I cant get $numrows to show the correct count, in fact it shows 0, so its not counting anything.