0

I have a xml file loaded simplexml, and I need to sort the fields by price, or by author or title. How could I do? Is it ok do it using xpath or another way? I saw a similar example here, but I have not very clear

XML

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <libro>
        <autor><![CDATA[Cervantes]]></autor>
        <titulo><![CDATA[El Quijote]]></titulo>
        <precio>30€</precio>
    </libro>
    <libro>
        <autor><![CDATA[Calderón de la Barca]]></autor>
        <titulo><![CDATA[La vida es sueño]]></titulo>
        <precio>25€</precio>
    </libro>
    <libro>
        <autor><![CDATA[Garcilaso de la vega]]></autor>
        <titulo><![CDATA[Egoglas]]></titulo>
        <precio>15€</precio>
    </libro>
    <libro>
        <autor><![CDATA[Raymond Carver]]></autor>
        <titulo><![CDATA[Catedral]]></titulo>
        <precio>16€</precio>
    </libro>
    <libro>
        <autor><![CDATA[George Orwell]]></autor>
        <titulo><![CDATA[1984]]></titulo>
        <precio>10€</precio>
    </libro>
    <libro>
        <autor><![CDATA[Fidor Dostoyevski]]></autor>
        <titulo><![CDATA[Crimen y Castigo]]></titulo>
        <precio>35€</precio>
    </libro>
    <libro>
        <autor><![CDATA[Juan Ponce]]></autor>
        <titulo><![CDATA[Cronica de la intervencion]]></titulo>
        <precio>25€</precio>
    </libro>
    <libro>
        <autor><![CDATA[Yukio Mishima]]></autor>
        <titulo><![CDATA[Confesiones de una mascara]]></titulo>
        <precio>22€</precio>
    </libro>
    <libro>
        <autor><![CDATA[Elfriede Jelinek]]></autor>
        <titulo><![CDATA[Deseo]]></titulo>
        <precio>20€</precio>
    </libro>
    <libro>
        <autor><![CDATA[Bram Stoker]]></autor>
        <titulo><![CDATA[Dracula]]></titulo>
        <precio>18€</precio>
    </libro>
</root>

PHP

$xml = simplexml_load_file('xml/libros.xml');
$sum = 0;
$h = "<table>";
$h .= "<tr>";
$h .= "<td><a href='#'  id='ordAut'><img src='img/up_down.png' /></a></td><td><a href='#'  id='ordTit'><img src='img/up_down.png' /></a></td><td><a href='#'  id='ordPre'><img src='img/up_down.png' /></a></td>";
$h .= "</tr>";
foreach ($xml->libro as $book) {
    $h .= "<tr>";
    $h .= "<td>".$book->autor."</td><td>".$book->titulo."</td><td>".$book->precio."</td>";
    $h .= "</tr>";
    $sum += $book->precio;
}

$h .= "<tr><td colspan=\"2\">sum:</td><td>$sum</td></tr></table>";
echo $h;
5
  • see this stackoverflow.com/questions/3998722/… - the accepted answer should do the trick! Commented Mar 24, 2013 at 22:09
  • good, just update your question if you need help. Commented Mar 24, 2013 at 22:23
  • jal, I got your comment that you had updated your question, but it linked to a page that said: question had been removed by author ? Commented Mar 24, 2013 at 22:40
  • @michi Sorry I made a mistake. I wrote it again Commented Mar 25, 2013 at 9:55
  • @michi this example sorts a field only, what I need is to sort the entire table, for example sort author, title, price, by price ascending Commented Mar 25, 2013 at 12:33

2 Answers 2

0

Sort your simplexml like this:

function sort_obj_arrwritten by GZipp in this post

$xml = simplexml_load_string($xmlstr, 'SimpleXMLElement', LIBXML_NOCDATA);

$books = $xml->xpath("//libro");

echo "<pre>";
print_r($books);
echo "</pre>";

$field = 'precio';
sort_obj_arr($books,$field,SORT_DESC);

echo "<pre>";
print_r($books);
echo "</pre>";


function sort_obj_arr(& $arr, $sort_field, $sort_direction)
{
    $sort_func = function($obj_1, $obj_2) use ($sort_field, $sort_direction)
    {
        if ($sort_direction == SORT_ASC) {
            return strnatcasecmp($obj_1->$sort_field, $obj_2->$sort_field);
        } else {
            return strnatcasecmp($obj_2->$sort_field, $obj_1->$sort_field);
        }
    };
    usort($arr, $sort_func);
}

Live demo @ http://codepad.viper-7.com/QvLqIq

Sign up to request clarification or add additional context in comments.

Comments

0

i know it sounds dumb at first: but map the data in an array and use a custom array callback for usort if you want to do it right right you will develop something that is called a paginator, which takes care of limit the result displayed on the page and lets you use custom filters.

1 Comment

I used usort, but I had problems

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.