Hello all
Hope your all well.
I have made(found and edited and such) this code that creates a drop down menu from an array which works great.
// drop down menu function
function generate_drop_menu($name, $options, $default=""){
if (!empty($options)){
$html = '<dl><select name="'.$name.'" id="'.$name.'">';
foreach ($options as $value => $label){
$html .='<option ';
if ($value == $default){
$html .='selected ';
}
// $label[] array numbered
$html .='value="'.$value.'" >'.$label[1].'</option>';
}
$html .='</select></dl>';
return $html;
}else{
$error = "Data supplied is not valid<br />";
return $error;
}
}
//call the function
echo generate_drop_menu('product', $cms_product, '<br />', '');
This code works great with just an array but i’m pulling data out of a database and creating a multidimensional array.
How can i edit this code to create a drop down menu so it can be used and used again no matter what data i put into it. Without getting a list of Array Array Array.
note: This data is being pulled out as an associative array.
This is the array i’m using.
Array
(
[0] => Array
(
[product_id] => 1
[product_name] => chairs
)
[1] => Array
(
[product_id] => 2
[product_name] => sofa
)
[2] => Array
(
[product_id] => 3
[product_name] => tables
)
[3] => Array
(
[product_id] => 4
[product_name] => barstools
)
[4] => Array
(
[product_id] => 5
[product_name] => benches
)
[5] => Array
(
[product_id] => 6
[product_name] => lighting
)
[6] => Array
(
[product_id] => 7
[product_name] => chaise
)
[7] => Array
(
[product_id] => 8
[product_name] => stool
)
)
I can quite easily create the drop down menu for it using this.
<?php
foreach ($cms_product as $arr)
{echo "<option value=\"". $arr['product_id'] . "\">" . $arr['product_name'] . "</option>";}
?>
But wanted to create a function to create the list for me as there are quite a few drop down menus.