Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have the following tables (relevant to my question)
category and product

fields to join
category - short, image
product - short, image

short = short form of category name
image = image name


basically i want to select an image for category based on the images already set in product (so when i select my drop down should only have the images that were set for products for that category)

my code below has two problems - one it doesn't show the already selected field in category and two it lists all the images and not just where the short field value match

code:
<?php
$qry=mysql_query("SELECT * FROM product", $con);
if(!$qry)
{
die("Query Failed: ". mysql_error());
}
?>


Select Category Image
<select name="image" id="image">
    <option value=""></option>
    <?php
    // First, define the desired default value
    $default_value = $row['image'];
    while($row=mysql_fetch_array($qry))
    {
        // Then you can mark that one as selected in your "while" loop
        $selected = ($row['image'] == $default_value) ? ' selected' : '';
        echo "<option value='" . $row['image'] . "'" . $selected . ">" . $row['image'] . "</option>";
    }
    ?>
</select>
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900