Sunday, August 17, 2008

Dealing with the Search Form and conditionals.

This last week has been a learning curve of sorts. I spent about 3 hours with my friends husband, Phil, who in some ways managed to confuse me even more, but in most ways helped me work through some of the issues I was up against.

He took me through some of the basics of sql and helped me to understand some simple things relating to 'values', 'names' and 'id's'. I finally got the connection between the allocation of values and names when doing forms, and how they related to the php code when executing a search.

Just prior to meeting with Phil I had a breakthrough with my recipe search function and got the search form working dynamically, so that the drop-down menu pulled the information for the categories and ingredients directly from the database. I was really pleased to be able to work this funcion out on my own. But much of it was trial and error and not a lot of understanding. Phil was able to enlighten me on it and I was then able to work through the actual php coding for the recipe search with him.

We talked about how I had set up the 'submit recipe' function in my design and analysis document and we both determined that by asking the user to submit three connected ingredients to their recipe may make the search function too complicated. So I decided to scrap this idea and make it a little more simple. I wanted to keep the idea of limiting the search to just 16 ingredients, so Phil showed me a way to set-up the $query statement to reflect a direct search for a category and then a 'LIKE' statement to reflect a search for an ingredient. He came up with this:

$query = "SELECT * FROM recipe WHERE recipe_ingredients_list LIKE '%".$_POST['ingredients']."%' and category = $type";

I got home from meeting with him and got stuck into the details of the search form. I had to fiddle around with the Select query a bit to get it to work. I shifted the conditionals around a little bit and finally got it to work really well. At first I got it working, but it was clear that the category conditional was not working as I kept getting recipes displayed which didn't fit in with the chosen category, but after shifting the order of the conditionals around I had success. This is what I ended up with:

$query = "SELECT recipe_title AS Recipe, recipe_ingredients_list AS Ingredients, recipe_method AS Method FROM recipe WHERE category_id = $type AND recipe_ingredients_list LIKE '%".$_POST['ingredients']."%'ORDER BY recipe_title ASC, recipe_ingredients_list ASC, recipe_method ASC";
//This $query uses the LIKE conditional and asks for any recipe within the designated category which contains the designated ingredient, LIKE that described in the search form, through the 'search for recipe' function on each page of the site. It calls for the title, ingredients list and method for each relevant recipe.

} else {

$query = "SELECT recipe_title AS Recipe, recipe_ingredients_list AS Ingredients, recipe_method AS Method FROM recipe WHERE recipe_ingredients_list LIKE '%".$_POST['ingredients']."%'ORDER BY recipe_title ASC, recipe_ingredients_list ASC, recipe_method ASC"; }
//This calls for any recipe within the designated ingredient list and does not require a designated category. Because some users may want to only search for a recipe with a certain ingredient without having to specify a category. Choosing a category is not compulsory. $result = @mysql_query ($query);

I managed to refine the $query even more by adding the ORDER BY conditional, and adding some headings and borders, to create a simple table to display the results in. The result was simple, but effective. Not sure if I will have the time to refine the layout for it, but am happy that I got the dynmics working, which fulfills the requirements of the assignment.

No comments: