Solved

Arrays

Posted on 2002-03-19
11
155 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
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
 

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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

Suggested Solutions

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…
If you don't have the right permissions set for your WordPress location in IIS, you won't be able to perform automatic updates. Here's how to fix the problem.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 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

9 Experts available now in Live!

Get 1:1 Help Now