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 2 of 2
  1. #1
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,098
    Thanks
    297
    Thanked 12 Times in 12 Posts

    Exclamation how do display an xml file on my website but grouped by cat and sub cat?

    Hi All

    I have an xml file which i created, and i am using simplexml_load_file to pull out the data and display it on my site.

    i can pull out the data as it appears in the xml file like so
    PHP Code:
    $xml simplexml_load_file('services.xml');
    foreach (
    $xml->service as $service)
    {
        echo 
    $service->name."<br />";

    but what i really want to do is display the data in groups by category then subcat so all the hair treatments are displayed grouped by their subcats then all the beauty treatments and then wedding.

    i want something like this

    Hair
    Cutting => Dry Trim, Wet Cut etc etc
    Colouring => Semi Perminant, Full Head etc etc
    Styling => Straightening, Style and Blow Dry etc etc

    Beauty
    Sienna X Tanning => etc
    Manicures/Pedicures => etc
    Extras => etc

    Weddings
    etc etc

    here is my xml file
    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <services>
    	<service>
    		<id>001</id>
    		<name>Dry Trim</name>
    		<description></description>
    		<category>Hair</category>
    		<subcat>Cutting</subcat>
    		<price>12.00</price>
    	</service>
    	<service>
    		<id>002</id>
    		<name>Wet Cut</name>
    		<description></description>
    		<category>Hair</category>
    		<subcat>Cutting</subcat>
    		<price>14.00</price>
    	</service>
    	<service>
    		<id>003</id>
    		<name>Wet Cut and Blow Dry</name>
    		<description></description>
    		<category>Hair</category>
    		<subcat>Cutting</subcat>
    		<price>18.00</price>
    	</service>
    	<service>
    		<id>004</id>
    		<name>Restyle Cut and Blow Dry</name>
    		<description></description>
    		<category>Hair</category>
    		<subcat>Cutting</subcat>
    		<price>22.00</price>
    	</service>
    	<service>
    		<id>005</id>
    		<name>Mens Trim</name>
    		<description></description>
    		<category>Hair</category>
    		<subcat>Cutting</subcat>
    		<price>7.00</price>
    	</service>
    	<service>
    		<id>006</id>
    		<name>Childs Trim</name>
    		<description></description>
    		<category>Hair</category>
    		<subcat>Cutting</subcat>
    		<price>6.00</price>
    	</service>
    	<service>
    		<id>007</id>
    		<name>Semi Perminant</name>
    		<description></description>
    		<category>Hair</category>
    		<subcat>Colouring</subcat>
    		<price>15.00</price>
    	</service>
    	<service>
    		<id>008</id>
    		<name>Full Head Perminent</name>
    		<description></description>
    		<category>Hair</category>
    		<subcat>Colouring</subcat>
    		<price>25.00</price>
    	</service>
    	<service>
    		<id>009</id>
    		<name>Cap</name>
    		<description></description>
    		<category>Hair</category>
    		<subcat>Colouring</subcat>
    		<price>35.00</price>
    	</service>
    	<service>
    		<id>010</id>
    		<name>1/2 Head Foils</name>
    		<description></description>
    		<category>Hair</category>
    		<subcat>Colouring</subcat>
    		<price>40.00</price>
    	</service>
    	<service>
    		<id>011</id>
    		<name>Full Head Foils</name>
    		<description></description>
    		<category>Hair</category>
    		<subcat>Colouring</subcat>
    		<price>45.00</price>
    	</service>
    	<service>
    		<id>012</id>
    		<name>Foils and Full Head Colour</name>
    		<description></description>
    		<category>Hair</category>
    		<subcat>Colouring</subcat>
    		<price>55.00</price>
    	</service>
    	<service>
    		<id>013</id>
    		<name>Conditioning Treatment</name>
    		<description></description>
    		<category>Hair</category>
    		<subcat>Colouring</subcat>
    		<price>7.50</price>
    	</service>
    	<service>
    		<id>014</id>
    		<name>Straightning</name>
    		<description></description>
    		<category>Hair</category>
    		<subcat>Styling</subcat>
    		<price>8.00</price>
    	</service>
    	<service>
    		<id>015</id>
    		<name>Style and Blow Dry</name>
    		<description></description>
    		<category>Hair</category>
    		<subcat>Styling</subcat>
    		<price>10.00</price>
    	</service>
    	<service>
    		<id>016</id>
    		<name>Hair Up</name>
    		<description></description>
    		<category>Hair</category>
    		<subcat>Styling</subcat>
    		<price>25.00</price>
    	</service>
    	<service>
    		<id>017</id>
    		<name>Full Body</name>
    		<description></description>
    		<category>Beauty</category>
    		<subcat>Sienna X Spray Tanning</subcat>
    		<price>20.00</price>
    	</service>
    	<service>
    		<id>018</id>
    		<name>Half Body</name>
    		<description>Below or Above Waist</description>
    		<category>Beauty</category>
    		<subcat>Sienna X Spray Tanning</subcat>
    		<price>10.00</price>		
    	</service>
    	<service>
    		<id>019</id>
    		<name>Tan Parties</name>
    		<description>host a tan party - get 5 or more friends to have a full body tan for the reduced price of £18 (per tan) and as the host, get yours free!!</description>
    		<category>Beauty</category>
    		<subcat>Sienna X Spray Tanning</subcat>
    		<price>18.00</price>		
    	</service>
    	<service>
    		<id>020</id>
    		<name>Mini Manicure</name>
    		<description>Nail shape, Cuticle Tidy and Polish</description>
    		<category>Beauty</category>
    		<subcat>Manicure / Pedicure</subcat>
    		<price>8.00</price>		
    	</service>
    	<service>
    		<id>021</id>
    		<name>Mini Pedicure</name>
    		<description>Nail Shape, Cuticle Tidy and Polish</description>
    		<category>Beauty</category>
    		<subcat>Manicure / Pedicure</subcat>
    		<price>10.00</price>		
    	</service>
    	<service>
    		<id>022</id>
    		<name>Manicure</name>
    		<description>Nail shape, Cuticle Tidy, Scrub, Massage and Polish</description>
    		<category>Beauty</category>
    		<subcat>Manicure / Pedicure</subcat>
    		<price>18.00</price>		
    	</service>
    	<service>
    		<id>023</id>
    		<name>Pedicure</name>
    		<description>Nail Shape, Cuticle Tidy, Scrub, Rasp, Massage and Polish</description>
    		<category>Beauty</category>
    		<subcat>Manicure / Pedicure</subcat>
    		<price>22.00</price>		
    	</service>
    	<service>
    		<id>024</id>
    		<name>Luxury Manicure</name>
    		<description>Nail Shape, Cuticle Tidy, Scrub, Massage, Mask/Paraffin Wax/Hot Oil and Polish</description>
    		<category>Beauty</category>
    		<subcat>Manicure and Pedicure</subcat>
    		<price>25.00</price>		
    	</service>
    	<service>
    		<id>025</id>
    		<name>Luxury Pedicure</name>
    		<description>Nail Shape, Cuticle Tidy, Scrub, Rasp, Massage, Mask/Paraffin Wax/Hot Oil and Polish</description>
    		<category>Beauty</category>
    		<subcat>Manicure / Pedicure</subcat>
    		<price>30.00</price>		
    	</service>
    	<service>
    		<id>026</id>
    		<name>French Polish</name>
    		<description>For French Polish</description>
    		<category>Beauty</category>
    		<subcat>Extras</subcat>
    		<price>6.00</price>		
    	</service>
    	<service>
    		<id>027</id>
    		<name>Shellac</name>
    		<description>For Shellac</description>
    		<category>Beauty</category>
    		<subcat>Extras</subcat>
    		<price>10.00</price>		
    	</service>
    	<service>
    		<id>028</id>
    		<name>Minx</name>
    		<description>For Minx</description>
    		<category>Beauty</category>
    		<subcat>Extras</subcat>
    		<price>12.00</price>		
    	</service>
    	<service>
    		<id>029</id>
    		<name>Christal/Nail Art</name>
    		<description></description>
    		<category>Beauty</category>
    		<subcat>Extras</subcat>
    		<price>10.00</price>		
    	</service>
    	<service>
    		<id>030</id>
    		<name>Full Set Crystal Toes</name>
    		<description></description>
    		<category>Beauty</category>
    		<subcat>Extras</subcat>
    		<price>30.00</price>		
    	</service>
    	<service>
    		<id>031</id>
    		<name>Bride Hair</name>
    		<description></description>
    		<category>Bridal</category>
    		<subcat>Trials</subcat>
    		<price>25.00</price>		
    	</service>
    	<service>
    		<id>032</id>
    		<name>Bridesmaid Hair</name>
    		<description></description>
    		<category>Bridal</category>
    		<subcat>Trials</subcat>
    		<price>12.00</price>		
    	</service>
    	<service>
    		<id>033</id>
    		<name>Make Up</name>
    		<description></description>
    		<category>Bridal</category>
    		<subcat>Trials</subcat>
    		<price>8.00</price>		
    	</service>
    	<service>
    		<id>034</id>
    		<name>Bride Hair</name>
    		<description></description>
    		<category>Bridal</category>
    		<subcat>Wedding Day</subcat>
    		<price>45.00</price>		
    	</service>
    	<service>
    		<id>035</id>
    		<name>Bridesmaid Hair</name>
    		<description></description>
    		<category>Bridal</category>
    		<subcat>Wedding Day</subcat>
    		<price>25.00</price>				
    	</service>
    	<service>
    		<id>036</id>
    		<name>Make Up</name>
    		<description></description>
    		<category>Bridal</category>
    		<subcat>Wedding Day</subcat>
    		<price>20.00</price>				
    	</service>
    	<service>
    		<id>037</id>
    		<name>Sienna X Bridal Fitting Service</name>
    		<description>Includes 1 trial tan and the tan for special day. After the trial note the day the tan looks best - this will help decide when to apply the tan for the wedding day.</description>
    		<category>Bridal</category>
    		<subcat>Sienna X Bridal Fitting Service</subcat>
    		<price>40.00</price>				
    	</service>
    </services>
    any help would be great
    many thanks
    Luke
    Last edited by LJackson; 10-30-2012 at 10:06 PM.

  • #2
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,098
    Thanks
    297
    Thanked 12 Times in 12 Posts
    sorted it
    PHP Code:
    <?php
    $xml 
    simplexml_load_file('services.xml');

    $typesListXml $xml->xpath('service/category');

    if (!empty(
    $typesListXml)) {
        
    $typesList = array();
        foreach (
    $typesListXml as $typeXml) {
            
    $typesList[] = (string)$typeXml;
        }

        
    $typesList array_unique($typesList);

        
    $moviesForType = array();
        foreach (
    $typesList as $type)
        {
            echo 
    "<div class='wrapper'>";
            echo 
    "<h3 class='hp-1'>$type</h3>";
            
    $rawData $xml->xpath('service[category="' $type '"]');
            if (!empty(
    $rawData))
            {
                foreach (
    $rawData as $rawMovie)
                {
                    
    $moviesForType[$type][] = $rawMovie->subcat;
                }
            }
            
            
    $moviesForType[$type] = array_unique($moviesForType[$type]);
            
            
    $prods = array();
            
    $count=1;
            foreach(
    $moviesForType[$type] as $key)
            {
                if(
    $count %<> 0)
                {
                    echo 
    "<div class='columns col-padding'>";
                }
                else
                {
                    echo 
    "<div class='columns'>";
                }
                echo 
    "<ul class='price-list'>";
                echo 
    "<li class='header price-item-header bold'>$key</li>";
                
    $serv $xml->xpath('service[subcat="'$key '"]');
                foreach(
    $serv as $serv)
                {
                    echo 
    "<li><a href='#' class='price-item'>$serv->name</a><span class='price'>£$serv->price</span><span class='filler'>&nbsp;</span></li>";
                }
                echo 
    "</ul></div>";
                
    $count++;
            }
            echo 
    "</div>";
        }
    }
    ?>


  •  

    Posting Permissions

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