Help with keyword variables & queries

I’m trying to tweak to the PHPmotion script (http://demo.phpmotiontemplates.com/default/)
to add another category to search.

I was told to do this:

  1. Capture the category $_GET and $_POST values. Look through the top of search.php file,
    and you can see how the keyword is retrieved and stored in a variable -
    the “keyword”/“keywords” variables - you can do the same for your category".

  2. After you’ve done that, you can do a check to see whether a category value was passed, and if so, add a conditional clause to the mysql queries in search.php
    add an extra WHERE clause to check based on the category value that was passed.

I’ve done some coding, but these instructions a bit beyond my skill level.

Any additional guidance or interpretation of these instructions will be appreciated.

Much thanks
Search.txt (13.5 KB)

The way I understand it, they want you to see if the $keyword and $keywords variables are empty to see if any category was passed or requested.

You’re already doing that by capturing the GET variable in

$keywords    = $_GET['keyword'];
$keywords    = str_replace(''s', "", $keywords)

The second line is just to replace single quotes with nothing.
Of course, if the variable is not empty is because someone passed a category and therefore in your query string you’ll need to add

"WHERE category= '$keywords' "

I am assuming that the name of the field is category.

Sorry I can’t be more explicit as it is kind of late here and I’m a bit tired but that’s the general idea.

Thanks for your reply. much appreciated.
Can you (or anyone) please give me a code example of what you mean, please?
Do you mean something like this:

$keywords    = $_GET['keyword'];
$keywords    = str_replace(''s', "", $keywords) "WHERE category= '$keywords' "

or this:

$keywords    = $_GET['keyword'];
$keywords    = str_replace(''s', "WHERE category= '$keywords' ", $keywords)

or something else?

Any additional guidance will be much appreciated.

No, tha’ts not the answer. It is something else.

$keywords    = $_GET['keyword']; 

This line simply gets the value from the form (if it is sent using the GET method only) ans assigns it to the variable $keywords

$keywords    = str_replace(''s', "", $keywords)

This line searches for sigle quotes in the variable $keywords and, if there’s one, substitutes it with nothing. The result of this operaiton is reassigned to the variable $keywords again

if $keywords!=“” (that is, if $keywords is different from nothing which means that there’s something there) then you have to add the text " WHERE name_of_field = $keywords" at the end of the $querystring.

I assume that variable this is the variable that where looking for.

IF $keywrods!="" {$querystring= $querystring . " WHERE  name_of_field = $keywords"; }

if you don’t want to make it equal, you could also use LIKE %$keywords% (that would be that the search result will look for term kept in the variable $keywords in any part of the field name_of_field

Thanks for your reply. And the explanation.
However I still don’t understand this:

"I assume that variable this is the variable that where looking for.

IF $keywrods!=“” {$querystring= $querystring . " WHERE name_of_field = $keywords"; }
if you don’t want to make it equal, you could also use LIKE %$keywords% (that would be that the search result will look for term kept in the variable $keywords in any part of the field name_of_field"

And I don’t know anything about making it equal or not

And additional help, or mod to the code, will be appreciated.

I assume that variable this is the variable that where looking for.

It measn that I don’t have the time right now to study the whole code and it looks like this is the variable with the main SLQ query.

IF $keywrods!="" {$querystring= $querystring . " WHERE name_of_field = $keywords"; }

It is the PHP way to say that if $keywords is not empty, then $querystring needs to change. The change will be appending the text " WHERE name_of_field = $keywords" at the end of the query.

Example:

$queryString=" SELECT movie.name, movie.releaseDate FROM movie"

Let’s say that someone wanted to see the titles for the category “acction”. Then the query will need to be changed to

$queryString="SELECT movie.name, movie.releaseDate FROM movie **WHERE category='action'**"

Now you will show the titles and releae dates of all the movies under that category. And the name of the category needs to be ‘action’ exactly.

But what if the category is not action but something else like ‘action and romance’ or ‘action and horror’ or ‘action and fantasy

Then the query will not work. But if you changed it to

$queryString="SELECT movie.name, movie.releaseDate FROM movie **WHERE category LIKE %'action'%**"

then you will show titles and release dates of all the movies under any category that includes the word ‘action’ in its description