Auto drop down selection 
Author Message
 Auto drop down selection

Hi there,

I have a function that I use to write a drop down box on my site. The
drop down contents are populated from a table. THe default selected
item is passed into the function as a variable.

However the 'selected' element is getting written to all the options
rather than just the one that matches the db query. Can anyone spot
where I have gone wrong?

        function vidcatDropDown($selected)
                {
                        //set sql and get list from db
                        $sql = "SELECT * FROM tbl_Cat ORDER BY vid_cat_name";
                        $result = db_query($sql, "database", "host", "username");
                        //process results and output
                        if ($myrow = mysql_fetch_array($result)){
                                echo $selected;
                                echo "<select name=\"vid_cat_id\">\n";
                                do {

                                        print("<option ");
                                        if ($selected = $myrow["vid_cat_id"]){
                                                        print "selected ";
                                                }
                                        printf("value=\"%s\">%s</option>\n" ,$myrow["vid_cat_id"],
$myrow["vid_cat_name"], $myrow["vid_cat_name"]);
                                } while ($myrow = mysql_fetch_array($result));
                                echo "</select>\n";
                        }else{
                                //handle errors
                                echo "No categories are loaded in the libruary";
                        }
                }

Many thanks for those that can,

Mark



Tue, 10 May 2005 23:16:20 GMT  
 Auto drop down selection
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Quote:

> Hi there,

> I have a function that I use to write a drop down box on my site. The
> drop down contents are populated from a table. THe default selected
> item is passed into the function as a variable.

> However the 'selected' element is getting written to all the options
> rather than just the one that matches the db query. Can anyone spot
> where I have gone wrong?

Looks like this line is the problem

if ($selected = $myrow["vid_cat_id"]){

should be

if ($selected == $myrow["vid_cat_id"]){

Give that a try.

Best,

- --
Waitman Gobble         EMK Design     Buena Park, California

Public Key                          http://pgp.emkdesign.com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.7 (GNU/Linux)

iD8DBQE93kv+lkBqm2sl6RIRAsQuAKCwItMMKNJnOKOAOh62WsHWzDRPiQCgoFC4
XD5v7ZaD3df3jAFuJkHcXxs=
=Rxxc
-----END PGP SIGNATURE-----



Tue, 10 May 2005 23:21:24 GMT  
 Auto drop down selection
Thanks Waitman, that worked a treat :-)


Sat, 14 May 2005 21:05:15 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Auto add in Drop down list

2. Auto-fill for drop down menu

3. Help ... Coding DROP DOWN LISTS / MULTI SELECTIONS ALSO

4. Blank selection in drop-down list

5. Drop Combo to Auto Drop

6. Browse selection auto on Tab switching - C4b

7. auto scrolling during text selection

8. Auto Shut Down

9. Drop List Auto Fill

10. Auto increment from drop combo box

11. Auto tab from Drop combo

12. how do I get a multicolumn drop box selection to show in the combo inputbox

 

 
Powered by phpBB® Forum Software