PDA

View Full Version : Need help with a FOR LOOP


Coolvirus
08-29-2002, 04:05 PM
I have a form with two sections, BUY and SELL. Let's say each section contained 5 rows and each row has a customer field. buy_customer1, buy_customer2, buy_customer3 and so on for the BUY section and sell_customer1, sell_customer2 and so on for the SELL section.

On submission, I want to compare each buy_customer field against each sell_customer field to find out if any of them are equal.

I'm thinking an outer and an inner FOR LOOP would do the trick. My problem is that the number of rows for each section can vary so I can't hard-code the integer at the end of buy_customerX and sell_customerX.

What would the FOR LOOP look like for my comparison?

Thanks in advance.

Tanker
08-29-2002, 04:37 PM
Someone else here can probably write a better routine, but it sounded like good practice for me. :)

Hope this helps.


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

<html>
<head>
<title>Compare checkboxes.</title>
<script language="JavaScript1.2">
<!--
function compLists(){
frm = document.compare
if(frm.buy.length != frm.sell.length){
alert("List lengths do not match,\r\nUnable to compare.")
}else{
for(i=0;i<frm.buy.length;i++){
if(frm['buy'][i].checked == true){
if(frm['sell'][i].checked == true){
alert(frm['buy'][i].value+" and "+frm['sell'][i].value+" checked")
}
}
}
}
}

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

<body>
<table>
<form action="" name="compare" id="compare">
<tr>
<td>Buy</td>
<td>Sell</td>
</tr>
<tr>
<td>
<input type="checkbox" name="buy" value="buy_1">Buy 1<br>
<input type="checkbox" name="buy" value="buy_2">Buy 2<br>
<input type="checkbox" name="buy" value="buy_3">Buy 3<br>
<input type="checkbox" name="buy" value="buy_4">Buy 4<br>
<input type="checkbox" name="buy" value="buy_5">Buy 5<br>
</td>
<td>
<input type="checkbox" name="sell" value="sell_1">Sell 1<br>
<input type="checkbox" name="sell" value="sell_2">Sell 2<br>
<input type="checkbox" name="sell" value="sell_3">Sell 3<br>
<input type="checkbox" name="sell" value="sell_4">Sell 4<br>
<input type="checkbox" name="sell" value="sell_5">Sell 5<br>
</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="button" value="Compare" onClick="compLists();"></td>
</tr>
</form>
</table>
</body>
</html>

Coolvirus
08-29-2002, 04:49 PM
Tanker, thanks for the reply.

I think I didn't explain the situation correctly. The fields involved are text fields, not checkboxes. The name for each field is buy_customerX and sell_customerX where X is the number of buy pieces and sell pieces that the user chose on a previous screen.

This is what I came up with although I haven't tested this yet (we're having some system problems at the moment). Would this work?


for( b = 1; b <= buy_rows; b++ )
{
eval("buy_cust = document.TradeTicket_form.buy_customer" + b + ".value");

for( s = 1; s <= sell_rows; s++ )
{
eval("sell_cust = document.TradeTicket_form.sell_customer" + b + ".value");

if( buy_cust == sell_cust )
alert("Warning: Same customer buying and selling");

}
}

PS: My indentation didn't come out the way I typed it in. Blanks are getting ignored.

ShriekForth
08-29-2002, 05:03 PM
I tried this, and came up with one problem. Moszilla did not want to contine when you asked for a field that did not exist, i.e. buy_customer5 when 4 was the last. So I created a function to get the number of items, it checks the type to make sure that an object exists. I suppose it could go further and make sure that it is the corrcet type as well. Ii could be placed around the main loop, but this seemed easier to read.
I did not know what you were doing with the results, so I stored them in an array as an array, the matches, and the value...


<script type="text/javascript" language="JavaScript">
matching = new Array();
matches = 0;

function checkForm(){
items = getItems();
for (x = 1; x <= items; x++){
eval('bc = document.temp.buy_customer'+ x + '.value');
if (bc != ""){
for (y = 1; y<= items; y++){
if(eval('bc == document.temp.sell_customer' + y +'.value')){
matching[matches] = new Array(x,y,bc);
matches ++;
}
} //end Y
}
} //end X
hold = "";
for (x = 0; x < matches; x++){
hold = hold + "buy_customer"+ matching[x][0]+ " = sell_customer"+matching[x][1] + " WITH '" + matching[x][2] + "' \n";
}
alert(hold);
}
function getItems(){
notDone = true;
items = 0;
i = 1;
while (notDone){
if (eval(' typeof document.temp.buy_customer' + i +' == "object"')){
items++;
}
else{
notDone = false;
}
i++;
}
return items;
}


</script>

<form name="temp">
<input type="text" name="buy_customer1" value="ShriekForth1">&nbsp;<input type="text" name="sell_customer1"><br>
<input type="text" name="buy_customer2">&nbsp;<input type="text" name="sell_customer2"><br>
<input type="text" name="buy_customer3">&nbsp;<input type="text" name="sell_customer3" value="ShriekForth1"><br>
<input type="text" name="buy_customer4">&nbsp;<input type="text" name="sell_customer4" value="ShriekForth"><br>
<a href="javascript: checkForm()">check</a>


That may get you on your way anyway.

ShriekForth