...

View Full Version : How to access an <option> number



Local Hero
02-05-2007, 07:29 AM
If I know the text of an option (or the value) how can I get the option position/number?

For example:

<select name="numfontype">
<option value="AARVC___.TTF">Aardvark Cafe</option>
<option value="Abduction.ttf">Abduction</option>
<option value="Adventure.ttf">Adventure</option>
<option value="air_mitalic.ttf">Air Millhouse</option>
</select>
If I know 'Adventure' how can I return a 2 ???

I can obtain the text and the values, but no positions. Please help!!!

mmarif4u
02-05-2007, 07:42 AM
I cant understand ur post correctly,,
but if u want that the drop down menu appear number wise then u
can change ur options to resolve the series...

Mhtml
02-05-2007, 12:39 PM
Yeah, you'll have to sort of explain a bit more of what you're trying to accomplish.

Is this even a server side problem? Or are you looking to use JavaScript and the DOM (or PHP and the DOM if you're generating this source at run-time) to get the index of a selected option? You're not quite clear, we could give a number of answers but I'm sure we'd rather give the correct one.

raf
02-05-2007, 12:56 PM
i can only speculate, but the sollution might be using an array to build the dropdown + to get the ordernumber (--> the key of the array).

Local Hero
02-05-2007, 01:06 PM
I'm Sorry, it's a tricky situation.
I have shopping cart software on site A and I want to add stuff from site B. Site B lets the user input options into fields to dynamically generate their visual product. The values from those fields are used for the product image and the PHP values are passed to hidden fields to send to the cart. For example:

$numfontype = $_POST['numfontype']
<select name="numfontype">
<option value="AARVC___.TTF">Aardvark Cafe</option>
<option value="Abduction.ttf">Abduction</option>
<option value="Adventure.ttf">Adventure</option>
<option value="air_mitalic.ttf">Air Millhouse</option>
</select>
The values need to be the font filename, but when I send the option to the cart it needs a number. The options in "numfontype" are in the same order as in the cart, but the cart starts at 433 and goes to 500. I know there is a way to save me hours of useless coding here. I'm just fuzzy/new to PHP.
My final code would need to be:

<input type="hidden" id="po104" name="product_options[104]" value"<?php echo($numfontype[$position# + 433]?>">
I don't need to change the dropdown for the user, I just need to be able to find the number position of an option when I know the value or name. I'm not sure if I'm still being confusing?? My situation could also work if I can put the option position# in a PHP variable, but the value needs to stay as the font filename. Maybe I could have something like:

$numfontype= $_POST['numfontype']
echo $numfontype[$_POST['numfontype']]['name'];
echo $numfontype[$_POST['numfontype']]['name']['POSITION#???'];
Please let me know if it doesn't make sense. I'm not sure what I'm trying to say.
THANKS THOUGH!!

raf
02-05-2007, 01:20 PM
so when you build the dropdown, why don't you just set it to

<select name="numfontype">
<option value="434">Aardvark Cafe</option>
<option value="435">Abduction</option>
<option value="436">Adventure</option>
<option value="437">Air Millhouse</option>
</select>

Local Hero
02-05-2007, 02:30 PM
I need that value to generate an image of the product. I know this is getting messy. Let me try and ask this question:

If I have This:

$numfontype = $_POST['numfontype']
<select name="numfontype">
<option value="AARVC___.TTF">Aardvark Cafe</option>
<option value="Abduction.ttf">Abduction</option>
<option value="Adventure.ttf">Adventure</option>
<option value="air_mitalic.ttf">Air Millhouse</option>
</select>
How do I finish this?


echo $numfontype; #Adventure.ttf
echo $numfontype[$_POST['numfontype']]['name']; #Adventure
$fontname = $numfontype[$_POST['numfontype']]['name'];
echo $numfontype[$_POST['numfontype']]['$fontname']['POSITION#???']; #2, the position of the option

I want to use PHP but maybe this would be better done in Javascript?

Saz
04-02-2008, 09:17 PM
Anyone know if raf is still around ?

aedrin
04-02-2008, 10:35 PM
And you had to dredge up this old article to ask this?

Andrew Johnson
04-02-2008, 10:39 PM
I don't know why this is in PHP if you would use this at run time, but in JS here's a function that will return the number you're looking for if you pass it the string:



function get_position(option_name)
for (var i=0;i<=document.getElementById("selectname").options.length;i++)
{
if (document.getElementById("selectname").options[i].value == option_name)
{
return (i + 1)
}
}
}

aedrin
04-02-2008, 10:42 PM
Did you read my response? This is an old article, I don't think you'll help anyone by solving the problem.

kbluhm
04-03-2008, 12:27 AM
If you use PHP, you'll have to pre-define the options that exist in the select, then iterate them and look for that value:


$options = array(
'AARVC___.TTF' => 'Aardvark Cafe',
'Abduction.ttf' => 'Abduction',
'Adventure.ttf' => 'Adventure',
'air_mitalic.ttf' => 'Air Millhouse',
);

$selected = isset( $_POST['numfontype'] ) ? $_POST['numfontype'] : NULL;

$index = 0;

foreach ( $options as $value => $text )
{
if ( $value === $selected )
{
break;
}
$index++;
}

echo $index;


Or, if you end up using JavaScript, have a look at selectedIndex,

o0O0o.o0O0o
04-03-2008, 12:44 AM
And you had to dredge up this old article to ask this?

ha ha :D

i really not able to unserstand why did SAZ used this article to find his loving mate raf



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum