How to sort the data that has been taken from an xml file to a html table using php?

rhog23

I need to sort table data that I've taken from an xml and I need to sort the table based on the Product Name. I've tried using the sortTable() function from w3school but it won't work. I've tried using the usort method, but I can only sort it using the item's attribute and I don't know how to send the xml data to html after it has been sorted out. Really need help here.

This is the xml code:

<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>

And this is the php code:

 <?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);
        }
    }


    ?>
Kevin

Another way to sort the product names is to import your $xpath object into the usort, then access the product name from there and use strcasecmp for comparison.

Idea:

$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);
});

Note: I wouldn't create the html markup using DOMDocument though, I'll just create the table through strings.

Sample Output

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to extract and Sort a Multidimensional Array data with PHP from a XML file (using simpleXML)

Data has been deleted from database but table row not removed and total record not reduced in php using ajax

Sort data from text file in a HTML table

a design through html and data taken from sql database using php and sql by using file_get_contents() and str_replace

How can I fetch XML Data from a file stored on my PC and populate a table in HTML using javascript?

How can I display data from MYSQL database using HTML table from a PHP file?

How to import data from XML file to HTML table

How to add records from an HTML table data taken from a table model into another table model?

how read the data from XML file using php

How to detect #N/A in a data frame (data taken from xlsx file) using pandas?

Fetch data from xml file and display in html table using Ajax and xpath

How to scrape HTML table data using PHP

Display data from database table into html table inside a php file

How do you pull a data from a second table that has the same id using PDO/php?

Trying to export data taken from HTML using beautiful soup

how to change the date format taken from the database in data table

How to put data from database table to a html table using ajax and php

How to know which file has been uploaded from multiple inputs of type file in reactjs using Express API

How to get all the data of a row that has been clicked in a table using javascript of jQuery

get data-id html from html table using php

draw a HTML table using JavaScript: This web page has not been found

Getting "file in use" error after populating a grid with data from a table after the table has been used in another form

How to get data from form using JS, when the form has been returned from AJAX itself?

How can i get the data which has been generated using load html feature of JavaFx WebView?

Reading data from a xml file inside HTML CDATA with PHP

How to use the images taken from user using input tag HTML?

How to create directory using ansible with directory names taken from a file

How to check if a node's value in a xml file is always taken from another fix node's value using xsl/xslt?

how to read data from csv file by using quick sort and display it