UPDATE : Help Required to Populate search results - Array Search

C

Craig Keightley

I have the following array:
function showCPUs(){
//name brandID dualCore dualProcessor
var cpuItem=['Pentium 4 HT Northwood','2','N','N'];
var cpuItem=['Pentium D 820','2','Y','N'];
var cpuItem=['Pentium D 830','2','Y','N'];
var cpuItem=['Pentium D 840','2','Y','N'];
var cpuItem=['Xeon','2','N','Y'];
var cpuItem=['Xeon','2','N','Y'];
var cpuItem=['Xeon','2','N','Y'];
var cpuItem=['Xeon Dempsey','2','N','Y'];
var cpuItem=['Athlon 64 3000','1','N','N'];
var cpuItem=['Athlon 64 3200','1','N','N'];
var cpuItem=['Athlon 64 3500','1','N','N'];
var cpuItem=['Athlon 64 4000','1','N','N'];
var cpuItem=['Athlon 64 X2 4200','1','Y','N'];
var cpuItem=['Athlon 64 X2 4400','1','Y','N'];
var cpuItem=['Athlon 64 X2 4600','1','Y','N'];
var cpuItem=['Athlon 64 X2 4800','1','Y','N'];
var cpuItem=['Opteron 242','1','N','Y'];
var cpuItem=['Opteron 244','1','N','Y'];
var cpuItem=['Opteron 246','1','N','Y'];
var cpuItem=['Opteron 252','1','Y','Y'];
}
</script>

and the following hidden fields that are set by drop down menus
<input name="cpuBrandID" type="text" id="cpuBrandID">
<input name="dualCoreID" type="text" id="dualCoreID">
<input name="dualProcessorID" type="text" id="dualProcessorID">

How can I search against the hidden values to generate a list of items from
my array?

eg
cpuBrandID = 2
dualCoreID = Y
dualProcessorID = N

will populate the page with the following processors from the array:
Pentium D 820
Pentium D 830
Pentium D 840

Many Thanks

Craig
 
C

Craig Keightley

After a tweak, I now have the following array:

var cpuItem = new Array(
['Pentium 4 HT Northwood','2','N','N','10.00','p4.jpg'],
['Pentium D 820','2','Y','N','10.00','pd.jpg'],
['Pentium D 830','2','Y','N','20.00','pd.jpg'],
['Pentium D 840','2','Y','N','40.00','pd.jpg'],
['Xeon','2','N','Y','50.00','xeon.jpg'],
['Xeon','2','N','Y','60.00','xeon.jpg'],
['Xeon','2','N','Y','70.00','xeon.jpg'],
['Xeon Dempsey','2','N','Y','140.00','xeon.jpg'],
['Athlon 64 3000','1','N','N','0.00','a64.jpg'],
['Athlon 64 3200','1','N','N','5.00','a64.jpg'],
['Athlon 64 3500','1','N','N','15.00','a64.jpg'],
['Athlon 64 4000','1','N','N','25.00','a64.jpg'],
['Athlon 64 X2 4200','1','Y','N','35.00','a64X2.jpg'],
['Athlon 64 X2 4400','1','Y','N','40.00','a64X2.jpg'],
['Athlon 64 X2 4600','1','Y','N','60.00','a64X2.jpg'],
['Athlon 64 X2 4800','1','Y','N','70.00','a64X2.jpg'],
['Opteron 242','1','N','Y','20.00','opteron.jpg'],
['Opteron 244','1','N','Y','30.00','opteron.jpg'],
['Opteron 246','1','N','Y','45.00','opteron.jpg'],
['Opteron 252','1','Y','Y','120.00','opteron.jpg']
);

is searching an array possible?
 
M

McKirahan

Craig Keightley said:
I have the following array:
function showCPUs(){
//name brandID dualCore dualProcessor
var cpuItem=['Pentium 4 HT Northwood','2','N','N'];
var cpuItem=['Pentium D 820','2','Y','N'];
var cpuItem=['Pentium D 830','2','Y','N'];
var cpuItem=['Pentium D 840','2','Y','N'];
var cpuItem=['Xeon','2','N','Y'];
var cpuItem=['Xeon','2','N','Y'];
var cpuItem=['Xeon','2','N','Y'];
var cpuItem=['Xeon Dempsey','2','N','Y'];
var cpuItem=['Athlon 64 3000','1','N','N'];
var cpuItem=['Athlon 64 3200','1','N','N'];
var cpuItem=['Athlon 64 3500','1','N','N'];
var cpuItem=['Athlon 64 4000','1','N','N'];
var cpuItem=['Athlon 64 X2 4200','1','Y','N'];
var cpuItem=['Athlon 64 X2 4400','1','Y','N'];
var cpuItem=['Athlon 64 X2 4600','1','Y','N'];
var cpuItem=['Athlon 64 X2 4800','1','Y','N'];
var cpuItem=['Opteron 242','1','N','Y'];
var cpuItem=['Opteron 244','1','N','Y'];
var cpuItem=['Opteron 246','1','N','Y'];
var cpuItem=['Opteron 252','1','Y','Y'];
}
</script>

