Solved

Arrays

Posted on 2002-03-19
11
197 Views
Last Modified: 2013-12-24
I have 2 columns and 10 rows of input fields in which the user has to fill the Item and Qty.
once the form is submited,the values should insert into the database (which has two columns).if the user key-in for only 3 rows, i need only that 3 rows to be submitted.

any help is greatly appreciated.
 
0
Comment
Question by:karaikudi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 2

Expert Comment

by:Cyril_H
ID: 6882148
I'm not sure to understand corectly, could you post your form page ??
0
 

Expert Comment

by:manonng
ID: 6882313
hi

is your appication an online shopping cart? I would recommend you use the session query rather than an array.

manon
 
0
 
LVL 6

Expert Comment

by:reitzen
ID: 6882947
Here is a form that I have that has 7 (change yours to 10) rows available for data input.  Sorry for the extra columns but you cut what you don't need.  Some of the code is not necessary for your form, I simply cut and pasted my code.  Hope its not too confusing.

Once the form is submitted, it will only write to the database the number of rows that the user has input data into.

<cfscript>
Variables.iTrafficID = 0;
Variables.iLoopCounter = 0;
</cfscript>

<!--- Write the changes to the database --->
<cfif IsDefined("FORM.sSaveTraffic")>
      <cfloop from="#Variables.iLoopCounter#" to="7" index="i">
            <!--- Create the correct FORM fields --->
            <cfset Variables.sFieldTrafficID = "iTrafficID" & i>
            <cfset Variables.sFieldSourceID = "iSourceID" & i>
            <cfset Variables.sFieldTraffic = "iTraffic" & i>
            <cfset Variables.sFieldLease = "iLease" & i>
            <cfset Variables.sFieldCancel = "iCancel" & i>
            <cfset Variables.sFieldReject = "iReject" & i>
            <cfset Variables.sFieldCost = "iCost" & i>
            <!--- If the SourceID field is zero it means the user has not changed this record so
            do not write the record to the database, otherwise write it --->
            <cfif (Variables.iSourceID NEQ "0") AND (Variables.iSourceID NEQ "")>
                  <!--- Include the query file --->
                  <cfinclude template="Queries/Put_ReportOwner_Traffic.cfm">
            </cfif>
      </cfloop>
</cfif>

<!--- Include the query file to get the status of each stage --->
<cfset stFields = StructNew()>
<cfinclude template="Queries/Get_ReportOwner_Traffic.cfm">
<cfif Get_ReportOwner_Traffic.RecordCount NEQ "0">
      <!--- Loop through the recordset setting the structure keys with data --->
      <cfloop query="Get_ReportOwner_Traffic">
            <cfset x=StructInsert(stFields,"iTrafficID" & Variables.iLoopCounter, Get_ReportOwner_Traffic.TrafficID)>
            <cfset x=StructInsert(stFields,"iSourceID" & Variables.iLoopCounter, Get_ReportOwner_Traffic.SourceID)>
            <cfset x=StructInsert(stFields,"iTraffic" & Variables.iLoopCounter, Get_ReportOwner_Traffic.Traffic)>
            <cfset x=StructInsert(stFields,"iLease" & Variables.iLoopCounter, Get_ReportOwner_Traffic.Lease)>
            <cfset x=StructInsert(stFields,"iCancel" & Variables.iLoopCounter, Get_ReportOwner_Traffic.Cancel)>
            <cfset x=StructInsert(stFields,"iReject" & Variables.iLoopCounter, Get_ReportOwner_Traffic.Reject)>
            <cfset x=StructInsert(stFields,"iCost" & Variables.iLoopCounter, Get_ReportOwner_Traffic.Cost)>
            <cfset Variables.iLoopCounter = Variables.iLoopCounter + 1>
      </cfloop>
      <!--- Continue populating the structure so there are 10 of each key --->
      <cfloop from="#Variables.iLoopCounter#" to="7" index="i">
            <cfset x=StructInsert(stFields,"iTrafficID" & i, "0")>
            <cfset x=StructInsert(stFields,"iSourceID" & i, "0")>
            <cfset x=StructInsert(stFields,"iTraffic" & i, "0")>
            <cfset x=StructInsert(stFields,"iLease" & i, "0")>
            <cfset x=StructInsert(stFields,"iCancel" & i, "0")>
            <cfset x=StructInsert(stFields,"iReject" & i, "0")>
            <cfset x=StructInsert(stFields,"iCost" & i, "0")>
      </cfloop>
