Forum Moderators: open

Message Too Old, No Replies

Dynamic URL built from Arrays

Dynamic URL built from Arrays

         

YankeeRoo

1:07 pm on May 17, 2009 (gmt 0)

10+ Year Member



Hi

Having a problem ascertaining the correct value for the first column of the array in the sample below showing up in the action commands at the bottom of the script.

Any idea what I need to put in place of the X in the "window.location"? I've worked out the 2nd through 4th position, but have no clue how to get the 1st position to be imbedded into the URL. For example if I click on the Analytical option within the METERS menu list, how do I get the 657 value to be included?

Thanks in advance
Tony

categorys=new Array(null
,new Array("999","TYPE","BRAND","PRODUCT")
,new Array("657","device","METERS","Analytical")
,new Array("484","device","METERS","AirFlow")
);
//level variable
var level=0;

function loadSubGroups() {
for (i = document.form.product.options.length; i >= 0; i--) {
document.form.product.options[i] = null;
}
var brand_name = document.form.brand.value;
var product_j=0
var product_array =new Array();

for(var i=1;i<categorys.length;i++){
if(categorys[i][2]==brand_name){
if(categorys[i]== 'device'){
//Search if the item exists already!
product_flag = true;
for(var m=0;m<product_array.length;m++){
if(product_array[m][0] == categorys[i][3]){
product_flag = false;
}
}
if(product_flag){
product_array[product_j]=new Array(categorys[i][3],categorys[i][3]);
product_j++;
}
}
}
}
product_array.sort();
for( var i=0;i<product_array.length;i++){

document.form.product.options[i]=new Option(product_array[i][0]);
document.form.product.options[i].value=product_array[i][1];
}
}

function viewProduct(type){
if(type == 'product'){
window.location="http://www.yankeeroo.com/store/home.php?cat="+document.form.X.value;
}
}

[1][edited by: YankeeRoo at 1:11 pm (utc) on May 17, 2009]

Fotiman

2:26 pm on May 18, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Welcome to WebmasterWorld! I would start by removing all of those "new Array()". Instead, use the literal array notation (it avoids the overhead of a new and, IMHO, is easier to follow).


categorys = [
null
,["999","TYPE","BRAND","PRODUCT"]
,["657","device","METERS","Analytical"]
,["484","device","METERS","AirFlow"]
];

To access the "657" value:
categorys[2][0]

In other words, categorys[2] gets the array ("657","device","METERS","Analytical"), and then [0] gets the first item in that array.

I think you'll want to correct this code:

 
if (categorys[i] == 'device') {

Should be:

if (categorys[i][1] == 'device') {

[edited by: Fotiman at 2:29 pm (utc) on May 18, 2009]

YankeeRoo

2:40 am on May 19, 2009 (gmt 0)

10+ Year Member



Ok, the code now looks like below:

What do I need to replace the '+document.form.battery.value;" with in order to make this work?

I'm expecting the URL of http://www.example.com/store/home.php?cat=657 to be passed through

Thanks mate

categorys = [
null
,["657","device","METERS","Analytical Meters"]
,["484","device","METERS","Air Flow Meters"]
,["580","device","METERS","Calibrators"]
];
//level variable
var level=0;

function loadSubGroups() {

for (i = document.form.product.options.length; i >= 0; i--) {
document.form.product.options[i] = null;
}
var brand_name = document.form.brand.value;
var product_j=0
var product_array =[];

for(var i=1;i<categorys.length;i++){
if(categorys[i][2]==brand_name){
if(categorys[i]== 'device'){
//Search if the item exists already!
product_flag = true;
for(var m=0;m<product_array.length;m++){
if(product_array[m][0] == categorys[i][3]){
product_flag = false;
}

}
if(product_flag){
product_array[product_j]=[categorys[i][3],categorys[i][3]);
product_j++;
}
}
}
}
product_array.sort();
for( var i=0;i<product_array.length;i++){

document.form.product.options[i]=new Option(product_array[i][0]);
document.form.product.options[i].value=product_array[i][1];
}

}

//battery product brand category_id
function viewProduct(type){
if(type == 'product'){
window.location="http://www.example.com/store/home.php?cat="+document.form.battery.value;
}
}

[1][edited by: whoisgregg at 1:03 pm (utc) on May 19, 2009]
[edit reason] Whoops, no URLs please. Use "example.com" :) [/edit]

YankeeRoo

9:27 am on May 20, 2009 (gmt 0)

10+ Year Member



It has to be something in either the JS or the HTML and I aint sure which.

Can someone tell me in laymans terms exactly what I need to do? (Im not a web programmer)

Heres the HTML.......
<form name=form>
<table width=100%>
<tr>
<td valign=top width="25">
&nbsp;
</td>
<td valign=top width="175">
<div align=left><b>Category</b></div>
<select name=brand onClick="loadSubGroups();" size=9 style="font-family: verdana; font-size: 11px">
<!-- Enter in selections and values for the BRAND colum -->
<option value="ACCESSORIES">Accessories</option>
<option value="ASSEMBLY">Assembly &amp; Parts</option>
<option value="BATTERY">Batteries</option>
<option value="CHARGERS">Battery Chargers</option>
<option value="METERS">Measurement Meters</option>
<option value="POWER">Power Products</option>
<option value="TORCH">Torches</option>
</select>
</td>
<td width="200">
<div align=left><b>Sub Category</b></div>
<select name=product onClick="viewProduct('product');" size=9 style="font-family: verdana; font-size: 11px"></select>
</td>
<td>
&nbsp;
</td>
</tr>
</table>
</form>