0

Sorry for the long title. It's taken me an hour, but I've finally got this working. My question is if there's a much quicker, more efficient way to do what I'm doing here.

Basically, I've got an HTML text input field that uses jQuery to populate autocomplete dropdown answers (in this case, artists for a list of songs I've favorited). I previously had it working smoothly until I found that it was displaying duplications of some artists who I had favorited more than one song of. So I needed to use array_unique() to get rid of the duplicates.

Unfortunately my list was not in array form from the MySQL results, but just a string. After fiddling around for a while I got it to display in an array, but now this messed up my formatting for the jQuery list. array_walk() helped me format each item in the array, and then I converted this to a string. It just seems like a lot of work. Can I do it in less steps?

Here is my code:

<input type="text" class="span3" style="margin: 0 auto; " autocomplete="off" name="artist" data-provide="typeahead" data-items="4" data-source='[<?

$artistlist = "SELECT * FROM songs where id >= 0";
$result = $mysqli->query($artistlist);

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

$artists[] = "\"".$row['artist']."\",";
//have to format this way for the jQuery results.

$artists = array_unique($artists);
}

function formatArtists(&$artists) {
$artists = str_replace("'", "&#39;", $artists);
//have to do this because of "O'" names.

}
array_walk($artists, "formatArtists");
foreach($artists as $artist) {
$artistSelect .= $artist;
}

$artistSelect = substr(trim($artistSelect), 0, -1);
//I have to convert the array to a string so that I can trim the final comma from the list of artists.  Otherwise the autocomplete breaks.

echo $artistSelect;
?>]'>

Thanks for the help, if you notice any other inefficiencies, I'm still learning so I would not mind correction!

1
  • 1
    At first try to use SELECT DISTINCT artists FROM songs where id >= 0 - this query will provide you list of artists without duplications. Commented Mar 12, 2014 at 16:40

1 Answer 1

2

Just a few advices:

Instead of looping across $artists you should call formatArtists when you are fetching query result

You can use a simple trick for avoiding artists repetitions using the artist's name as array key

There is a php function called implode() which will avoid you some lines.

Applying them all:

<input type="text" class="span3" style="margin: 0 auto; " autocomplete="off" name="artist" data-provide="typeahead" data-items="4" data-source='[<?

$artistlist = "SELECT * FROM songs where id >= 0";
$result = $mysqli->query($artistlist);
$artists = array();
while( $row = mysqli_fetch_assoc($result)) {
    $artist_name = '"'.formatArtists($row['artist']).'"';
    $artists[$artist_name] = $artist_name;
}
echo implode(',',$artists);//Will join every element from array using , character
?>]'>
Sign up to request clarification or add additional context in comments.

2 Comments

So if I do it this way, do I even need the formatArtists function? I could just apply it directly to $artist_name?
You're right if you replace $artist_name = '"'.formatArtists($row['artist']).'"'; with $artist_name = '"'.str_replace("'", "&#39;",$row['artist']).'"';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.