bullbreed Posted February 9, 2010 Share Posted February 9, 2010 HI. I had help from the forum the other day about inserting data from the database in to a menu form field This was the code I was taught and it works. However, If I have 2 articles in the database with the same category the menu shows the category twice. e.g Category----------------Article Animals----------------------Cats Animals----------------------Dogs Category----------------Article Vegetables----------------------Carrotts The Menu Form Field Shows Animals Animals Vegetables <div class="infowrap"> <?php //Open the database mysql_connect("localhost","root",""); mysql_select_db("********"); //Select the database $sql = "SELECT DISTINCT(article_section) FROM articles"; $query = mysql_query($sql)or die;(mysql_error()); if(mysql_num_rows($query) > 0){ ?> <label> <select name="exist_cat" id="exist_cat"> <option selected></option> <?php while($r = mysql_fetch_array($query)){ echo '<option value=" ' .$r['article_section'] . '">' . $r['article_section'] . '</option>'; } ?> </select> <?php } ?> </label> </div> How do I restrict the output to show the category only once. Because if I have 50 articles in the animal section the menu form element will show Animals 50 times Link to comment https://forums.phpfreaks.com/topic/191519-displaying-data-in-to-a-menu-form-element/ Share on other sites More sharing options...
Fergal Andrews Posted February 9, 2010 Share Posted February 9, 2010 Hi bullbreed, I set up a quick db and created an articles table with 3 records, one of which I duplicated. When I ran the script I only got one option for 'Animals', so I reckon the script is fine. I'd have a closer look at the database or better still post a dump of it here. Fergal HTML result of script: <div class="infowrap"> <label> <select name="exist_cat" id="exist_cat"> <option selected></option> <option value=" Animals">Animals</option><option value=" Vegtables">Vegtables</option></select> </label> </div> Link to comment https://forums.phpfreaks.com/topic/191519-displaying-data-in-to-a-menu-form-element/#findComment-1009634 Share on other sites More sharing options...
bullbreed Posted February 9, 2010 Author Share Posted February 9, 2010 Hi, thanks very much for that. I checked the database and there was a space after the second Animal value so it returned it twice Your a star. Link to comment https://forums.phpfreaks.com/topic/191519-displaying-data-in-to-a-menu-form-element/#findComment-1009664 Share on other sites More sharing options...
bbaker Posted February 9, 2010 Share Posted February 9, 2010 be sure to trim() your data before it gets saved into the db Link to comment https://forums.phpfreaks.com/topic/191519-displaying-data-in-to-a-menu-form-element/#findComment-1009702 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.