Forum Moderators: coopster

Message Too Old, No Replies

See If Item Already Exisits In a PHP ComboBox

ComboBox Item Exists

         

Solvo

4:15 pm on Oct 25, 2009 (gmt 0)

10+ Year Member



I have a db with about 50 tracks by 10 artists. What I want to do is populate or build a select list with the Artist names so the user can select a prefered artist thus populating a second combobox with tracks by this artist.

I obviousley only want the artist to appear once in the "Artist" combobox so I need to check to see if the artist already exists. if it does then i don't add it again.

EG

echo '<select name="MyJukeBox" onchange="PopulateTracks(this)"><option value="">Select Artist</option>';

while ($row = mysql_fetch_array($result)) {

.NET Equiv --> If MyJukebox.Items.Contains($row['Artist'])=False then

echo '<option value="',$row['File Name'],'">',$row['Artist'],'</option>';

.NET Equiv --> End if

}

echo '</select>';

It's the PHP Equivelant of the .NET IF statement that I am stuck with...

Any Ideas? THank You in Advance.
Solvo

Solvo

5:39 pm on Oct 25, 2009 (gmt 0)

10+ Year Member



Actually it doesn't matter, I just used an arrey. as follows:

while ($row = mysql_fetch_array($result)) {

$ThisArtist=$row['Artist'];

If (!in_array($ThisArtist, $MyArtists)) {

echo '<option value="',$row['File Name'],'">',$row['Artist'],'</option>';

$ACount=count($MyArtists);
$ACount++;

$MyArtists[$ACount]=$row['Artist'];

}
}

rocknbil

7:13 pm on Oct 25, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A better approach might be to use DISTINCT in the query itself, then a) you don't have to jump through hoops in your programming, and b) it will **actually select** less rows, making your query faster. It will also avoid selecting all columns (select *) which is an inefficient select if you only need two items. Example,

$query = "select distinct Artist, FileName from music order by Artist";

Solvo

9:07 am on Oct 26, 2009 (gmt 0)

10+ Year Member



Oh dear, I really need to get myself some kind of reference material don't I?

So for the purpose of leaning the above query does the following.

1. We are only querying Columns "Artist" and "FileName" (select distinct Artist, FileName)

2. From the database "Music" (from music)

3. Ensuring that "Artist" is Distict (order by Artist). e.g. If I changed "order by Artist" to "order by FileName" would that prevent duplicate file names?

I started PHP about a week ago, now I'ts time to get a big fat book I think.

Again Thank You RocknBill.

rocknbil

5:45 pm on Oct 26, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oh dear, I really need to get myself some kind of reference material don't I?

Understand that PHP and mySQL are not tied as one technology; I access mySQL from Perl, PHP, and as a stand-alone app. The efficiency of your PHP application is going to rely on the efficiency of how you are accessing mySQL. So if you want a good reference, it is here:

mySQL Documentation [dev.mysql.com]

... the above query does the following.

1 and 2, correct. 3, no.

The "order by" and direction clause can be applied to any valid field and has no effect on the select itself. The default without order by is order asc (ascending) by the auto increment column. With a column 1,2,3,4,5 as rows, no order clause and "order by id asc" are synonymous. "order by id desc" will give you 5,4,3,2,1.

Order by's on text fields order alphabetically. So!

If I changed "order by Artist" to "order by FileName" would that prevent duplicate file names?

No. It would just rearrange the results.

If you have duplicate file names, you may have bigger issues, but to answer the question,

$query = "select distinct FileName Artist, from music order by Artist";

If both fields have duplicates on different records, hmm . . . don't know . . . I'd re-think my logic.