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
https://forums.phpfreaks.com/topic/219048-dynamic-lists-with-dynamic-selected/
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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.