Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialŽiga Krašovec
16,173 PointsSearch feature not working at all
Hi, I'm stuck in this course while trying to figure it out why my search feature won't work in any way
function search_catalog_array($search, $limit = null, $offset = 0){
include('connection.php');
//$category = strtolower($category);
try {
$sql = "SELECT media_id, title, category, img
FROM Media
WHERE title LIKE ?
ORDER BY
REPLACE (
REPLACE(
REPLACE(title, 'The ', ''),
'An ',
''
),
'A ',
''
)";
if(is_integer($limit)){
$results = $db->prepare($sql . " LIMIT ? OFFSET ? ");
$results->bindValue(1, "%".$search."%", PDO::PARAM_STR);
$results->bindParam(2, $limit, PDO::PARAM_INT);
$results->bindParam(3, $offset, PDO::PARAM_INT);
}else{
$results = $db->prepare($sql);
$results->bindValue(1, "%".$search."%", PDO::PARAM_STR);
}
$results->execute();
} catch (Exception $e) {
echo "Unable to retrieved results";
exit;
}
$catalog = $results->fetchALL(PDO::FETCH_ASSOC);
return $catalog;
}
function get_catalog_count($category = null, $search = null){
$category = strtolower($category);
include('connection.php');
try {
$sql = "SELECT COUNT(media_id) FROM Media";
if(!empty($search)){
$result = $db->prepare(
$sql
. " WHERE title LIKE ?");
$result->bindValue(1, "%".$search."%", PDO::PARAM_STR);
}elseif(!empty($category)){
$result = $db->prepare(
$sql
. " WHERE LOWER(category) = ?"
);
$result->bindParam(1, $category, PDO::PARAM_STR);
}else{
$result = $db->prepare($sql);
}
$result->execute();
} catch (Exception $e) {
echo "Bad query";
echo $e;
}
$count = $result->fetchColumn(0);
return $count;
}
Those are the two main functions that are being used (at least I think so). I checked the syntax numerous times and I can't figure out what it is that I'm doing wrong.
Thanks for any help!
Alex Bauer
9,426 PointsAlex Bauer
9,426 PointsI don't think anything is wrong with your functions code in functions.php because it matches up with mine and I just got my search function to work. I was having the same problem until I downloaded the file in the following video and compared her code to mine. The problem for me was in catalog.php I didn't use this if statement:
if (empty($current_page)) { $current_page = 1; }
After I implemented the code it worked perfectly. I hope this helps anyone who's also struggling with this.