wkilc Posted July 20, 2016 Share Posted July 20, 2016 I am editing a script that pulls values from a database to populate a pulldown menu: if($_GET['car'] == $row['car'] ){ echo "selected=\"selected\""; } print " value=\"$page_name&car=" . $row['car'] . "\">" . $row['car'] . "</option>\n"; } print "</select>"; This works great, except some of the "car" values in my table are empty... so I get one row in my pulldown menu that's empty: <option selected="selected" value="/list.php?car="></option><option value="/list.php?car=Honda">Honda</option><option value="/list.php?car=Hyundai">Hyundai</option> etc... How can I tell it to not print an "empty" result in my pull down? My apologies if I am not explaining this well. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/301528-populating-pulldown-menu-from-database/ Share on other sites More sharing options...
benanamen Posted July 20, 2016 Share Posted July 20, 2016 (edited) If you have a properly normalized database with one table with a list of car names you shouldn't have any empty rows. You would also want to be using the ID of the car name not the actual name itself for the value. Additionally, the file name does not belong in the value. Edited July 20, 2016 by benanamen Quote Link to comment https://forums.phpfreaks.com/topic/301528-populating-pulldown-menu-from-database/#findComment-1534754 Share on other sites More sharing options...
Zane Posted July 21, 2016 Share Posted July 21, 2016 just add onto your current IF statement if($_GET['car'] == $row['car'] && !empty($row['car']) ){ echo "selected=\"selected\""; } print " value=\"$page_name&car=" . $row['car'] . "\">" . $row['car'] . "</option>\n"; } Quote Link to comment https://forums.phpfreaks.com/topic/301528-populating-pulldown-menu-from-database/#findComment-1534755 Share on other sites More sharing options...
benanamen Posted July 21, 2016 Share Posted July 21, 2016 (edited) @Zane, the if is just a quick fix band-aid for what is pretty clearly a bad database design. @wkilc, if your wanting to get things done correctly, post your DB schema and we will help you get this right from the ground up. Your Database is the foundation on which you will write all your code. If that is not right, the code you build on it will not be right and you will continue to run into unnecessary problems. Edited July 21, 2016 by benanamen 1 Quote Link to comment https://forums.phpfreaks.com/topic/301528-populating-pulldown-menu-from-database/#findComment-1534759 Share on other sites More sharing options...
Zane Posted July 21, 2016 Share Posted July 21, 2016 Well the OP didn't ask for opinions on his db setup, and didn't post any information about his db structure, so I don't honestly know what his db looks like. I can't just assume he doesn't have a primary key index setup, but that seems to be what everyone is assuming. Quote Link to comment https://forums.phpfreaks.com/topic/301528-populating-pulldown-menu-from-database/#findComment-1534760 Share on other sites More sharing options...
benanamen Posted July 21, 2016 Share Posted July 21, 2016 (edited) Well the OP didn't ask for opinions on his db setup, and didn't post any information about his db structure, Yeah, that's the thing with noobs (Don't mean it in a bad way), they don't know what to ask or provide. It the typical XY problem. http://xyproblem.info/ OP sees a particular problem and asks how to fix that problem not realizing it is just a symptom of the real problem. My experience says there is a very high probability that the DB structure is wrong which is why I asked the OP to provide it. I am sure you would agree that the DB is the first thing to ensure is correct and if not to fix that first. Edited July 21, 2016 by benanamen Quote Link to comment https://forums.phpfreaks.com/topic/301528-populating-pulldown-menu-from-database/#findComment-1534761 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.