1
xml
에서 가져온 테이블 데이터를 정렬해야하며 제품 이름을 기준으로 테이블을 정렬해야합니다. 나는 함수를 사용하여 w3school
에서 시도했지만 작동하지 않습니다. usort
메서드를 사용하여 시도했지만 항목의 특성을 사용하여 정렬 할 수만 있으며 xml
데이터를 정렬 한 후에 html
데이터를 보내는 방법을 모르겠습니다. 정말 도움이 필요합니다. PHP를 사용하여 XML 파일에서 HTML 테이블로 가져온 데이터를 정렬하는 방법은 무엇입니까?
이
는xml
코드 :
<channel>
<item id='123'>
<g:productname>67510BS Black Shirt</g:productname>
<g:price>20</g:price>
<g:stock>190</g:stock>
</item>
<item id='122'>
<g:productname>10973JU White Shirt</g:productname>
<g:price>23</g:price>
<g:stock>59</g:stock>
</item>
<item id='103'>
<g:productname>12390IJ Yellow Shirt</g:productname>
<g:price>18</g:price>
<g:stock>27</g:stock>
</item>
<item id='89'>
<g:productname>12094OK Grey Shirt</g:productname>
<g:price>10</g:price>
<g:stock>0</g:stock>
</item>
<item id='200'>
<g:productname>98704OW Brown Shirt</g:productname>
<g:price>15</g:price>
<g:stock>54</g:stock>
</item>
</channel>
그리고 이것은 PHP 코드입니다 : 다음 액세스
<?php
$document = new DOMDocument('1.0', 'UTF-8');
$document->formatOutput = true;
$document->preserveWhiteSpace = false;
$document->load('shirt.xml');
filterxml($document)
createhtml($document);
function filterxml($doc) {
$xpath = new DOMXPath($doc);
// Find the <item> nodes that has g:availability = Disabled or stock = 0, and then delete them
$nodes = $xpath->query("/rss/channel/item[(g:availability = 'Disabled') or (g:stock = 0)]");
// Remove the offending nodes from the DOM
for ($i = 0; $i < $nodes->length; $i++) {
$node = $nodes->item($i);
$node->parentNode->removeChild($node);
}
// ----------- THIS IS THE USORT THAT I'VE TRIED -----------
/* $listitem = $xpath->query('//item');
$items = iterator_to_array($listitem);
function sort_by_numeric_id_attr($a, $b) {
return (int) $a->getAttribute('id') - (int) $b->getAttribute('id');
}
usort($items, 'sort_by_numeric_id_attr');*/
}
function createhtml($doc) {
$html = new DOMDocument('1.0', 'UTF-8');
$html->preserveWhiteSpace = true;
$xpath = new DOMXPath($doc);
$num = 0;
$header = array (
'No.',
'Product Name',
'Price',
'Stock'
);
$htmltag = $html->appendChild($html->createElement('html'));
$body = $htmltag->appendChild($html->createElement('body'));
$body->setAttribute('onload', 'sortTable()');
$table = $body->appendChild($body->createElement('table'));
$table->setAttribute('id', 'productTable');
$row = $table->appendChild($html->createElement('tr'));
foreach($header as $label) {
$row
->appendChild($html->createElement('th'))
->appendChild($html->createTextNode($label));
}
foreach ($xpath->evaluate('//item') as $item) {
$row = $table->appendChild($html->createElement('tr'));
$num++;
$number = $row->appendChild($html->createElement('td', $num));
$prodName = $xpath->evaluate('string(g:productname)', $item);
$itemName = $row->appendChild($html->createElement('td', $prodName));
$itemName->setAttribute('width', '100px');
$price = $xpath->evaluate('number(g:price)', $item);
$row
->appendChild($html->createElement('td'))
->appendChild(
$html->createTextNode('$ ' . number_format($price, 0, '', '.') . ',-')
);
$stock = $xpath->evaluate('number(g:stock)', $item);
$stocktd = $row->appendChild($html->createElement('td', $stock));
$stocktd->setAttribute('width', '350px');
}
$script=<<<END
function sortTable() {
var table, rows, switching, i, x, y, shouldSwitch, dir, switchcount = 0;
table = document.getElementById('tabelProduk');
switching = true;
dir = 'asc';
while(switching) {
switching = false;
rows = table.getElementsByTagName('tr');
for (i=1; i<(rows.length-1); i++) {
shouldSwitch = false;
x = rows[i].getElementsByTagName('td')[n];
y = rows[i].getElementsByTagName('td')[n];
if(dir == 'asc') {
if(x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) {
shouldSwitch = true;
break;
}
}
else if (dir == 'desc') {
if (x.innerHTML.toLowercase() < y.innerHTML.toLowerCase()) {
shouldSwitch = true;
break;
}
}
}
if (shouldSwitch) {
rows[i].parentNode.insertBefore(rows[i+1], rows[i]);
switching = true;
switchcount++;
}
else {
if (switchcount == 0 && dir == 'asc') {
dir = 'desc';
switching = true;
}
}
}
}
END;
$scripttag = $htmltag->appendChild($html->createElement('script', $script));
$scripttag->setAttribute('type', 'text/javascript');
$html->formatOutput = true;
$htmlsave = $html->saveHtml();
file_put_contents('download/Shirt.html', $htmlsave);
}
}
?>
오 마이! 고마워요 @ 고스트, 그게 효과가! – rhog23
@ rhog23 기쁜 약간의 빛 – Ghost