Coldfusion , How to build a "save as favorite" items list.

Posted on 2010-09-07
Medium Priority
Last Modified: 2013-12-24
CFMX 7, Windows 2003, Mysql

On my real estate listings site I want  the prospect to be able to save properties as favorites on a cookie by clicking an "add" or "remove" link on my results page and then output the favorites on my page as a link : MLSID | StreetNumber StreetName

Is there any simple way to accomplish this? code examples appreciated!

Question by:Bang-O-Matic
  • 5
  • 5
  • 2
  • +1
LVL 16

Expert Comment

by:Gurpreet Singh Randhawa
ID: 33623955
I suggest you use Database tables for this! There are many advantages to it!

1) You can manipulate them  from admin panel for any user
2) Different members will have different kinds of bookmarks stored for them.
3) They can enable/disable the favorites if they want to view or not in their members area!
4) Deletion of links is possible.
5) You can count the visits of each link visited and track its most visited and can also add it to the top.
6) you can display the count against it.

7) and much more, that can help you build a better save as favorite list!

Same Example could be like this!

CREATE  TABLE `zimyellowpages`.`favorites` (

  `fav_key` INT NOT NULL ,

  `FavoriteName` VARCHAR(255) NULL ,

  `visits` INT NULL DEFAULT 0 ,

  `favoriteURL` VARCHAR(255) NULL ,

  `isOpera` VARCHAR(45) NULL ,

  `isMozilla` VARCHAR(45) NULL ,

  `isChrome` VARCHAR(45) NULL ,


  PRIMARY KEY (`fav_key`) ,

  UNIQUE INDEX `FavoriteName_UNIQUE` (`FavoriteName` ASC) )


now you can create your coldfusion code where you can manipulate everything, you can also store cookies in your system!

Expert Comment

ID: 33623958
Hey, very simple if you just want to store the data in a cookie.  You can set a cookie using the <cfcookie> tag.

If you want this cookie to last, maintaining the user's favorites list between browser sessions, your cfm code would set the cookie on the client's browser like this:
<cfif not isDefined( 'cookie.favorites' )>
  <cfcookie name="favorites" expires="never" value="">

Putting the above code in your Application.cfm file, or in the onRequestStart() method of an Application.cfc file would definitely be useful, to make sure that the cookie is always created when the user first comes to any of the pages on your site.

Then, if you are having the client submit a list of favorites, you can store the entire list in the cookie by using the cookie scope.
Ex: <cfset cookie.favorites = "1,2,3,4,5,6">
Ex: <cfset cookie.favorites = form.favorites>

When you want to read back each individual favorite in the cookie, you can treat it just like any other variable:
<cfloop list="#cookie.favorites#" index="favorite">

Hope that helps!
LVL 16

Expert Comment

by:Gurpreet Singh Randhawa
ID: 33623981
Maintaining Cookies can be cumbersome if you have a big list of details for your favorite list!

Cookies can be used and a combination of cookie/database can help you in building a better system, if yuou still reply on COOKIES, that has many disadvantages,

Some browers do not allow cookies as it might have been disabled by the user on his/her machine!

sometimes the website gets blocked due to heavy usage of cookies.
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.


Author Comment

ID: 33627807
Since I'm not a programmer and know very little about cfml, I'd like to stick with the quickest and easiest method of accomplishing this task. then build on that (maybe incorporating the database) once I have some working code and understand whats going on. I had posed this same question a couple yrs back here, and got a solution that seemed simple enough but didn't completely work, for me.  the code I'm working with is attached below and was written by dgrafx .  I got the cookie set on my results.cfm page and built the addfavorites.cfm page and have the "add or remove" links showing up on my results .cfm page but when i click to add a fav, the resulting link shows up but does not carry over my MLSID | ADDRESS it just looks like this on the page :  #listlast(ii,"|")# and the url is yourrealestatepage.cfm?RealID=#listfirst(ii,

How can I work my MLSID and ADDRESS variables into this code?

<!---on my results.cfm page--->
<cfif Not structkeyexists(cookie,"favpage")>
<cfset cookie.favpage="">

<!---make the add or remove link for each record displayed--->
show a link that says "Add to favorites" or "Remove from fav" like this:
<a href="addfavorites.cfm?favID=#RealID#|#trim(RealName)#" target="hide"><cfif listfindnocase(cookie.favpage,"#RealID#|#trim(RealName)#")>Remove From Favorites<cfelse>Add To Favorites</cfif></a>
<!---you see how the target is "hide"
create a zero size iframe-the reason is so that they don't leave the page when clicking the add link.--->

<iframe name="hide" id="hide" style="height:0px;width:0px;"></iframe>

