Many sites have links like "Add this to your favorites" etc. I have a recipe database i'd like a similar functionality for. I assume the best way to do this is with sessions. Have a user login, and then, when they click "add this recipe to your favorites" it will add to his/her username record. This is what I am thinking of doing: TBLRECIPES: Fields: RECIPE_ID, RECIPE_NAME TBLUSER: Fields: USER_ID, USER_NAME, USER_PASSWORD TBLFAVORITES: Fields: FAVORITE_ID, USER_ID, RECIPE_ID I can put this code on each recipe page: <!------ I have a variable "x" on each page that is the recipe_id----> <!------ Also, session.user_id is set as the current user's user ID----> <cfset current_recipe_id = #x#> <!------ query to see if it's already listed as user's favorite----> <cfquery="favorites" datasource="mydatabase"> SELECT r.RECIPE_ID, u.USER_ID, t.FAVORITE_ID, t.USER_ID, t.RECIPE_ID FROM (TBLRECIPES AS r INNER JOIN TBLFAVORITES AS t ON r.RECIPE_ID = t.RECIPE_ID ) INNER JOIN TBLUSER AS u ON t.USER_ID = u.USER_ID WHERE RECIPE_ID = #current_recipe_id# AND USER_ID = #session.user_id# </cfquery> <cfif favorites.recipe_id NEQ ""> This recipe is in your favorites. <cfelse> [COLOR="Red"] <a href="......??....">Add recipe to favorites!</a>[/COLOR] </cfiif> Code (markup): So, if it's not in the user's favorites, a link would appear saying "add recipe to favorites!". I would like it so when a user clicks this, it adds a record to the TBLFAVORITES with a USER_ID = #session.user_id# and a RECIPE_ID = #current_recipe_id#. So, I don't know how to do this with a hyperlink (see above code in RED). Also, my other question would be, how would a user be able to REMOVE a recipe from his/her favorites? Would I have to user the query above and somehow say "delete record WHERE FAVORITE_ID = #favorites.FAVORITE_ID#"? Thanks.
You could do this two ways, A hyperlink <a href="addfav.cfm?RECIPE_ID=#current_recipe_id#">Add to Favorits</a> Code (markup): Then on the addfav.cfm page <cfquery......> INSERT INTO TABLERECIPIES (USER_ID, RECIPE_ID) VALUES ('#SESSION.USER_ID#', '#URL.RECIPE_ID#') </cfquery> Code (markup): Or with a form and hidden field <cfoutput> <form action="addfav.cfm" methode="post"> <input type="hidden" name="Recipe_id" value="#favorites.current_recipe_id#'/> <input type="submit"/> </cfoutput> </form> Code (markup): Then on the addfav.cfm page <cfquery......> INSERT INTO TABLERECIPIES (USER_ID, RECIPE_ID) VALUES ('#SESSION.USER_ID#', '#FORM.RECIPE_ID#') </cfquery> Code (markup):
How can I do it without having them sent to an adfav.cfm page? I would like them to click "add recipe" and then it changes to "recipe is added", without sending the user to another page.
They dont have to go to another page. Just change the <a href></a> to the page their on and put your cfquery on that same page with an if statment on top of it. Like this <cfif IsDefinded("FORM.current_recipe_id")> <cfquery.....> Insert INto whatever table( userd, fav_recipe) VALUES '#SESSION.USER_ID, '#FORM.CURRENT.RECIPE_ID#') </cfquery> </cfif> Code (markup): When the click the link or the form button it will just refreash the page and run the insert statement. The cfquery wont run if the cfif statment if not definded. The statment wont run if