1

(Edit: This is a follow-up to a previous question)

I'm working on complete redesign of a site that allows users to check out multiple machines for multiple users.

Here's what I have so far.

A front page that outputs two cfqueries.

One query is the active machines (ie, already checked out) It's a basic cfoutput query and displays the info based on the values of the query. I use a cfif statement to filter only the active machines (value = 1 if active in SQL)

It also outputs stuff like MAC, IP OS etc.

The other query is the inactive machines that a user can check out. It is also filtered based on whether it is active, this time by cfif query.active eq 0. This query has a few form fields so that the user can check the check boxes of devices to check in, enter in the require fields (userID and comments, basically) and submit it.

There is a dropdown menu at the bottom of this page that allows you to select the action, and the form passes in an action number (form.choose_action) that tells the cfc what it should do. IE if the action number is 4, that tells the cfc to send the person to the edit page and passes the deviceID along with it.

So basically I'm passing in the DeviceID in every situation, and the UserID only if the machine is active.

When the user clicks submit, it goes straight to a page which calls a cfc that does most of the heavy lifting.

All in all, this is an extremely poor solution, as it only really works well when one machine is being checked in or out at a time.

Here's a picture to better explain what's going on.

example

Here's what I would like:

Multiple devices can be selected and be checked in/checked out, hopefully using checkboxes in some way.

If checking a machine out for a user, UserIDs have to be somehow attached to the respective devices in the entry form (the inactive output query) and should be entered along with the deviceID in a single insert, if possible.

I want to be able to check in multiple devices, but also stop people from checking out machines that are active (as they are already checked out). Same goes for inactive machines, I would like to stop a user from checking in a machine that has yet to be checked out. As of now I'm just using number (ie if field is greater than 0 )

Some notes: Only one user is going to be using this at a time, so I don't have to worry about session stuff or having lots of people trying to check in the same device.

At the moment I currently push variables to a page which then passes the form data into a CFC, this might not be the best way to do it.

Really the big problem I'm having is from a design standpoint, my logic is very convoluted and inefficient. At the moment the only thing I could think of is passing the device values in through an array. But not sure if I could do this in a checkbox.

Thank you for your time.

Community
  • 1
  • 1
jorblume
  • 607
  • 6
  • 19

2 Answers2

2

Since the devices may be assigned to different users, you need a naming convention that will allow you to associate each deviceID with its related fields ie userID, comments, etcetera. One option is to add the "deviceID" as a suffix to each set of fields:

  <cfoutput query="yourQuery">
      Device ID <input type="checkbox" name="deviceID" value="#deviceID#">
      UserID:<input type="text" name="userID_#deviceID#" ...>
      Comment:<input type="text" name="comment_#deviceID#" ...>
  </cfoutput>

Say your query contained three device id's ie 35,48,52 the resulting field names would look like this. Notice each set of fields shares the same device id.

 <input type="checkbox" name="deviceID" value="35">
 <input type="text" name="userID_35" ...>
 <input type="text" name="comment_35" ...>

 <input type="checkbox" name="deviceID" value="48">
 <input type="text" name="userID_48" ...>
 <input type="text" name="comment_48" ...>

 <input type="checkbox" name="deviceID" value="52">
 <input type="text" name="userID_52" ...>
 <input type="text" name="comment_52" ...>

Then when the form is submitted, form.deviceID will contain a comma delimited list of the checked devices. You can loop through that list and use the current ID to grab the associated userID and comment values. Once you have those values, use the action type to run an INSERT, UPDATE or whatever you need to do.

Note, again since each device may be assigned to a different user, you cannot do the INSERT in a single statement

  <cfparam value="form.deviceID" default="">

  <!--- for each device id .. --->
  <cfloop list="#form.deviceID#" index="currDeviceID">

      <!--- grab the associated user and comment --->
      <cfset userID = FORM["userID_"& currDeviceID]>
      <cfset comment = FORM["comment_"& currDeviceID]>

      ... validate ...

      <cfquery ...>
           run your query
      </cfquery>

  </cfloop>
Leigh
  • 28,765
  • 10
  • 55
  • 103
  • 1
    This method works very well and it lends itself to friendly javascript as well. For example if you have something on that row that triggers an event it might look like this (if you're not using a library like jQuery) `onClick = "javascript: doSomethingToThisRow(#yourQuery.deviceID#);` then your function may look like `function doSomethingToThisRow(device){getElementById("userID_"+device)...}` – genericHCU Dec 06 '12 at 12:21
1

Q: At the moment the only thing I could think of is passing the device values in through an array. But not sure if I could do this in a checkbox.

A: When multiple checkboxes are check, ColdFusion sees them as a list of values. So you would get something like

checkedout=4312,5231,51

if three check boxes were checked

checkedin=31,9876

if two items were checked.

James A Mohler
  • 11,060
  • 15
  • 46
  • 72
  • Ah I see, so you could split the list with a loop and assign each variable to a value? I guess the problem is that this all plays off a sql insert that really can only work if one variable is specified in the insert. – jorblume Dec 05 '12 at 19:37
  • SELECT can use `` Insert has to loop – James A Mohler Dec 05 '12 at 19:45