How can a specific product name be selected in an option list based on a brand ID in PHP?
To select a specific product name based on a brand ID in PHP, you can create an associative array where the keys are the brand IDs and the values are arrays of product names. Then, you can use the brand ID to access the corresponding array of product names and populate the option list.
// Associative array of brand IDs and product names
$products = array(
'brand1' => array('Product A', 'Product B', 'Product C'),
'brand2' => array('Product X', 'Product Y', 'Product Z')
);
// Brand ID selected by user
$selectedBrand = 'brand1';
// Populate option list with product names based on selected brand ID
echo '<select name="product">';
foreach ($products[$selectedBrand] as $product) {
echo '<option value="' . $product . '">' . $product . '</option>';
}
echo '</select>';
Keywords
Related Questions
- What are best practices for selecting the storage location for form data on a server using PHP?
- How can a beginner in PHP development effectively navigate and utilize the available resources, such as forums and documentation, to enhance their skills and knowledge?
- What are some best practices for optimizing the performance of PHP scripts that handle PDF files?