Jump to content

Dynamic lists with dynamic selected


petenaylor

Recommended Posts

Hi all

 

I need to combine these two scripts:

 

Firstly, the following decides which out of the following list is selected based on its value in the mySQL table:

 

<select name="pack_choice">

<option value="Meters / Pack"<?php echo (($result['pack_choice']=="Meters / Pack") ? ' selected="selected"':'') ?>>Meters / Pack (m2)</option>

<option value="m3"<?php echo (($result['pack_choice']=="m3") ? ' selected="selected"':'') ?>>Meters / Pack (m3)</option>

<option value="Quantity"<?php echo (($result['pack_choice']=="Quantity") ? ' selected="selected"':'') ?>>Quantity</option>

</select>

 

Although this works OK, I need it also to show dynamic values like this:

 

select name="category">

<?php

$listCategories=mysql_query("SELECT * FROM `product_categories` ORDER BY id ASC");

while($categoryReturned=mysql_fetch_array($listCategories)) {

echo "<option value=\"".$categoryReturned['name']."\">".$categoryReturned['name']."</option>";

}

?>

</select>

 

I'm not sure if this is possible?

 

Many thanks for your help.

 

Pete

Link to comment
Share on other sites

Here's how, if anyone is interested:

 

<select name="category">

    <?php

 

$r=mysql_fetch_array(mysql_query("SELECT * FROM `products` WHERE id='".$edit."'"));

 

 

        $listCategories=mysql_query("SELECT * FROM `product_categories` ORDER BY id ASC");

        while($categoryReturned=mysql_fetch_array($listCategories)) {

            echo "<option value=\"".$categoryReturned['name']."\"".(($r['category']==$categoryReturned['name']) ? ' selected="selected"':'').">".$categoryReturned['name']."</option>";

        }

        ?>

      </select>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.