Jump to content

Problem with AJAX/PHP


LeeMC1989

Recommended Posts

Hi there, I'm not new to the forum but I have misplaced my other account..

 

This is a script where when you type a Album name it retrieves the album name from database.

 

Ok well heres my problem, I have a script that when typing something into a text input ajax requests are sent/recieved and displayed in a div with results, with this I have made it so can click the result and it will populate into the input.

 

I have tried to modify this the best I can but failed while doing so.., I have it set up so when you type the album name into the box it will retrieve the album name but when I click the result I want the album URL to populate the input box..

(Usually the thing you are searching for populates the input)

 

The problem is when I click the album its populating the input with any albumart URL and not the albums correct matching URL from database..

 

This probably sounds confusing :s..

 

Search Album in input > Click album result > Populate input with albumart URL from database

 

I'm sorry if I'm confusing you but I can't seem to be able to explain this properly :s..

 

<?php
// Fill up array with names

include("../connectvars.php");
$query = mysql_query("SELECT * FROM `Music` GROUP BY musicalbum");
$b=mysql_fetch_array($query);
while ($art=mysql_fetch_array($query)) {
$a[] = $art['musicalbum'];
$b[] = $art['musicalbumart'];
}

//get the q parameter from URL
$q=$_GET["a"];

//lookup all hints from array if length of q>0
if (strlen($q) > 0)
{

$hint="";
for($i=0; $i<count($a); $i++)
{

	if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q))))
	{

		if ($hint=="")
		{

			$hint="<a href=# onclick=document.getElementById('malbumart').value='".$b[$i]."'>".$a[$i]."</a>";
		}
		else
		{

			$hint="<a href=# onclick=document.getElementById('malbumart').value='".$b[$i]."'>".$hint."</a> , <a href=# onclick=document.getElementById('malbumart').value='".$b[$i]."'>".$a[$i]."</a>";

		}
	}
}

}

// Set output to "no suggestion" if no hint were found
// or to the correct values
if ($hint == "")
  {
  $response="No AlbumArt Suggestions";
  }
else
  {
  $response=$hint;
  }

//output the response
echo $response;
?>

 

I guess a way to explain this..

 

DATABASE EXAMPLE:

AlbumName1 | Albumart1

AlbumName2 | Albumart2

AlbumName3 | Albumart3

 

Okay so if I search Albumname1 in the searchbox and click the name it should populate the box with Albumart1 (But it doesn't it populates it with some other albumart..) I want the matching albumart to match up with the album name..

Link to comment
Share on other sites

I have a hard time following the code.  But I can tell you this is wrong:

 

 

$query = mysql_query("SELECT * FROM `Music` GROUP BY musicalbum");
$b=mysql_fetch_array($query);
while ($art=mysql_fetch_array($query)) {
$a[] = $art['musicalbum'];
$b[] = $art['musicalbumart'];
}

 

You fetch one row and assign it to $b. If you var_dump($b) after that line I think you'll be surprised by what it contains.  Then you proceed to fetch other rows, but now you add additional array elements, each of which is the contents of the 'musicalbumart' column.  Things would be a lot cleaner if you just did this:

 

$query = mysql_query("SELECT musicalbum, musicalbumart FROM `Music` GROUP BY musicalbum");
while ($row = mysql_fetch_assoc($query)) {
  $rows[] = $row;
}

 

Now you have an array ($rows) which has one entry for each album/albumart row in the database.

 

Truly this is a mess. 

 

I'm not sure why you're trying to make 2 arrays when 1 will do.  I don't really understand you 

 

Link to comment
Share on other sites

Sorry about the trailing off above.. i was going to write something like I don't understand your approach to .... whatever, but I trailed off and posted that accidently. 

 

At any rate, I'm glad you got things sorted out, and hopefully I put you on the right track.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.