?
Solved

Updating multiple records based on dropdown option submitted values

Posted on 2007-04-04
8
Medium Priority
?
447 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
6 Comments
 
LVL 32

Expert Comment

by:Brendt Hess
ID: 18852849
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
ID: 18852875
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:Brendt Hess
ID: 18852927
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:asaworker
ID: 18852950
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
ID: 18852970
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 2000 total points
ID: 18852975
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

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Stellar Phoenix SQL Database Repair software easily fixes the suspect mode issue of SQL Server database. It is a simple process to bring the database from suspect mode to normal mode. Check out the video and fix the SQL database suspect mode problem.

592 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