kee2ka4 Posted September 13, 2008 Share Posted September 13, 2008 Hello Peeps, I have a question regarding the select tag in a form. I have a php ForEach loop that loops throught a database an gets the value and the name of the select tags for the dropdown list. Here is the code: <select name="c"> <option value="">ALL</option> <?php foreach($catList as $catName): ?> <option value="<?php echo $catName['id'] ?>"><?php echo $catName['name'] ?></option> <?php endforeach; ?> </select> I needed to include a selected = "" tag if the $catName['id'] == $_GET['c']; I am not sure how to code this in the loop. Can anyone plz guid me on this. Thanks, Zub Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted September 13, 2008 Share Posted September 13, 2008 Change <?php foreach($catList as $catName): ?> <option value="<?php echo $catName['id'] ?>"><?php echo $catName['name'] ?></option> <?php endforeach; ?> to <?php foreach($catList as $catName): ?> <option value="<?php echo $catName['id'] ?>"<?php (($catName['id'] == $_GET['c']) ? 'selected="selected"' : null) ?>><?php echo $catName['name'] ?></option> <?php endforeach; ?> Quote Link to comment Share on other sites More sharing options...
kee2ka4 Posted September 13, 2008 Author Share Posted September 13, 2008 Hi, Thanks for your msg. I changed the code but it doesn't seem to work! Do I also tried added echo 'selected="selected"' but get an error so removed it. The dropdown works fine but the selected tag doesn't get added. Also the $_GET['c'] contains an id because I echo out the value of it at the top. Any Idea why it doesn't work? Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted September 13, 2008 Share Posted September 13, 2008 Add 'echo ' (without the quotes) before (($catName['id'] == $_GET['c']) Quote Link to comment Share on other sites More sharing options...
kee2ka4 Posted September 13, 2008 Author Share Posted September 13, 2008 Hi! Thanks for your advise, I got it to work by adding the echo at the start. Thanks! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.