Solved

How do you omit duplicate entries from the list created in php?

Posted on 2008-06-21
3
601 Views
Last Modified: 2013-12-12
Hi,
I have got some PHP code that populates a list based on entries found within a MySQL database.
It is successfully creating the list, but I was wondering how to omit the duplicate entries.
I would also like them listed alphabetically.
Eventually users will select from the values available, so duplicates are not meant to be there and setting it up alphabetically is user friendly.
Thanks..the code is attached.!
-------------------------
Populating a list box
-------------------------
 
<?php
include("Settings.php"); //Path to Settings.php
 
$sql= "SELECT * FROM event";
 
$result = mysql_query($sql) or die("Sql Query Failed: " . mysql_error());
 
 
$ListOptions = "";
 
while ($row = mysql_fetch_array($result)) {
        //The following line creates the <option></option> tags for the <select> list
        $ListOptions .= "\t<option value='" . $row["ID"] . "'>" . $row["EventType"] . "</option>\n";
}
 
?>
 
<select name='MyList'>
<option value="0" selected>Select EventType</option>
<?php echo $ListOptions; ?>
</select>

Open in new window

0
Comment
Question by:Amanda Watson
  • 2
3 Comments
 
LVL 14

Expert Comment

by:agriesser
ID: 21839955
To sort them alphabetically, change your SQL statement from 'SELECT * from event' to 'SELECT * from event ORDER BY EventType' f.ex.

To avoid having duplicate entries in the database I'd suggest to create a primary key or unique column in the database itself. That way, no duplicate entries can be written to the database as a primary key or a column marked as unique assures that there are no duplicate values in it.

If you already have duplicate values in it and want to show possible duplicate values only once, you can use:

SELECT * from event GROUP BY EventType ORDER BY EventType

That way you will only get unique entries reported.

You might even want to change the primary key from ID (what it is probably at the moment) to EventType, therefore it's impossible to add duplicate entries into this table.

0
 
LVL 11

Author Comment

by:Amanda Watson
ID: 21840072
Thanks, that great.
Thanks for your help!
0
 
LVL 14

Accepted Solution

by:
agriesser earned 125 total points
ID: 21840120
You're welcome.
Will try to answer your other question too in a few minutes, please don't forget to assign points if the solution is acceptable for you.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Add email address from a web page to outlook 2010 contacts ? 8 41
PHP Syntax Error 4 32
Need to remove %20 from url in get 17 36
Log in through ID 5 18
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question