<cfelse>
      <!--- Create the structure to hold the field data --->
      <cfloop from="0" to="7" index="i">
            <cfset x=StructInsert(stFields,"iTrafficID" & i, "0")>
            <cfset x=StructInsert(stFields,"iSourceID" & i, "0")>
            <cfset x=StructInsert(stFields,"iTraffic" & i, "0")>
            <cfset x=StructInsert(stFields,"iLease" & i, "0")>
            <cfset x=StructInsert(stFields,"iCancel" & i, "0")>
            <cfset x=StructInsert(stFields,"iReject" & i, "0")>
            <cfset x=StructInsert(stFields,"iCost" & i, "0")>
      </cfloop>
</cfif>

<form action="Reports/Traffic.cfm" method="post" name="frmTraffic">
<table cellpadding="10" cellspacing="0" border="1" align="center" width="500">
<th class="navhdrrev">Traffic Source</th>
<th class="navhdrrev">Traffic</th>
<th class="navhdrrev">Leased</th>
<th class="navhdrrev">Cancelled</th>
<th class="navhdrrev">Rejected</th>
<th class="navhdrrev">Monthly Cost</th>
<th class="navhdrrev">Delete Record</th>

<!--- Loop to create the form rows --->
<cfset Variables.iLoopCounter = 0>
<cfoutput>
<cfloop from="0" to="7" index="i">
<tr>
      <td align="left" valign="top" class="navbar">
            <cfset Variables.sFieldSourceID = "iSourceID" & Variables.iLoopCounter>
            <input type="Text" name="#Variables.sFieldSourceID#" value="#Trim(NumberFormat(StructFind(stFields,Variables.sFieldSourceID),"999,999"))#" size="4">
      </td>
      <td align="center" valign="top" class="navbar">
            <cfset Variables.sFieldTraffic = "iTraffic" & Variables.iLoopCounter>
            <input type="Text" name="#Variables.sFieldTraffic#" value="#Trim(NumberFormat(StructFind(stFields,Variables.sFieldTraffic),"999,999"))#" size="4">
      </td>
      <td align="center" valign="top" class="navbar">
            <cfset Variables.sFieldLease = "iLease" & Variables.iLoopCounter>
            <input type="Text" name="#Variables.sFieldLease#" value="#Trim(NumberFormat(StructFind(stFields,Variables.sFieldLease),"999,999"))#" size="4">
      </td>
      <td align="center" valign="top" class="navbar">
            <cfset Variables.sFieldCancel = "iCancel" & Variables.iLoopCounter>
            <input type="Text" name="#Variables.sFieldCancel#" value="#Trim(NumberFormat(StructFind(stFields,Variables.sFieldCancel),"999,999"))#" size="4">
      </td>
      <td align="center" valign="top" class="navbar">
            <cfset Variables.sFieldReject = "iReject" & Variables.iLoopCounter>
            <input type="Text" name="#Variables.sFieldReject#" value="#Trim(NumberFormat(StructFind(stFields,Variables.sFieldReject),"999,999"))#" size="4">
      </td>
      <td align="center" valign="top" class="navbar">
            <cfset Variables.sFieldCost = "iCost" & Variables.iLoopCounter>
            <input type="Text" name="#Variables.sFieldCost#" value="#Trim(NumberFormat(StructFind(stFields,Variables.sFieldCost),"999,999,999.99"))#" size="15">
            <!--- Create the hidden field for the LeasingID  --->
            <cfset Variables.sFieldTrafficID = "iTrafficID" & Variables.iLoopCounter>
            <input type="Hidden" name="#Variables.sFieldTrafficID#" value="#Trim(StructFind(stFields,Variables.sFieldTrafficID))#">
      </td>
      <td align="center" valign="middle" class="navbar">
            <a href="/Reports/Traffic.cfm&TID=#Trim(StructFind(stFields,Variables.sFieldTrafficID))#&Action=Delete">
            <img src="/images/icon_delete.gif" width="20" height="20" border="0" alt="">
            </a>
      </td>
