I spent days figuring and reading about how to produce an xml file from a table in my db. I got it working fine and produces a valid xml file for me to parse. This file contains over 13000 records and I only need to pull records to display to the user where a certain xml field (category) contains data. Can this be done? The only other way I can think is to filter those records through the php script that is pulling the records and producing the xml file. Any assistance is appreciated. My code is below that I am using to create the xml and the client side jquery code to display the xml contents. I know my jquery script only is showing 2 of the xml fields because of testing.
Thanks.
PHP code to produce the xml file
$query = "SELECT name,test_code,cpt_code,components FROM tm_test";
$result = mysq开发者_JAVA技巧l_query($query);
$doc = new DomDocument('1.0');
header('Content-Type: application/xml; charset=ISO-8859-1');
// create root node
$root = $doc->createElement('tests');
$root = $doc->appendChild($root);
while($array = mysql_fetch_array($result)) {
// add node for each row
$occ = $doc->createElement('test');
$occ = $root->appendChild($occ);
$child = $doc->createElement('name');
$child = $occ->appendChild($child);
$value = $doc->createTextNode($array['name']);
$value = $child->appendChild($value);
$child = $doc->createElement('test_code');
$child = $occ->appendChild($child);
$value = $doc->createTextNode($array['test_code']);
$value = $child->appendChild($value);
$child = $doc->createElement('cpt_code');
$child = $occ->appendChild($child);
$value = $doc->createTextNode($array['cpt_code']);
$value = $child->appendChild($value);
$child = $doc->createElement('components');
$child = $occ->appendChild($child);
$value = $doc->createTextNode($array['components']);
$value = $child->appendChild($value);
$child = $doc->createElement('method');
$child = $occ->appendChild($child);
$value = $doc->createTextNode($array['method']);
$value = $child->appendChild($value);
$child = $doc->createElement('clinical_indication');
$child = $occ->appendChild($child);
$value = $doc->createTextNode($array['clinical_indication']);
$value = $child->appendChild($value);
$child = $doc->createElement('category');
$child = $occ->appendChild($child);
$value = $doc->createTextNode($array['category']);
$value = $child->appendChild($value);
}
$doc->save('testlist.xml');
echo $doc->saveXML();
?>
jQuery script to display all records
$(document).ready(function(){
$.ajax({
type: "GET",
url: "testlist.xml",
dataType: "xml",
success: parseXml
});
function parseXml(xml) {
$(xml).find('test').each(function(){
$("#testList").append('<li><div data-role="collapsible" data-collapsed="true" data-theme="b"><h3>' + $(this).find("name").text() + '</h3>' + '<p>' + $(this).find("test_code").text() + '</p></div></li>');
});
}
});
Just pass the desired category in your ajax call to PHP.
$.ajax({
type: "GET",
url: "testlist.xml",
data: "category=the_category_you_want",
dataType: "xml",
success: parseXml
});
The category will be available to your PHP script as $_GET['category']
.
Change your SQL query to filter by this category using a WHERE
clause:
$query = "SELECT name,test_code,cpt_code,components FROM tm_test
WHERE category = ?";
and bind the variable value (to prevent SQL injection).
精彩评论