$dbQuery="select id, name from artists where name like '$keyword%' order by name asc limit 15";
echo mysql_num_rows($dbResult)."\n";
while ($dbRow=mysql_fetch_array($dbResult)) {

// for each artist, return the number of albums belonging to that artist
// use a separate query here to obtain the number
// return the number after the artist's name in the echo statement, separated with a '^' (1b)

echo $dbRow["id"]."^".$dbRow["name"]."\n";

Have this code and as you can see i have to write another dbquery that will display the number of albums an artist has. I also have to modify this piece of code

function showArtists() {
if (ajaxObject.readyState==4) {
var artistsArray=ajaxObject.responseText.spl…
var numArtists=artistsArray[0];
var htmlStr="";
for (var i=1; i<=numArtists; i++) {
var artistDetails=artistsArray.split("^")…
htmlStr+="<div onmouseover='javascript:suggestOver(this… ";
htmlStr+="onmouseout='javascript:sugge… ";
htmlStr+="onclick='javascript:setArtis… ";

// add the number of albums in brackets after the artist's name in the next line (1b)
htmlStr+="class='suggest_link'>" + artistDetails[1] + "</div> ";

Anyone know how i write this, ive tried a few things but when i try it on the webpage it stops the ajax auto fill when typing.


What you are looking for is something like this:

select artist, count(artist)
from mytable
group by artist
SELECT id, name, count(*) as `albums` FROM artists
LEFT JOIN albums ON artists.id = albums.artistid
WHERE artists.name like '$keyword%'
GROUP BY artists.id
ORDER BY artists.name ASC

the ajax doesn't look like the complete code either so hard to advise on that, if it's stopping things that worked before there is likely a javascript error in the console.

If you change to the query above, making `albums` your albums table and with the correct id fields it looks like you want to change this line:

echo $dbRow["id"]."^".$dbRow["name"]."\n";

to this

echo $dbRow["id"]."^".$dbRow["name"]."^".$dbRow["albums"]."\n";

then in the javascript you access it with "artistDetails[2]"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.