Solved

Updating multiple records based on dropdown option submitted values

Posted on 2007-04-04
8
422 Views
Last Modified: 2012-08-14
I have a query that supposed to get the value from a multiple select dropdown and update only those records in a database. I also want it to add a sequence number based on the order the option shows in in a dropdown. Query below

<cffunction name="setFeaturedCourse" returntype="void" output="false">
            <cfargument name="id" type="numeric" required="yes">
            <cfargument name="featuredcourse" type="string" required="yes">
            <cfquery name="updateFeaturedCourse" datasource="#application.dsName#" username="#application.dsUserName#" password="#application.dsPassword#">
                  UPDATE courses SET
                        featuredcourse = '#arguments.featuredcourse#'
                        
                  WHERE auto_no = '#arguments.id#'
            </cfquery>
            <cfreturn>
      </cffunction>

cfm page guts:
<cfsilent>
<cfif isUserInRole("myASA")>
      <cfinvoke component="/amsacfcs/user" method="getUser" returnvariable="theUser" />
<cfelse>
      <cflocation url="#request.startPath#index.cfm" >
</cfif>
<cfinvoke component="/amsacfcs/training" method="getAllCourseDetails" returnvariable="theAllDetails" />
<cfif isDefined("form.id")>
      <!--- The form has been submitted, check for required fields --->      
            <!--- Display --->
      
      

      
      <cfif NOT isDefined("errorCode")>
      

            <!--- All required fields have been entered, check for a valid date --->
                  
            <cfif form.id NEQ 0>
                  <!--- Modify --->
                  <cfinvoke component="/amsacfcs/training" method="setFeaturedCourse">
                        <cfinvokeargument name="id" value="#form.id#">                        
                        <cfinvokeargument name="featuredcourse" value="#form.coursefeatured#">
                  </cfinvoke>
                  <cflocation url="choose.cfm?sid=crs&aid=m">
<!--- Modifying Previous Entries --->
            </cfif>
      </cfif>            
      <cfset theID = "#form.id#">
      <cfset theFeaturedCourse = "#form.coursefeatured#">      
</cfif>
<cfinvoke component="/amsacfcs/training" method="getCourseCategoryTypes" returnvariable="courseCategoryListing" />

</cfsilent>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Test</title>
<cfoutput>
<script src="#request.startPath#includes/script.js" type="text/javascript" language="JavaScript"></script>
<script language="JavaScript" type="text/javascript">
      function openCalendar(update_field) {
            openstr = "calendar.cfm?update_field=" + update_field;
            newwin = window.open(openstr, "asaCalendar", "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=1,width=250,height=300;");
      }
