Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Jan 2019
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Show items under categories with PHP and XML

    'm trying to figure out how to display and sort items by category with PHP. Source of the data would be an XML file.

    XML file looks like this:

    Code:
    <root>
      <item>
        <name>name A</name>
        <category>category 1</name>
        <desc>desc A</desc>
      </item>
      <item>
        <name>name B</name>
        <category>category 1</name>
        <desc>desc B</desc>
      </item>
      <item>
        <name>name C</name>
        <category>category 2</name>
        <desc>desc C</desc>
      </item>
    </root>
    Output should be something like:

    Category 1
    name A
    desc A

    name B
    desc B

    Category 2
    name C
    desc C

    Any help or direction would be great!

  2. #2
    Master Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    5,823
    Thanks
    26
    Thanked 606 Times in 599 Posts
    Convert the XML into a PHP array, then you can apply PHP's array sorting functions.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  3. Users who have thanked Dormilich for this post:

    exxeraga (Jan 15th, 2019)

  4. #3
    New to the CF scene
    Join Date
    Jan 2019
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thats where i'm stuck actually, I pull the data from XML but cant figure out how to sort it and get needed output

    Code:
    $xml = simplexml_load_file("file.xml");
                                        
        foreach ($xml->item as $item) {
        
            echo $item->category;
            echo $item->name; 
            echo $item->desc;
        }

  5. #4
    New to the CF scene
    Join Date
    Jan 2019
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts
    So i figure out I have to parse XML file into multidimensional array where first dimension will be the category...

  6. #5
    Senior Coder
    Join Date
    Aug 2006
    Posts
    1,877
    Thanks
    12
    Thanked 402 Times in 399 Posts
    You can select the array of items with xpath, then sort them with usort, as follows. I had to make some tweaks to your xml because it wasn't correct.
    Code:
    <?php
    $string = '
    <root>
      <item>
        <name>name A</name>
        <category>category 1</category>
        <desc>desc A</desc>
      </item>
      <item>
        <name>name B</name>
        <category>category 1</category>
        <desc>desc B</desc>
      </item>
      <item>
        <name>name C</name>
        <category>category 2</category>
        <desc>desc A</desc>
      </item>
    </root>';
    
    function cmp ($a,$b)
    	{
    	return strcmp ($a->desc,$b->desc);
    	}
    ?>
    <!DOCTYPE html>
    <html>
    <head>
    	<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
    </head>
    <body>
    <?php
    	$xml = simplexml_load_string ($string);
    	$list = $xml->xpath("item");	// select all <items> as an array
    	usort ($list,cmp);
    ?>
    </body>
    </html>

  7. Users who have thanked tracknut for this post:

    exxeraga (Jan 15th, 2019)

  8. #6
    New to the CF scene
    Join Date
    Jan 2019
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks, I managed to solve it (with help from other sources) but I'll give a try to your idea also.

    Code that gave me the output that i needed (in case someone else run into a same noobie trouble as me )

    Code:
        $xml = simplexml_load_file("../assets/xml_menu/meni_bs.xml") or die ("Greska: meni_bs.xml ne postoji");
    
        $array = [];
    
        foreach ($xml->item as $itm) {
            if (!isset($array[(string)$itm->podkategorija])) {            
                $array[(string)$itm->podkategorija] = [];
            }
            $array[(string)$itm->podkategorija][(string)$itm->ime] = (string)$itm->kratki_opis;
        }
    
        foreach($array as $sub=>$data){
            echo "$sub <br />";
    
            foreach($data as $name=>$desc){
                echo "$name<br />  ($desc)<br />";
            }
        }
    Thanks for your help and time, I really appreciate it!

  9. #7
    New to the CF scene
    Join Date
    Jan 2019
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Cant edit the above post... Code i gave before is not corrected for the example xml data
    This is the correct one:

    Code:
        $xml = simplexml_load_file("file.xml");
    
        $array = [];
    
        foreach ($xml->item as $itm) {
            if (!isset($array[(string)$itm->category])) {            
                $array[(string)$itm->category] = [];
            }
            $array[(string)$itm->category][(string)$itm->name] = (string)$itm->desc;
        }
    
        foreach($array as $sub=>$data){
            echo "$sub <br />";
    
            foreach($data as $name=>$desc){
                echo "$name<br />  ($desc)<br />";
            }
        }

  10. #8
    Master Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    5,823
    Thanks
    26
    Thanked 606 Times in 599 Posts
    That code only works correctly, if <name> is unique for each category (i.e. only appears in one <item> per category), otherwise only the last description is used.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •