Searching the Database by Tags
- This will require an AJAX query, whose format
we just get to make up.
For instance, we make the request with a URL like this:
http:server162.site:####/query?keyList=castle+nature%20scene
As before, we can teach the server to answer these requsts first, and
then teach the browser to make them.
Note that to URL-enclode a string with spaces and other special
characters, we can use the Javascript function
encodeURIComponent
, and to decode it we can use
decodeURIComponent
.
-
Use something like this
SQL command to check the location column (containing the
landmark tag) and the tags column for the desired tags:
SELECT * FROM photoTags WHERE (location = "Castle" OR
tags LIKE "%Castle%") AND (location = "Nature Scene" OR
tags LIKE "%Nature Scene%")
The percent signs match any string, just like the * in Unix.
-
The database callback should call a function
which puts together the http response. In the JSON which goes into the
body of the response, you can add a "message" property,
containing a string saying something like,
These are all of the photos satisfying this query.
If there are no photos satisfying the query, include only the message:
These were no photos satisfying this query.
With each photo, return its entire database record.
-
To check the input, you might want to use a Javascript regular
expression.
For instance, the expression
/[^a-z ]/.test(tag)
returns false when the string tag contains characters other
than lower-case digits and the space character.