coldfusion infinity

I'm still building my testing application.  There have been a few changes in the scope of the project and I am returning for your expert advise.

Here's the situation:

A test has infinite chapters.
A chapter has infinite questions.

When a test taker enters the site, they sign in to their profile page.  This page needs to list the chapters that have been passed, and the ones that are available to take.

I'm wondering when and best way to call data from mysql db, and weather or not to put the past/available questions into the session, or individual page requests.  

Would there be any advantage to doing some of the "heavy lifting" by the mysql server?

Is that a clear enough explanation of what i need to do?

Thanks for your wisdom!
Who is Participating?
azadisaryevConnect With a Mentor Commented:
on the page that displays the list of chapters, add code similar to this at the top of the page (assuming you have session management enabled for your application):

<cfif NOT structkeyexists(session, 'userchapters')>
  <cfquery name="getChapters" datasource="...">
  SELECT c.chapter_id, c.chapter_name, IFNULL(uc.chapter_id, 0, 1) AS chapter_completed
  FROM chapters c LEFT JOIN user_chapters uc ON c.chapter_id = uc.chapter_id
  WHERE uc.user_id = <cfqueryparam cfsqltype="cf_sql_integer" value="#some-variable-that-holds-user-id#">
  ORDER_BY chapter_completed DESC, chapter_name
  <cflock scope="session" type="exclusive" timeout="20" throwontimeout="no">
    <cfset session.userchapters = structnew()>
    <cfoutput query="getChapters">
      <cfset session.userchapters[getChapters.chapter_id] = structnew()>
      <cfset session.userchapters[getChapters.chapter_id]['chaptername'] = getChapters.chapter_name>
      <cfset session.userchapters[getChapters.chapter_id]['completed'] = yesnoformat(getChapters.chapter_completed)>
<!--- display chapters list from session --->
<cfloop collection="#session.userchapters#" item="key">
  <p>#session.userchapters[key]['chaptername']# | <cfif session.userchapters[key]['completed']>completed<cfelse><a href="chapter.cfm?chapter_id=#key#>go to chapter</a></cfif></p>

the above code checks to see if session var exists that holds the chapters, and if not runs the query to select chapters for the user and populates the session structure with query data. it then outputs the data from that session var as chapters list with either 'completed' text or a link to chapter's page next to each chapter name.

edit the code as necessary.

if you have A LOT of different pages that will display the passed/not passed chapters, or if it is just one page, but it is requested by a user many times during the session (i.e. user is returned to that page after completing each new chapter) - then yes, stick that data into session scope structure after the user loggs into their profile. all you basically need to store in that structure is name and id of the chapters and a true/false completion flag (if the chapter has been completed or not by this user).

after a user completes a chapter, change the completion flag for that chapter in the session structure

[if your app is going to be under heavy load (lots of concurrent users) you better make sure your hardware is up-to-par for this...]

i do not really understand what 'heavy lifting' you are talking about having your db doing... what you have described does not look 'heavy' at all...

Short-Order-CoderAuthor Commented:
Hey Azadi!  

If there are an infinite number of chapters, how/when would I call them and display them as a list?

Am I making this more difficult than it needs to be?

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Short-Order-CoderAuthor Commented:
Wow.  I need to break this down to understand it.  I'll post back with any specific questions, but this looks to be perfect!
Short-Order-CoderAuthor Commented:
I am not this advanced.  Hell, I didn't even know there was a bracket structure in CF or what cflock was really for.

Is there a "less correct" way of doing this?  The strain on the server isn't going to be that intense.

I know this goes against the point of asking an expert....
Short-Order-CoderAuthor Commented:
For example,

If I have a table called "passed" where I stored passedId, chapId, userId, passedDate;

And I had a table that had a list of all chapters,

Would there be anything that could be spun from that?

The number of chapters is not going to change more than once per year.  Does that help me any?
Short-Order-CoderAuthor Commented:
or if when a user signs up, a table were created that stored all of the chapterId's and a null value for not-passed?
you do not need to create a separate table for each user to store all chapters with passed/not passed value - your current db setup is more than adequate for what you want to do.

you can sure forgo storing user's chapters in a session structure - just run a query and output the results, without putting them into any persistent scope.

based on your explanation of your table structures, this query will be something like this:

  <cfquery name="getChapters" datasource="...">
  SELECT c.chapId, c.chapName, IFNULL(p.chapId, 0, 1) AS chapPassed, p.passedDate
  FROM chapters c LEFT JOIN passed p ON c.chapId = p.chapId
  WHERE p.userId = <cfqueryparam cfsqltype="cf_sql_integer" value="#some-variable-that-holds-user-id#">
  ORDER_BY chapPassed DESC, chapName

just stick it on top of the page that displays the chapters list for user.
then output it to display capters to user:

<cfoutput query="getChapters">
  <p>#chapName# | <cfif chapPassed>passed (#dateformat(passedDate, "dd-mmm-yy")#<cfelse><a href="chapter.cfm?id=#chapID#>proceed to chapter</a></cfif></p>

that's as simple as it gets...

Short-Order-CoderAuthor Commented:
That makes much more sense to me (youre too smart).  

What is <cfqueryparam cfsqltype="cf_sql_integer" ?
from CFML Reference for <cfqueryparam> tag:
"Verifies the data type of a query parameter and, for DBMSs that support bind variables, enables ColdFusion to use bind variables in the SQL statement. Bind variable usage enhances performance when executing a cfquery statement multiple times."

you should use <cfqueryparam> tag, especially in your queries that use user-editable data, for these main reasons:
1) it creates 'bind variables' in your SQL, which make the query execute faster, especially if query caching is enabled
2) it helps protect your application against SQL Injections (because your DB knows never to execute a bind variable as if it was SQL code)
3) if can be helpful in verifying datatypes of data you pass to your query


if you do not have CFML Reference Manual, i strongly suggest you download one (free PDF) from

Short-Order-CoderAuthor Commented:
That certainly makes more sense.

I am working my way through the CFML manual.  I think I may also need to seek some more edu on programming fundamentals.  Any thoughts on where to find this?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.