</script>
<style type="text/css" media="all">
      /* adjust appearance because of no breadcrumb */
      h2.subhomehead{margin:2.25em 40px .8em 0;}
      .courses { font-size:.9em;}
      .row0 {background-color: ##ffffff;}  
      .row1 {background-color: ##f5f5f4;}
      .buttons {width:130px;height:30px;}  
      select { width:650px; }
      table caption {font: 1.2em/1.2em "Trebuchet MS", Arial, Sans, Helvetica, sans-serif;
      font-style: normal;
      font-weight: bold;
      padding: 0 0 .5em 0;
      color: ##215a95;
      text-align:left;
      white-space: normal;}
</style>
<!--[if IE]>
<link rel="stylesheet" type="text/css" href="#request.startPath#includes/all_ie.css" />
<![endif]-->
<!--[if lt IE 6]>
<link rel="stylesheet" type="text/css" href="#request.startPath#includes/ie5.css" />
<![endif]-->
<script language="JavaScript" type="text/javascript">
<!-- Begin
function moveOver(whereto) { //1
var selectedItem = document.theForm.availablecourses.selectedIndex;
var selectedText = document.theForm.availablecourses.options[selectedItem].text;
var selectedValue = document.theForm.availablecourses.options[selectedItem].value;
var i;
var isNew = true;

switch (whereto) { //2
      case 0:   //if they are filling in the Stafford box
      var boxLength = document.theForm.featuredcourses.length;
      if (boxLength != 0) { //3
            for (i = 0; i < boxLength; i++) { //4
                  thisitem = document.theForm.featuredcourses.options[i].text;
                  if (thisitem == selectedText) {  //5
                        isNew = false;
                        return "1";
                  } //5
            }//4
      }//3

      if (isNew && boxLength <= 2) {//3
            newoption = new Option(selectedText, selectedValue, false, false);
            document.theForm.featuredcourses.options[boxLength] = newoption;
      } else {
            if (isNew) { //4
                  alert("You are limited to 3 Featured Courses.")

            } //4
      }//3
            
      return "1";



} //2
} //1




function removeMe(wherefrom) { //1

switch (wherefrom) { //2

case 0:  //remove from Stafford box
var boxLength = document.theForm.featuredcourses.length;
arrSelected = new Array();
var count = 0;
      for (i = 0; i < boxLength; i++) { //3
            if (document.theForm.featuredcourses.options[i].selected) { //4
                  arrSelected[count] = document.theForm.featuredcourses.options[i].value;
            } //4
      count++;
      } //3
      
var x;
for (i = 0; i < boxLength; i++) { //4
      for (x = 0; x < arrSelected.length; x++) { //5
            if (document.theForm.featuredcourses.options[i].value == arrSelected[x]) { //6
                  document.theForm.featuredcourses.options[i] = null;
            } //6
      } //5
boxLength = document.theForm.featuredcourses.length;
} //4

return;

} //2
} //1

function move(f,bDir,sName) {
      var el = f.elements["featuredcourses"]
      var idx = el.selectedIndex
      if (idx==-1)
            alert("You must first select the course to reorder.")
      else {
            var nxidx = idx+( bDir? -1 : 1)
            if (nxidx<0) nxidx=el.length-1
            if (nxidx>=el.length) nxidx=0
            var oldVal = el[idx].value
            var oldText = el[idx].text
            el[idx].value = el[nxidx].value
            el[idx].text = el[nxidx].text
            el[nxidx].value = oldVal
            el[nxidx].text = oldText
            el.selectedIndex = nxidx
      }
}

function setFeaturedCourses() {
var boxLength = document.theForm.featuredcourses.length;
var arrText = new Array(3);
var arrValue = new Array(3);
var newValue = "";
var tempVar = "";
var finalValue = "";
                  for (i = 0; i < boxLength; i++) { //4
                        arrText[i] = document.theForm.featuredcourses.options[i].text;
                        arrValue[i] = document.theForm.featuredcourses.options[i].value;
                        newValue = arrValue[i];
                        tempVar = eval("document.theForm.coursefeatured_" + newValue);
                        tempVar.value="Y";
                        finalValue = tempVar.value;
                        //alert(arrText[i] + "\n\nFeatured Course=" + finalValue);
                  }
}


-->
</script>
</cfoutput>
</head>

<body><!--- The BODY contains the padding that pushes in the SIZER --->

                              <p>You are limited to adding 3 Featured Courses.</p>
                              <cfoutput><form name="theForm" <!---action="#getFileFromPath(getBaseTemplatePath())#"---> method="post"></cfoutput>
                              
                              <cfoutput query="theAllDetails">
                                                      <input type="hidden" name="id_#auto_no#" id="id_#auto_no#" value="#auto_no#" />
                                                      <input type="hidden" name="coursefeatured_#auto_no#" id="coursefeatured_#auto_no#" value="#featuredcourse#" />
                                                      </cfoutput>
                              <fieldset class="manage">
                                    <table cellpadding="0" cellspacing="0" border="0">
                                          <caption>Available Courses</caption>
                                          <tr>
                                                <td valign="top" colspan="2">
                                                      <select name="availablecourses" class="courses" size="20" multiple="multiple" ondblclick="moveOver(0);">
                                                      <cfoutput query="theAllDetails">
                                                      <cfset class="row#Int(theAllDetails.CurrentRow MOD 2)#">  
                                                      <option value="#auto_no#" class="#class#">#title#</option>
                                                      </cfoutput>
                                                      </select></td>
                                          </tr>
                                          <tr>
                                                <td valign="top" colspan="2">
                                                      <input name="button" type="button" class="buttons" onclick="moveOver(0);" value="Add Course &#8595;" style="margin: 10px 0 10px 0;" /></td>
                                          </tr>
                                    </table>
                                    <table cellpadding="0" cellspacing="0" border="0">
                                          <caption>Featured Courses</caption>
                                          <tr>
                                                <td valign="top">
                                      <select name="featuredcourses" size="7" multiple="multiple">
                                    </select></td>
                                                <td valign="top"><input name="button" class="buttons" type="button" onclick="removeMe(0);" value="Remove Course" /><br/><input type="button" class="buttons" value="Move Up &#8593;" onclick="move(this.form,true,'test');" /><br/><input type="button" class="buttons" value="Move Down &#8595;" onclick="move(this.form,false,'test');" /></td>
                            </tr>
                            <tr>
                                                <td colspan="2">
                                                      
                                                            <input type="button" class="buttons" name="Submit" value="Create" style="margin: 10px 0 10px 0;" onclick="setFeaturedCourses();" />
                                                      </td>
                                          </tr>
                                    </table>
                                    </fieldset>
                              </form>
                        
</body>
</html>
0
Comment
Question by:asaworker
  • 3
  • 2
8 Comments
 
LVL 32

Expert Comment

by:bhess1
Comment Utility
I'm afraid that most of us are not going to find the information we need to answer your question in this HTML and ColdFusion code.  What we need is:

What you want to do.
Sample Before data
The data you have to work with (what you would submit to the query)
Sample After data

If we have this, we can examine the SQL code and ensure it performs as needed or show you code that should do what is needed.

If the question is about ColdFusion, we really are not your best resource.  Same with HTML.
0
 

Author Comment

by:asaworker
Comment Utility
I need to update multiple records with a new featuredcourse value based on what options are listed in the featuredcourses listbox. I have the values correctly, for me its a sql issue in how to get the values passed to the cfc.
0
 
LVL 32

Expert Comment

by:bhess1
Comment Utility
Okay - now we have the "what you want to do" piece clearly.

Can you provide examples of data prior to the update, the information you have available, and what you want the data to look like after the update?

Thanks!
0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

Author Comment

by:asaworker
Comment Utility
Before:
auto_no      title                           featuredcourse
79      Student Financial Literacy      N

After:
auto_no      title                           featuredcourse
79      Student Financial Literacy      Y

0
 

Author Comment

by:asaworker
Comment Utility
SQL code in cffucntion

UPDATE courses SET
featuredcourse = '#Evaluate("coursefeatured_" & ThisRow)#'
WHERE auto_no='#Evaluate("id" & ThisRow)#'
0
 
LVL 36

Accepted Solution

by:
SidFishes earned 500 total points
Comment Utility
to submit to a cfc from a form you need to either specify the method directly

<form method="post" action="http:/someserver.com/com/youcfc.cfc?method=setFeaturedCourse'>


or set up a form handler (i prefer this way...i use it as a form switchbox)


<form method="post" action="http:/someserver.com/act/act_formhandler.cfm">

act_formhandler.cfm
--------------------------

<cfif isdefined('form.submitbtnname')>

<cfinvoke method = "setFeaturedCourse"
                  Component = "com.yourCFC"
                  returnVariable = "whatever">
<cfinvokeargument name="featuredcourse" value="#form.featuredcourse#">
<cfinvokeargument name="Order" value="#form.displayorder#">
            </cfinvoke>

</cfif>


you'll also have to do some coding in the cfc to break the featuredcourse list returned by the multiple selections and match that to the displayorder list



0

Featured Post

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
INSERT INTO SELECT JOIN THING 2 24
C# SQL BULK INSERT CLASS 5 33
Trouble connecting to SqlServer database 4 29
Sql query 34 16
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Recently while working on a project I got a very annoying cfdocument has no body error message. I had never seen this error before. So I checked the code. The code was pretty simple; it was Just showing me the cfdocumnt tag and inside that tag a …
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how the fundamental information of how to create a table.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now