and the following hidden fields that are set by drop down menus
<input name="cpuBrandID" type="text" id="cpuBrandID">
<input name="dualCoreID" type="text" id="dualCoreID">
<input name="dualProcessorID" type="text" id="dualProcessorID">

How can I search against the hidden values to generate a list of items from
my array?

eg
cpuBrandID = 2
dualCoreID = Y
dualProcessorID = N

will populate the page with the following processors from the array:
Pentium D 820
Pentium D 830
Pentium D 840

Many Thanks

Craig

1) "var cpuItem" is declared multiple times.

2) "type=text" is not a hidden field.


Is this what you're looking for? Watch for word-wrap.

<html>
<head>
<title>cpuItem.htm</title>
<script type="text/javascript">
var s = 0;
var cpuItem = new Array();
cpuItem[s++] = ['cpuName','cpuBrand','dualCore','dualProcessor'];
cpuItem[s++] = ['Pentium 4 HT Northwood','2','N','N'];
cpuItem[s++] = ['Pentium D 820','2','Y','N'];
cpuItem[s++] = ['Pentium D 830','2','Y','N'];
cpuItem[s++] = ['Pentium D 840','2','Y','N'];
cpuItem[s++] = ['Xeon','2','N','Y'];
cpuItem[s++] = ['Xeon','2','N','Y'];
cpuItem[s++] = ['Xeon','2','N','Y'];
cpuItem[s++] = ['Xeon Dempsey','2','N','Y'];
cpuItem[s++] = ['Athlon 64 3000','1','N','N'];
cpuItem[s++] = ['Athlon 64 3200','1','N','N'];
cpuItem[s++] = ['Athlon 64 3500','1','N','N'];
cpuItem[s++] = ['Athlon 64 4000','1','N','N'];
cpuItem[s++] = ['Athlon 64 X2 4200','1','Y','N'];
cpuItem[s++] = ['Athlon 64 X2 4400','1','Y','N'];
cpuItem[s++] = ['Athlon 64 X2 4600','1','Y','N'];
cpuItem[s++] = ['Athlon 64 X2 4800','1','Y','N'];
cpuItem[s++] = ['Opteron 242','1','N','Y'];
cpuItem[s++] = ['Opteron 244','1','N','Y'];
cpuItem[s++] = ['Opteron 246','1','N','Y'];
cpuItem[s++] = ['Opteron 252','1','Y','Y'];
function showCPUs() {
var form = document.form1;
form.cpuName.value = "";
form.dualCore.value = form.dualCore.value.toUpperCase();
form.dualProcessor.value = form.dualProcessor.value.toUpperCase();
//
if (form.cpuBrand.value == "") return;
if ("12".indexOf(form.cpuBrand.value) < 0) {
alert("Invalid CPU Brand!");
return;
}
if (form.dualCore.value == "") return;
if ("YN".indexOf(form.dualCore.value) < 0) {
alert("Invalid Dual Core!");
return;
}
if (form.dualProcessor.value == "") return;
if ("YN".indexOf(form.dualProcessor.value) < 0) {
alert("Invalid Dual Processor!");
return;
}
//
var item = ""
for (var i=1; i<cpuItem.length; i++) {
if (form.cpuBrand.value == cpuItem[1]) {
if (form.dualCore.value == cpuItem[2]) {
if (form.dualProcessor.value == cpuItem[3]) {
if (item != "") item += "\n";
item += cpuItem[0];
}
}
}
}
form.cpuName.value = item;
}
</script>
</head>
<body onload="document.form1.cpuBrand.focus()">
<form action="" method="get" name="form1">
<b> CPU Brand : </b>
<input type="text" name="cpuBrand"
size="1" maxlength="1" onchange="showCPUs()">
<br>
<b> Dual Core (Y/N) : </b>
<input type="text" name="dualCore"
size="1" maxlength="1" onchange="showCPUs()">
<br>
<b> Dual Processor (Y/N) : </b>
<input type="text" name="dualProcessor"
size="1" maxlength="1" onchange="showCPUs()">
<br><br>
<b> CPU Name(s) :</b>
<br><textarea name="cpuName" cols="30" rows="10"
readonly style="background-color:lightyellow"></textarea>
<br>
<br><input type="reset" value="Clear"
onclick="document.form1.cpuBrand.focus()">
</form>
</body>
</html>
 