</tr>
<cfset Variables.iLoopCounter = Variables.iLoopCounter + 1>
</cfloop>
<tr>
      <td align="center" colspan="6" height="50" valign="middle">
            <input type="Submit" name="sSaveTraffic" value="Save Changes">
      </td>
</tr>
</cfoutput>
</table>
</form>

HTH
Rob
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:karaikudi
ID: 6887415
Hi Rob

Thanks for your code, but my form is something like this,

<form action="Report.cfm" method="post" name="f1">
<table border="0" cellpadding="0" cellspacing="0" width="250" align="center">
<th align="center">ITEMS</th><th align="center">QTY</th>
<cfloop index="LoopCount" from="1" to="10">
<cfoutput>    
     <tr>
          <td align="center"><input type="Text" name="item.#LoopCount#" size="20"></td>
          <td align="center"><input type="Text" name="qty.#LoopCount#" size="10"></td>
     </tr>    
     </cfoutput>
</cfloop>
     <tr>
          <td colspan="2"> &nbsp; </td>
     </tr>    
     <tr>
          <td colspan="2" align="right"><input type="submit" value="order now" name="submit"></td>
     </tr>    
</table>
</form>
0
 
LVL 2

Expert Comment

by:Cyril_H
ID: 6888224
Here's a sample code based on yours, but it's not the "easiest way" to do this. A better way should be to check fields after submittion with CF... but here it is :

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
     <title>Untitled</title>
</head>

<script language="JavaScript">
function Jltrim ( s )
{
return s.replace( /^\s*/, "" )
}

function Jrtrim ( s )
{
return s.replace( /\s*$/, "" );
}

function Jtrim ( s )
{
return Jrtrim(Jltrim(s));
}

function DoSelection(){
   
     ITM = new Array(0);
     QTY = new Array(0);
     
     for(count=1;count<=10;count=count+1) {

               if (Jtrim(eval("document.f1.item"+count+".value")) != "") {

                    ITM.push('<input type="Text" name="item'+count+'" size="20" value="'+eval("document.f1.item"+count+".value")+'">');    
                    QTY.push('<input type="Text" name="qty'+count+'" size="20" value="'+eval("document.f1.qty"+count+".value")+'">');    
               };
     };    
     
     document.write('<HTML>');
     document.write('<BODY onload="document.f2.submit();">');
     document.write('<form action="Report.cfm" method="post" name="f2">');    

     
     for(count=0;count<=(ITM.length-1);count=count+1) {
              document.write(ITM[count]);
               document.write(QTY[count]);
               document.write("<BR>");
     };    
     
     document.write('</form>');
     document.write('</BODY>');
     document.write('</HTML>');              
     document.close();
     
}
</script>

<body>

<form action="Report.cfm" method="post" name="f1">
<table border="0" cellpadding="0" cellspacing="0" width="250" align="center">
<th align="center">ITEMS</th><th align="center">QTY</th>
<cfloop index="LoopCount" from="1" to="10">
<cfoutput>    
    <tr>
         <td align="center"><input type="Text" name="item#LoopCount#" size="20"></td>
         <td align="center"><input type="Text" name="qty#LoopCount#" size="10"></td>
    </tr>    
    </cfoutput>
</cfloop>
    <tr>
         <td colspan="2"> &nbsp; </td>
    </tr>    
    <tr>
         <td colspan="2" align="right"><input type="button" value="order now" name="submit" onclick="DoSelection();"></td>
    </tr>    
</table>
</form>


</body>
</html>
0
 

Author Comment

by:karaikudi
ID: 6893737
Hi Cyril

