如何使用php将从xml文件中获取的数据排序到html表?

时间:2017-11-23 03:08:14

标签: php html xml sorting xmltable

我需要对从xml获取的表数据进行排序,我需要根据产品名称对表进行排序。我尝试使用sortTable()中的w3school函数,但它不起作用。我已尝试使用usort方法,但我只能使用项目的属性对其进行排序,而我不知道如何将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);
        }
    }


    ?>

1 个答案:

答案 0 :(得分:1)

对产品名称进行排序的另一种方法是将$xpath对象导入usort,然后从中访问产品名称并使用strcasecmp进行比较。

点子:

$items = iterator_to_array($listitem);
// sort by product name
usort($items, function($a, $b) use ($xpath) {
    $product_name_a = $xpath->evaluate('string(g:productname/text())', $a);
    $product_name_b = $xpath->evaluate('string(g:productname/text())', $b);

    return strcasecmp($product_name_a, $product_name_b);
});

注意:我不会使用DOMDocument创建html标记,但我只是通过字符串创建表。

Sample Output