C

Craig Keightley

Thank you thats what i need
I forgot to rewrite the text values to hidden as it was from my web page to
display my values

many thanks

Craig
McKirahan said:
Craig Keightley said:
I have the following array:
function showCPUs(){
//name brandID dualCore dualProcessor
var cpuItem=['Pentium 4 HT Northwood','2','N','N'];
var cpuItem=['Pentium D 820','2','Y','N'];
var cpuItem=['Pentium D 830','2','Y','N'];
var cpuItem=['Pentium D 840','2','Y','N'];
var cpuItem=['Xeon','2','N','Y'];
var cpuItem=['Xeon','2','N','Y'];
var cpuItem=['Xeon','2','N','Y'];
var cpuItem=['Xeon Dempsey','2','N','Y'];
var cpuItem=['Athlon 64 3000','1','N','N'];
var cpuItem=['Athlon 64 3200','1','N','N'];
var cpuItem=['Athlon 64 3500','1','N','N'];
var cpuItem=['Athlon 64 4000','1','N','N'];
var cpuItem=['Athlon 64 X2 4200','1','Y','N'];
var cpuItem=['Athlon 64 X2 4400','1','Y','N'];
var cpuItem=['Athlon 64 X2 4600','1','Y','N'];
var cpuItem=['Athlon 64 X2 4800','1','Y','N'];
var cpuItem=['Opteron 242','1','N','Y'];
var cpuItem=['Opteron 244','1','N','Y'];
var cpuItem=['Opteron 246','1','N','Y'];
var cpuItem=['Opteron 252','1','Y','Y'];
}
</script>

and the following hidden fields that are set by drop down menus
<input name="cpuBrandID" type="text" id="cpuBrandID">
<input name="dualCoreID" type="text" id="dualCoreID">
<input name="dualProcessorID" type="text" id="dualProcessorID">

How can I search against the hidden values to generate a list of items from
my array?

eg
cpuBrandID = 2
dualCoreID = Y
dualProcessorID = N

will populate the page with the following processors from the array:
Pentium D 820
Pentium D 830
Pentium D 840

Many Thanks

Craig

1) "var cpuItem" is declared multiple times.

2) "type=text" is not a hidden field.


Is this what you're looking for? Watch for word-wrap.

<html>
<head>
<title>cpuItem.htm</title>
<script type="text/javascript">
var s = 0;
var cpuItem = new Array();
cpuItem[s++] = ['cpuName','cpuBrand','dualCore','dualProcessor'];
cpuItem[s++] = ['Pentium 4 HT Northwood','2','N','N'];
cpuItem[s++] = ['Pentium D 820','2','Y','N'];
cpuItem[s++] = ['Pentium D 830','2','Y','N'];
cpuItem[s++] = ['Pentium D 840','2','Y','N'];
cpuItem[s++] = ['Xeon','2','N','Y'];
cpuItem[s++] = ['Xeon','2','N','Y'];
cpuItem[s++] = ['Xeon','2','N','Y'];
cpuItem[s++] = ['Xeon Dempsey','2','N','Y'];
cpuItem[s++] = ['Athlon 64 3000','1','N','N'];
cpuItem[s++] = ['Athlon 64 3200','1','N','N'];
cpuItem[s++] = ['Athlon 64 3500','1','N','N'];
cpuItem[s++] = ['Athlon 64 4000','1','N','N'];
cpuItem[s++] = ['Athlon 64 X2 4200','1','Y','N'];
cpuItem[s++] = ['Athlon 64 X2 4400','1','Y','N'];
cpuItem[s++] = ['Athlon 64 X2 4600','1','Y','N'];
cpuItem[s++] = ['Athlon 64 X2 4800','1','Y','N'];
cpuItem[s++] = ['Opteron 242','1','N','Y'];
cpuItem[s++] = ['Opteron 244','1','N','Y'];
cpuItem[s++] = ['Opteron 246','1','N','Y'];
cpuItem[s++] = ['Opteron 252','1','Y','Y'];
function showCPUs() {
var form = document.form1;
form.cpuName.value = "";
form.dualCore.value = form.dualCore.value.toUpperCase();
form.dualProcessor.value = form.dualProcessor.value.toUpperCase();
//
if (form.cpuBrand.value == "") return;
if ("12".indexOf(form.cpuBrand.value) < 0) {
alert("Invalid CPU Brand!");
return;
}
if (form.dualCore.value == "") return;
if ("YN".indexOf(form.dualCore.value) < 0) {
alert("Invalid Dual Core!");
return;
}
if (form.dualProcessor.value == "") return;
if ("YN".indexOf(form.dualProcessor.value) < 0) {
alert("Invalid Dual Processor!");
return;
}
//
var item = ""
for (var i=1; i<cpuItem.length; i++) {
if (form.cpuBrand.value == cpuItem[1]) {
if (form.dualCore.value == cpuItem[2]) {
if (form.dualProcessor.value == cpuItem[3]) {
if (item != "") item += "\n";
item += cpuItem[0];
}
}
}
}
form.cpuName.value = item;
}
</script>
</head>
<body onload="document.form1.cpuBrand.focus()">
<form action="" method="get" name="form1">
<b> CPU Brand : </b>
<input type="text" name="cpuBrand"
size="1" maxlength="1" onchange="showCPUs()">
<br>
<b> Dual Core (Y/N) : </b>
<input type="text" name="dualCore"
size="1" maxlength="1" onchange="showCPUs()">
<br>
<b> Dual Processor (Y/N) : </b>
<input type="text" name="dualProcessor"
size="1" maxlength="1" onchange="showCPUs()">
<br><br>
<b> CPU Name(s) :</b>
<br><textarea name="cpuName" cols="30" rows="10"
readonly style="background-color:lightyellow"></textarea>
<br>
<br><input type="reset" value="Clear"
onclick="document.form1.cpuBrand.focus()">
</form>
</body>
</html>
 
