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
- Is it necessary to use error_reporting(E_ALL) in PHP to display undefined variable errors?
- What are the best practices for handling form submissions in PHP to prevent data from being overwritten on each page load?
- What is the significance of the $ character in regular expressions in PHP, and how does it behave with different line break characters?