Jump to content

[SOLVED] printing only unique variables


jeff5656

Recommended Posts

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.