M

McKirahan

Craig Keightley said:
Thank you thats what i need
I forgot to rewrite the text values to hidden as it was from my web page to
display my values

many thanks

Craig

[snip]

Your welcome.

BTW, in my code
<form action="" method="get" name="form1">
can be changed to
<form name="form1">
as the form is never submitted.

Of course, you'll have something else.
 
M

Mick White

Craig said:
After a tweak, I now have the following array:

var cpuItem = new Array(
['Pentium 4 HT Northwood','2','N','N','10.00','p4.jpg'],
['Pentium D 820','2','Y','N','10.00','pd.jpg'],
['Pentium D 830','2','Y','N','20.00','pd.jpg'],
['Pentium D 840','2','Y','N','40.00','pd.jpg'], ...
);

is searching an array possible?

Yes it is.

Loop through cpuItem, then loop through each individual array.
But what do you want to acheive? All instances of the search term?
And what if the search term is "Pentium" or "penteum"(spelling)? I would
create a separate entry for the model ("Pentium", for instance) and
model designation ("4 HT Northwood")

http://www.mickweb.com/javascript/forms/databases/lookup.html

Written some time ago, today I would use a database for this type of search.
Mick




Mick
 
L

Lasse Reichstein Nielsen

McKirahan said:
BTW, in my code
<form action="" method="get" name="form1">
can be changed to
<form name="form1">
as the form is never submitted.

Not while remaining valid HTML. The "action" attribute is required
on form elements.

/L
 
L

Lasse Reichstein Nielsen

Craig Keightley said:
After a tweak, I now have the following array:

var cpuItem = new Array(
['Pentium 4 HT Northwood','2','N','N','10.00','p4.jpg'],
['Pentium D 820','2','Y','N','10.00','pd.jpg'],
['Pentium D 830','2','Y','N','20.00','pd.jpg'], ....
['Opteron 246','1','N','Y','45.00','opteron.jpg'],
['Opteron 252','1','Y','Y','120.00','opteron.jpg']
);

is searching an array possible?

Sure. Run through the array one element at a time.
However, it might not be the smartest way to do it. If you
always want to search for the name, i.e., the first element
of the array, you might just use that as a property of an
object instead:

var cpuItem = {
'Pentium 4 HT Northwood' : ['2','N','N','10.00','p4.jpg'],
'Pentium D 820' : ['2','Y','N','10.00','pd.jpg'],
'Pentium D 830' : ['2','Y','N','20.00','pd.jpg'],
....
'Opteron 246' : ['1','N','Y','45.00','opteron.jpg'],
'Opteron 252' : ['1','Y','Y','120.00','opteron.jpg']
}

function findCpuData(name) {
return cpuItem[name];
}

/L 'and please don't top post'
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top