Skip to main content
Participant
January 27, 2009
Answered

Help! How to do a 'Save to Print' option

  • January 27, 2009
  • 2 replies
  • 553 views
Okay, so I admit I know just enough ColdFusion to be dangerous but not efficient.

I am developing an Obituary database for a client including Last Name, First Name, Date of Death, Source, Source Date, etc. Each record has a unique ID as primary key.

The end user can search for records that meet various search criteria. The client would like the end user to be able to save various records to a "print record" that would store them until they are ready to print all the records. (Client is a library so they want to save printing paper).

I've tried to tackle this problem in multiple ways but without much success. I think I may be limited by my knowledge of client and session variables and/or structs and arrays.

Because of my lack of knowledge of structs and arrays, I set up a table in my database called print list and simply stored the ID of the record when the end user added it to the print list. The problem, however, is that if mutiple users are using the database at one time, the print list will not be exclusive to that particular user.

I need to know the most efficient way to allow the end user to add to and delete from a list of print records and of course eventually print the record and empty the list, but do so in a way that multiple users on separate workstations could potentially do so simulaneously without interfere with each other.

Any ideas would be much appreciated.
    This topic has been closed for replies.
    Correct answer
    This is exactly why session variables were invented...for this kind of situation, and thus, you should scrap the table in your database and use session variables. e.g:

    Try this code out in a test page and it should give you some idea how to accomplish this:

    <cflock scope="session" type="exclusive" timeout="30">
    <cfset session.printList = arrayNew(1) />
    </cflock>

    <cfset arrayAppend(session.printList, "someID") />
    <cfset arrayAppend(session.printList, "someOtherID") />
    <cfset arrayAppend(session.printList, "yetAnotherID") />
    <cfset arrayAppend(session.printList, "thisCouldBeAnotherID") />

    <cfdump var="#session.printList#" />

    <cflock scope="session" type="readonly" timeout="30">

    <cfoutput>
    <ul>
    <cfloop index="i" from="1" to="#arrayLen(session.printList)#">
    <li>This is array Item <strong>#i#</strong> and its value is <strong>#session.printList #</strong></li>
    </cfloop>
    </ul>
    </cfoutput>

    </cflock>


    It basically create a variable in the session scope which is unique to the user and then in this variable we create an array. A simple array is NOT complicated, don't be worried about this. All an array is, is literally a list. We make lists all the time in daily life and this is the programming equivelant. It gives us an ordered way to store and play with items.

    The example shows you the creation of the session variable with the array...I also show populating the array using arrayAppend...try adding some of your own and refreshing the page. You'll then see in the CFDUMP what this variable holds.

    Finally, I play with the array with a simple CFLOOP...just loop out the list to the screen.

    Good luck,
    Mikey.

    2 replies

    Correct answer
    January 27, 2009
    This is exactly why session variables were invented...for this kind of situation, and thus, you should scrap the table in your database and use session variables. e.g:

    Try this code out in a test page and it should give you some idea how to accomplish this:

    <cflock scope="session" type="exclusive" timeout="30">
    <cfset session.printList = arrayNew(1) />
    </cflock>

    <cfset arrayAppend(session.printList, "someID") />
    <cfset arrayAppend(session.printList, "someOtherID") />
    <cfset arrayAppend(session.printList, "yetAnotherID") />
    <cfset arrayAppend(session.printList, "thisCouldBeAnotherID") />

    <cfdump var="#session.printList#" />

    <cflock scope="session" type="readonly" timeout="30">

    <cfoutput>
    <ul>
    <cfloop index="i" from="1" to="#arrayLen(session.printList)#">
    <li>This is array Item <strong>#i#</strong> and its value is <strong>#session.printList #</strong></li>
    </cfloop>
    </ul>
    </cfoutput>

    </cflock>


    It basically create a variable in the session scope which is unique to the user and then in this variable we create an array. A simple array is NOT complicated, don't be worried about this. All an array is, is literally a list. We make lists all the time in daily life and this is the programming equivelant. It gives us an ordered way to store and play with items.

    The example shows you the creation of the session variable with the array...I also show populating the array using arrayAppend...try adding some of your own and refreshing the page. You'll then see in the CFDUMP what this variable holds.

    Finally, I play with the array with a simple CFLOOP...just loop out the list to the screen.

    Good luck,
    Mikey.
    Participant
    January 27, 2009
    Thanks, Mikey! This is exactly what I was looking for. It worked like a charm. The only thing I needed to add was the after the array item in the loop so it would display correctly.

    You just made what was very intimidating to me (session variables and arrays) much easier for me wrap my head around.

    Thanks again!
    January 27, 2009
    No worries at all.

    This is what the CF community is here for. I'm not an expert, but I help those who may know a little less than me. And in turn each helps his own other relative CF friend. Let's keep this community going.

    Mikey.
    Inspiring
    January 27, 2009
    InstantJoe wrote:

    >
    > Any ideas would be much appreciated.
    >

    The first idea is to read up and learn about the Session scope. It
    exists for this exact purpose, is well covered in the documentation and
    all over the intranet and is much to large a topic to cover top to
    bottom in one, user supported, message forum post.

    Your database concept is not necessarily a bad way to do this though.
    It has some trade offs, but one potential nice one is that a user could
    build a print list over two or more sessions before actually printing.

    All you need to do is to add some type of user id to your record id to
    the print table so that it is possible to know which users belong to
    which print requests.

    But this goes back to the session scope as that would be the natural way
    to track user ids. If you want the down and dirtiest way to do this,
    look at the CFID and CFTOKEN session values automatically generated by
    ColdFusion when Session state is enabled. These will uniquely identify
    a user who has started a session.

    Not the most secure, and an application's requirements will likely grow
    quickly so that a more sophisticated user model will be needed. But
    that should get you started.

    HTH
    Ian
    Participant
    January 27, 2009
    Thanks for the input, Ian. I did look a bit at session scope. So if I'm understanding you correctly, I should set a UserID variable to CFToken in the session scope and then use that to identify print records for a particular individual?

    And my understanding is that using session variables even of the same name will be unique on each workstation? Do they die when closing the browser or only on the session time out designated in the application file?

    Thanks again.