Solved

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

Posted on 2008-06-21
3
569 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
Comment Utility
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
Comment Utility
Thanks, that great.
Thanks for your help!
0
 
LVL 14

Accepted Solution

by:
agriesser earned 125 total points
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now