jeff5656 Posted June 8, 2009 Share Posted June 8, 2009 I query the variable "resident" but I only want to display 1 instance. In other words if the name "smith" comes up twice, don't keep putting smith as a separate <option> in the form. Only use Smith once. How do I do this? Here's the code I have: <?php $query4 = "SELECT * FROM icu WHERE signoff_status = 'a'"; $results4 = mysql_query ($query4) or die (mysql_error()); ?> <form action="resident_print.php" name="resident_print" method="post" > <select name = "resident" > <option value="">--Select <?php while ($row4 = mysql_fetch_assoc ($results4)) { if ($row4['resident'] != '') { ?><option value = "<?php echo $row4['resident'];?>"><?php echo $row4['resident'];?></option> <?php } } ?> </select> <input type="submit" value="Print by resident" /></form> Link to comment https://forums.phpfreaks.com/topic/161374-solved-printing-only-unique-variables/ Share on other sites More sharing options...
joshgarrod Posted June 8, 2009 Share Posted June 8, 2009 I had a similar requirement, this is what I use, just edit it for your needs: $query = "select year, count(year) from classifieds where year in (select distinct year from classifieds) group by year"; Link to comment https://forums.phpfreaks.com/topic/161374-solved-printing-only-unique-variables/#findComment-851618 Share on other sites More sharing options...
PFMaBiSmAd Posted June 8, 2009 Share Posted June 8, 2009 Mysql has a DISTINCT keyword that can be used with a SELECT query - DISTINCT and DISTINCTROW are synonyms and specify removal of duplicate rows from the result set. To use this, the columns you select must just be those that you want the distinct values of. You would need to specify the resident column instead of using * And what's up with this line of code - if ($row4['resident'] != '') { That implies that you have rows with blank data in your table. You should be validating input data to insure you don't insert rows that have nothing in them and if by some chance you do allow that column to be empty, you should specify that condition in the WHERE part of the query so that only rows with something in that column are matched by the query. Link to comment https://forums.phpfreaks.com/topic/161374-solved-printing-only-unique-variables/#findComment-851628 Share on other sites More sharing options...
jeff5656 Posted June 8, 2009 Author Share Posted June 8, 2009 Thanks. The DISTINCT worked perfectly. Also I got rid of the if statement and changed the query to: <?php $query4 = "SELECT DISTINCT resident FROM icu WHERE signoff_status = 'a' and resident !=''"; Thanks for the suggestions. Link to comment https://forums.phpfreaks.com/topic/161374-solved-printing-only-unique-variables/#findComment-851677 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.