PDA

View Full Version : trouble finding selected variable


swissbeets
07-08-2008, 08:05 PM
i found this code to make a drop down menu and need to implement it into the selected size of myproduct and then have it be POSTed then i need to get it on the other end and display it

but i cannot find out which variable is the one i want to post


function showOptionsDrop($array, $active, $echo=true){
$string = '';

foreach($array as $k => $v){
$s = ($active == $k)? ' selected="selected"' : '';
$string .= '<option value="'.$k.'"'.$s.'>'.$v.'</option>'."\n";
}

if($echo) echo $string;
else return $string;
}//i feel like i have tried every single variable in the function

here is what i have now

<?php
while($row = mysql_fetch_array($product_set)){



echo "<p>";
echo $row['product_name']."<br />";
echo "$".$row['product_price']."<br />"."<br />";
show_picture($row);
echo "<br/>";


echo "Size: "; ?><p>
<select name="size">
<?php
$size = array('Select','S','M','L','XL');
showOptionsDrop($size);
?>
</select>
</p> <?php


echo "<a href=\"shoppingcart.php?prod=".urlencode($row['product_id'])."?&size=['$size']"."?&action=add\">Add to Shopping Cart"."</a>";
}



?>

davidj
07-08-2008, 09:29 PM
function showOptionsDrop($array, $active, $echo=true){

$string = '';

foreach($array as $k => $v){
$s = ($active == $k)? ' selected="selected"' : '';
$string .= '<option value="'.$k.'"'.$s.'>'.$v.'</option>'."\n";
}

if($echo) echo $string;
else return $string;
}//


as above

you need to pass in an array with your option values

$k = keys
$v = values

the $k is the option for the dropdown

swissbeets
07-08-2008, 09:36 PM
i dont know if you answered my posted question, sorry i should have been more specific, i already set the array as

<p>
<select name="size">
<?php
$size = array('Select','S','M','L','XL');
showOptionsDrop($size);
?>
</select>
</p>


now i need to get it into my URL which is currently
echo "<a href=\"shoppingcart.php?prod=".urlencode($row['product_id'])."?&size=['$size']"."?&action=add\">Add to Shopping Cart"."</a>";

but i am doing something wrong with the size and it is not working

sorry if you already answered my question and i just dont understand

davidj
07-08-2008, 09:46 PM
you need an associative array

$size = array('na'=>'Select','small'=>'S','medium'=>'M','large'=>'L','xlarge'=>'XL');

the keys become the values and the values become the labels

davidj
07-08-2008, 09:48 PM
also

&size=['$size']

this is wrong

whats with the square brackets

swissbeets
07-08-2008, 10:06 PM
I thought since it was an array i had to do that but i got it mixed up with a string i think

so in the url how do i tell the browser to display the selected value so that i can get it in the shopping cart?

since $size is an array is there a way ?

davidj
07-08-2008, 10:12 PM
you are using an array in the <select construct

after the function has turned your array into <options its no longer an array just a simple dropdown

treat it as such

you need to catch the option value in a $_POST then constitute the URL query string dynamically