Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Integrating PHP with Databases!
You have completed Integrating PHP with Databases!
Preview
Let’s create the function that actually pulls the items using our search term.
Links
SQL Like Operator
SQL Wildcards
Example Code
function search_catalog_array($search, $limit = null, $offset = 0) {
include("connection.php");
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();
return $catalog;
}
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
let's create the function that actually
pulls the items using our search term.
0:00
We'll duplicate the category,
catalog array.
0:01
And we'll name this search_catalog_array.
0:18
Instead of the argument category,
we'll wanna change that to search.
0:22
Then we're ready to replace
the WHERE statement as we did above.
0:27
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up