1.I'm getting error in your code, 2.how to insert into the database?

can anyone help...
0
 
LVL 2

Expert Comment

by:Cyril_H
ID: 6893781
1. what kind of error ?

2. If your problem is just to insert only filled imputs into the dB, here's a simple code :

<!---- form1.cfm --->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
     <title>Untitled</title>
</head>

<body>

<form action="Report.cfm" method="post" name="f1">
<table border="0" cellpadding="0" cellspacing="0" width="250" align="center">
<th align="center">ITEMS</th><th align="center">QTY</th>
<cfloop index="LoopCount" from="1" to="10">
<cfoutput>    
    <tr>
         <td align="center"><input type="Text" name="item#LoopCount#" size="20"></td>
         <td align="center"><input type="Text" name="qty#LoopCount#" size="10"></td>
    </tr>    
    </cfoutput>
</cfloop>
    <tr>
         <td colspan="2"> &nbsp; </td>
    </tr>    
    <tr>
         <td colspan="2" align="right"><input type="submit" value="order now" name="submit"></td>
    </tr>    
</table>
</form>

</body>
</html>


<!--- report.cfm --->
<CFLOOP list="#Form.fieldnames#" index="idx">

     <CFIF (Left(idx,4) IS "ITEM") AND (Trim(Evaluate("Form."&idx)) IS NOT "")>
     
          <CFQUERY datasource="#MyDSN#">
          INSERT INTO MyTABLE(ITEMS,
                                  QUANTITY)
               VALUES ('#Evaluate("Form."&idx)#',
                         <CFIF IsNumeric(Right(idx,2))>
                          '#Evaluate("Form.QTY"&Right(idx,2))#')
                    <CFELSE>
                          '#Evaluate("Form.QTY"&Right(idx,1))#')
                    </CFIF>            
          </CFQUERY>
         
     </CFIF>

</CFLOOP>
0
 

Author Comment

by:karaikudi
ID: 6898469
Hi Cyril

just to insert only filled inputs into the dB, works fine.
thank you very much for your help.

In the previous code, i got the validation(JavaScript) error, - object doesn't support this property or method..
If you could solve the error, it will be helpful to me.

Thank you.
0
 
LVL 2

Accepted Solution

by:
Cyril_H earned 50 total points
ID: 6898621
Ok, maybe you have to proceed step by step for debugging Javascript. Try to comment/uncomment some lines in the function DoSelection() to see where's exactly the problem.

1st Step, try :
---------------

<script language="JavaScript">
function Jltrim ( s )
{
return s.replace( /^\s*/, "" )
}

function Jrtrim ( s )
{
return s.replace( /\s*$/, "" );
}

function Jtrim ( s )
{
return Jrtrim(Jltrim(s));
}

function DoSelection(){
   
    for(count=1;count<=10;count=count+1) {

      alert(Jtrim(eval("document.f1.item"+count+".value")));

   }    
   
}
</script>

Let's see if it works

2nd step :
----------

<script language="JavaScript">

function DoSelection(){
   
    ITM = new Array(0);
    QTY = new Array(0);
   
   ITM.push('add_element');
   
   QTY.push('add_element');
   
   
}
</script>

Let me know if these 2 things work
0
 
LVL 35

Expert Comment

by:mrichmon
ID: 10784598
No comment has been added lately, so it's time to clean up this question.
I will leave the following recommendation in the Cleanup topic area:

Accept Cyril_H

Please leave any comments here within the next four days.

mrichmon
EE Cleanup Volunteer
0

Featured Post

Free NetCrunch network monitor licenses!

Only on Experts-Exchange: Sign-up for a free-trial and we'll send you your permanent license!

Here is what you get: 30 Nodes | Unlimited Sensors | No Time Restrictions | Absolutely FREE!

Act now. This offer ends July 14, 2017.

Question has a verified solution.

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

Introduction This article explores the design of a cache system that can improve the performance of a web site or web application.  The assumption is that the web site has many more “read” operations than “write” operations (this is commonly the ca…
When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

724 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