AJAX/XML/PHP - Comparing results from two queries and displaying them
Right now I have one query that returns a list of results and displays them each as links on a page using AJAX. I have another working query that I want to use to compare to the first one, but I'm unsure how to do it exactly (new to AJAX).
What I'm trying to do ultimately is find matches in the two queries' results and format the links (the $("#judgesCompleted").append) that match, with different styles from those that don't.
PHP (xml2.php):
$query = "SELECT Name FROM judges LEFT JOIN $court
ON ($court.JudgeID = judges.JudgeID)
where Month='$month' and Year='$year' order by Name asc;";
$resultID = mysql_query($query, $linkID) or die("Data not found.");
$xml_output = "<?xml version=\"1.0\"?>\n";
$xml_output .= "<entries>\n";
for($x = 0 ; $x < mysql_num_rows($resultID) ; $x++){
$row = mysql_fetch_assoc($resultID);
$xml_output .= "\t<entry>\n";
$xml_output .= "\t\t<name>" . $row['Name']. "</name>\n";
$xml_output .= "\t</entry>\n";
}
$xml_output .= "</entries>";
echo $xml_output;
AJAX/JS:
$.ajax({
type: "POST",
url: "xml2.php",
data: 'court='+x,
dataType: "xml",
success: parseXml
});
function parseXml(xml)
{
$(xml).find("entry").each(function()
{
$("#judgesCompleted").append('<a href="viewreport.php">'+$(this).find("name"开发者_运维问答).text()+'</a><br />');
});
}
My new query:
$query2 = "SELECT Name FROM judges
LEFT JOIN $court ON ($court.JudgeID = judges.judgeID)
LEFT JOIN users ON ($court.userID = users.userID) WHERE Month='10' AND Year='2011' AND users.type = 'user' ORDER BY Name ASC; "
Hopefully that makes sense.
I'd do the heavy lifting in PHP:
- Run the first query, save it results into an array (A1)
- Run the second query, save its results into another array (A2)
- (you could probably write it as one query to lighten the DB load if that is a concern)
The first array will have more results than the second, so iterate over A1. On each value, check if it exists in A2. As you go, shuffle the matches into one XML node and the misses into another XML node.
Grab that XML with AJAX and separate back out the two nodes (matches and misses), then go through each one to apply styling as needed.
You can try to UNION the 2 queries and implement the compare logic in javascript.
精彩评论