Web Analytics Made Easy -
StatCounter help manipulating an xml file to return unique values - CodingForum

Announcement

Collapse
No announcement yet.

help manipulating an xml file to return unique values

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • help manipulating an xml file to return unique values

    Hi All

    i have an xml file which i am trying to get a list of unique 'binding' field.

    here is my code
    PHP Code:
        $latest = simplexml_load_file('all_dvds.xml');
        foreach ($latest->film as $film)
        {
            $prodID = $film->id;
            $prodName = $film->name;
            $prodBinding = $film->binding;
            $prodReleaseDate = $film->release_date;
            $prodAge= $film->age_rating;
            $prodRRP = $film->rrp;
            $prodIMG = $film->image;
            $prodCheapest = $film->cheapest;
            $cat = array_unique($prodBinding);
            print_r($cat);
            if($store_count ==1)
            {?>
                <div>
                <a href="<?php echo $storeLink?>" title="Click to see related product(s) / retailer and apply code">
                <div class="store_logo"><img src="<?php echo $storeLogo?>" alt="<?php echo $storeName?>" title="<?php echo $storeName?>" height="32" border="0" /></div>
                </a>
                <div class="description"><?php echo $storeDescription?></div>
                </div><?php
                $store_count
    ++;
            }
        }
    ?>
    i tried adding a unique array to the $prodBinding field but as its not an array it doesnt work, whats the correct way do do this?
    many thanks

  • #2
    how about:

    xml.xml
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <latest>
    	<film>
    		<binding>Goodfellas</binding>
    	</film>
    	<film>
    		<binding>Donny Brasco</binding>
    	</film>
    	<film>
    		<binding>The Godfather</binding>
    	</film>
    	<film>
    		<binding>Donny Brasco</binding>
    	</film>
    	<film>
    		<binding>The Godfather</binding>
    	</film>
    </latest>
    PHP Code:
    <?php
        $latest 
    simplexml_load_file('xml.xml');
        
    $arr = array();
        foreach (
    $latest->film as $film){
            
    $prodBinding $film->binding;
            
    array_push($arr$prodBinding);
        }
        
    print_r(array_unique($arr)); 
    ?>
    Stop making things so hard on yourself.
    i is tugbucket :: help raise tugburg :: Whitehaven Kiwanis

    Comment


    • #3
      thanks mate will give it a go

      Comment


      • #4
        ok i need so help with generating my xml file,

        i have a sql query which pulls out data from several db tables
        PHP Code:
        SELECT dvd.filmIDdvd.filmNamedvd.filmThumbIMGdvd.filmBindingdvd.filmReleaseDate
                       
        filmAgeRatingfilmTrailerfilmRRPformat.prodIDformat.formatID
                       
        COUNTpop.prodID ) AS popularity
                        FROM tbl_dvds 
        AS dvd 
                        INNER JOIN tbl_product_format 
        AS format ON format.prodID dvd.filmID
                        LEFT JOIN  tbl_popularity 
        AS pop ON pop.prodID dvd.filmID
                        GROUP BY dvd
        .filmIDdvd.filmNamedvd.filmThumbIMGdvd.filmBindingdvd.filmReleaseDate
                         
        filmAgeRatingfilmTrailerfilmRRPformat.prodIDformat.formatID
                ORDER BY filmName ASC 
        which works great, the only problem is it that some films get returned twice because they have more than 1 formatID associated with them for example

        its returing something like this
        prodID->title->binding->formatID
        0001->my dvd title->dvd->1
        0002->my dvd title 2->dvd->1
        0002->my dvd title 2->dvd->2

        0003->my dvd title 3->dvd->1

        so for each new format there is a new row returned, which is fine for the query but when generating my xml file i dont want several new items added for each format just the one row with each of the different formats added, something like

        <prod>
        <prodID>0002</prodID>
        <title>my dvd title 2</title>
        <binding>dvd</binding>
        <format_group>
        <format>1</format>
        <format>2</format>
        </format_group>
        </prod>

        but im not sure how to add the formats like this,

        here is my current code(which adds a new item for each row returned)
        PHP Code:
                $file= fopen("all_dvds.xml", "w");
                $_xml ="<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\r\n";
                 
        $_xml .="<site>\r\n";
                
                        
        $query mysql_query("
                        SELECT dvd.filmID, dvd.filmName, dvd.filmThumbIMG, dvd.filmBinding, dvd.filmReleaseDate, 
                       filmAgeRating, filmTrailer, filmRRP, format.prodID, format.formatID, 
                       COUNT( pop.prodID ) AS popularity
                        FROM tbl_dvds AS dvd 
                        INNER JOIN tbl_product_format AS format ON format.prodID = dvd.filmID
                        LEFT JOIN  tbl_popularity AS pop ON pop.prodID = dvd.filmID
                        GROUP BY dvd.filmID, dvd.filmName, dvd.filmThumbIMG, dvd.filmBinding, dvd.filmReleaseDate, 
                         filmAgeRating, filmTrailer, filmRRP, format.prodID, format.formatID
                ORDER BY filmName ASC"
        ) or die (mysql_error());
                        while(
        $row mysql_fetch_array($query))
                        {
                            
        $_xml .="\t<film>\r\n";
                            
        $_xml .="\t<id>" $row["filmID"] . "</id>\r\n";
                            
        $_xml .="\t<title>" htmlspecialchars($row["filmName"]) . "</title>\r\n";
                            
        $_xml .="\t<binding>" $row["filmBinding"] . "</binding>\r\n";
                            
        $_xml .="\t<image>" $row["filmThumbIMG"] . "</image>\r\n";
                            
        $_xml .="\t<release_date>" $row["filmReleaseDate"] . "</release_date>\r\n";
                            
        $_xml .="\t<age_rating>" $row["filmAgeRating"] . "</age_rating>\r\n";
                            
        $_xml .="\t<trailer>" $row["filmTrailer"] . "</trailer>\r\n";
                            
        $_xml .="\t<rrp>" $row["filmRRP"] . "</rrp>\r\n";
                            
        $_xml .="\t<popularity>" $row["popularity"] . "</popularity>\r\n";
                            
        $_xml .="\t</film>\r\n";
                        }
                        
                        
        $_xml .="\t</site>\r\n";
                        
        fwrite($file$_xml);

                         
        fclose($file);
        any help is greatly appreciated

        Comment


        • #5
          Sorry, I tried and failed.

          My idea was to blow them in to arrays and then loop through them and merge where needed. this is as far as got:

          Code:
          /**
          * from: http://www.2mknetwork.net/11/how-to-save-mysql-query-as-array.html
          */
          function queryListCategory(){
          	$qlcat=mysql_query("SELECT prodID, prodTitle, prodFormat, prodBinding FROM dvds ORDER BY prodID ASC") or die(mysql_error());
          	$i=0;
          	$fnum=mysql_num_fields($qlcat);
          	while($cat=mysql_fetch_array($qlcat)){
          		for($x=0;$x<$fnum;$x++){
          			$lcat[$i][mysql_field_name($qlcat, $x)]=$cat[mysql_field_name($qlcat, $x)];
          		}
          		$i++;
          	}
          	return $lcat;
          }
          
          $arrays = queryListCategory();
          
          // $arrays is your array
          $r=array();
          
          foreach($arrays as $v){
              $r[$v['prodID']][]=array('prodID'=>$v['prodID'], 'prodTitle'=>$v['prodTitle'], 'prodFormat'=>$v['prodFormat'], 'prodBinding'=>$v['prodBinding']);
          }
          $count = 0;
          foreach($r as $rs){
          	$count++;
          	$merged_items = call_user_func_array('array_merge_recursive', $rs);
          	foreach($merged_items as $ps){
          		if(count($ps) > 1){
          		}
          	}
          	print_r($merged_items);
          }
          
          //print_r($r);
          I used a lot simpler DB for testing, my insert:

          Code:
          mysql_query("INSERT INTO dvds (prodID, prodTitle, prodFormat, prodBinding) VALUES ('0001', 'The Godfather', '1', 'dvd')");
          mysql_query("INSERT INTO dvds (prodID, prodTitle, prodFormat, prodBinding) VALUES ('0001', 'The Godfather', '2', 'dvd')");
          mysql_query("INSERT INTO dvds (prodID, prodTitle, prodFormat, prodBinding) VALUES ('0002', 'Goodfellas', '1', 'dvd')");
          mysql_query("INSERT INTO dvds (prodID, prodTitle, prodFormat, prodBinding) VALUES ('0002', 'Goodfellas', '2', 'dvd')");
          mysql_query("INSERT INTO dvds (prodID, prodTitle, prodFormat, prodBinding) VALUES ('0003', 'Donny Brasco', '1', 'dvd')");
          mysql_query("INSERT INTO dvds (prodID, prodTitle, prodFormat, prodBinding) VALUES ('0004', 'Scarface', '2', 'dvd')");
          mysql_query("INSERT INTO dvds (prodID, prodTitle, prodFormat, prodBinding) VALUES ('0001', 'The Godfather', '3', 'vhs')");
          Stop making things so hard on yourself.
          i is tugbucket :: help raise tugburg :: Whitehaven Kiwanis

          Comment


          • #6
            Code:
            Array
            (
                [prodID] => Array
                    (
                        [0] => 0001
                        [1] => 0001
                        [2] => 0001
                    )
            
                [prodTitle] => Array
                    (
                        [0] => The Godfather
                        [1] => The Godfather
                        [2] => The Godfather
                    )
            
                [prodFormat] => Array
                    (
                        [0] => 1
                        [1] => 2
                        [2] => 3
                    )
            
                [prodBinding] => Array
                    (
                        [0] => dvd
                        [1] => dvd
                        [2] => vhs
                    )
            
            )
            Array
            (
                [prodID] => Array
                    (
                        [0] => 0002
                        [1] => 0002
                    )
            
                [prodTitle] => Array
                    (
                        [0] => Goodfellas
                        [1] => Goodfellas
                    )
            
                [prodFormat] => Array
                    (
                        [0] => 1
                        [1] => 2
                    )
            
                [prodBinding] => Array
                    (
                        [0] => dvd
                        [1] => dvd
                    )
            
            )
            Array
            (
                [prodID] => 0003
                [prodTitle] => Donny Brasco
                [prodFormat] => 1
                [prodBinding] => dvd
            )
            Array
            (
                [prodID] => 0004
                [prodTitle] => Scarface
                [prodFormat] => 2
                [prodBinding] => dvd
            )
            that's farthest I got. anything else i tried started to destroy the already "good to go" arrays.

            I wanted to implode like key values with a comma and the run an explode when it cam time to create the XML.
            Stop making things so hard on yourself.
            i is tugbucket :: help raise tugburg :: Whitehaven Kiwanis

            Comment


            • #7
              hm ok thanks for giving it a go mate, there must be a way of doing this, just gotta find the solution

              appreciate your help
              Luke

              Comment

              Working...
              X