<!---Then on addfavorites.cfm--->
<cfif structkeyexists(url,"favID")>
<cfif NOT listfindnocase(cookie.favpage,favID)>
<cfcookie name="favpage" value="#ListAppend(cookie.favpage,favID)#" expires="30"><!--- adds to if not there already --->
<cfcookie name="favpage" value="#ListDeleteAt(cookie.favpage,listfind(cookie.favpage,favID))#" expires="30"><!--- deletes if there already --->

<!---then on your left nav you do:--->
<cfloop list="#cookie.favpage#" index="ii"> 
<a href="yourrealestatepage.cfm?RealID=#listfirst(ii,"|")#">#listlast(ii,"|")#</a><br>

Open in new window

LVL 25

Expert Comment

ID: 33628330
in ColdFusion you need to surround code with <cfoutput> tags.
if you do not then you will see #variable# instead of the variables value.

just for simplicities sake - add <cfoutput> at the very beginning of your page and </cfoutput> at the very end and then try it

Also it looks like you want to use MLSID instead of RealID and ADDRESS instead of RealName - just swap out the names - simple as that!

good luck ...
LVL 25

Expert Comment

ID: 33628351
oh - and very important - this method uses ONE cookie - just to clarify ...

Author Comment

ID: 33629076
Thanks Its working! one more small detail, how would I put a "remove" link beside each property on the output list of favs? it works but the only way to remove from favs is the go back and find the property and click remove.  would I just add the add remove link like this?

<cfloop list="#cookie.favpage#" index="ii">
<a href="yourrealestatepage.cfm?MLSID=#listfirst(ii,"|")#" class="smallgraytext">#listlast(ii,"|")#</a>
| <a href="addfavorites.cfm?favID=#MLSID#|#StreetNumber# #Street#" target="hide"><cfif listfindnocase(cookie.favpage,"#MLSID#|#trim(Street)#")>Remove From Favorites<cfelse>Add To Favorites</cfif></a>

LVL 25

Accepted Solution

dgrafx earned 2000 total points
ID: 33629616
you do it the same as the other page except you don't loop through your cookie
you just have the link to addfavorites.cfm and for the text of the link you simply say
if listfindnocase(cookie.favpage etc ...

note that in order for this to work you need to send into addfavorites.cfm the EXACT same string as what you are testing for with the listfindnocase(cookie.favpage,"?????")

you CANNOT add mlsid & streetnumber & street then check for something different - it looks like you are checking then for mlsid & street
this will always result in a difference ...

<a href="addfavorites.cfm?favID=#MLSID#|#(StreetNumber)# #trim(Street)#" target="hide"><cfif listfindnocase(cookie.favpage,"#MLSID#|#trim(StreetNumber)# #trim(Street)#")>Remove From Favorites<cfelse>Add To Favorites</cfif></a>

ALSO note that Trim() is a big deal!
if you trim on one side of an equation you nearly always ( depending on what you are doing ) trim on the other side or chances are you will get differences!
A good practice is to trim when inserting or updating you database as well.

Author Comment

ID: 33630805
"you do it the same as the other page except you don't loop through your cookie
you just have the link to addfavorites.cfm and for the text of the link you simply say
if listfindnocase(cookie.favpage etc ..."

Please pardon me but I don't understand...If I dont have it in the loop, how would I get a "remove" link for each saved property that was output in the loop?  Attached is a screenshot note the "My Favorites" at the bottom right what I need is a remove link beside each favorite.

LVL 25

Expert Comment

ID: 33630918
i assumed you have a list of properties where the links normally reside
then if a person clicks a property they are then taken to a details page that has just the one property ........

Author Comment

ID: 33631341
Oh I see what you are saying now, and yes I do have details page where the link, as you explained above, would work.
Is there any way to add a remove link to thefavs loop as pictured??

 I know you have answered the original question and I will give you the points.  

Author Closing Comment

ID: 33631369
LVL 25

Expert Comment

ID: 33632742
you mean where it says My Favorites ?
yes - just do it with the loop as stated above - I believe you were on the right track you just really need to pay attention to the notes i posted like check for the same thing you are inserting and trim etc ...
my response above meant to remove one single link that was on a detail page - like for example right by the property title

note: if this is a site where you have prospects sign in for any reason then i would eventually move this feature to a database feature.
same concept but a bit easier to manage and favs will last forever (or until deleted)
the cookie method you are using is fine for a simple solution but at any time IE (or any browser) could decide to delete your prospects cookie.favpage and of course then prospect will be mad at your site "crummy site - can't even keep track of a few properties"
C'est la vie ...

Featured Post

Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month16 days, 5 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question