Help with filtering my XML feed

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
fanzine
Forum Newbie
Posts: 5
Joined: Mon Apr 27, 2009 11:42 am

Help with filtering my XML feed

Post by fanzine »

Hi, I am relatively new to php, and am in need of some basic xml filtering assistance.
I am storing some XML element values in an array, and I would like to be able to filter these before I output the html.
I would like to have a reusable method of filtering and outputting html where I can pass in a keyword and a corresponding xml element.
For example:
  • Where the xml element "title" contains the string "t-shirt"
  • or as a seperate query where the xml element "category" is equal to the string "t-shirt"
Finally when I output the html, I would like to have an alternating css class name for the container div. So that I can apply different styles for all the resulting product matches. And I need to be able to control the number of results.

I am currently reading my xml document using the code below. Thanks for your help with this.

Code: Select all

<?php
 
class RSSFeed {
 
    public $items = array();
 
    public function addFeed($rss_url) {
        $xml = simplexml_load_file($rss_url);
        foreach($xml->channel->item as $Item) {
            $this->Items[] = array( "title" => $Item->title,
                                    "link" => $Item->link,
                                    "description" => $Item->description,
                                    "guid" => $Item->guid,
                                    "price" => $Item->price,
                                    "category" => $Item->category,
                                    "image" => $Item->image,
                                    "range" => $Item->range,
                                    );
        }
        sort($this->Items);
        
    }
    
 
    
    public function exportAsHTML() {
        foreach($this->Items as $Item) {
            echo '<div>
                       <div><img src="'.$Item["image"].'"></div>
                   <div>&pound;'.$Item["price"].'</div>
                   <div><a href="'.$Item["link"].'">'.$Item["title"].'</a></div>
                  </div>';
            
            
            
            
        }
    }
 
}
 
$rss = new RSSFeed();
$rss->addFeed("http://localhost/rss");
$rss->exportAsHTML();
 
?>
My xml looks like this.

Code: Select all

<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
 
  <channel>
    <title>Personalised Baby Gifts RSS Feed</title>
    <description> BESPOKE T-SHIRT FOR NEW BABIES TO KEEP FOREVER WITH THEIR NAME AND BIRTH WEIGHT TO REMIND YOU HOW SMALL THEY WERE.</description>
    <link></link>
    <lastBuildDate>Thurs, 23 Apr 2009 11:12:55 -0100 </lastBuildDate>
    <pubDate>Mon, 27 Apr 2009 18:10:10 +0100</pubDate>
    <image url="/images/top.jpg" link=""></image>
    
    <item>
      <title>Sporty T</title>
      <category>t-shirts</category>
      <description>A personalised baby T-shirt made from soft 100% organic cotton in natural colour with shoulder poppers, they are available in two sizes. 
SIZE 1 for small newborn babies up to 8lbs/4kg. SIZE 2 for big newborn babies up to 14lbs/7kg or babies 1-3 months.</description>
      <link>/baby-t-shirts/index.php?type=sporty</link>
      <guid>TC1</guid>
      <range>sporty1</range>
      <colour>green</colour>
      <price>15.00</price>
      <gender>boy</gender>
      <image>/images/t_popup_sportA1.jpg</image>
    </item>
    
    <item>
      <title>Sporty T</title>
      <category>t-shirts</category>
      <description>A personalised baby T-shirt made from soft 100% organic cotton in natural colour with shoulder poppers, they are available in two sizes. 
SIZE 1 for small newborn babies up to 8lbs/4kg. SIZE 2 for big newborn babies up to 14lbs/7kg or babies 1-3 months.</description>
      <link>/baby-t-shirts/index.php?type=sporty</link>
      <price>15.00</price>
      <guid>TD1</guid>
      <range>sportyt</range>
      <colour>red</colour>
      <gender>girl</gender>
      <image>/images/t_popup_sportA2.jpg</image>
   </item>
  </channel>
</rss>
Last edited by Benjamin on Mon Apr 27, 2009 8:11 pm, edited 1 time in total.
Reason: Changed code type from text to php, xml.
Yossarian
Forum Contributor
Posts: 101
Joined: Fri Jun 30, 2006 4:43 am

Re: Help with filtering my XML feed

Post by Yossarian »

Where the xml element "title" contains the string "t-shirt"
What is that filter supposed to do? Only show things that match? Filter out matches, or be clever enuf to do both? Or are you planning on highlighting matches in some way?

Does the original class have to remain the same - are you using it on other applications?

Highlighting every other "container", is that an optional thing which applies to each feed or to each feed item?

<div class=odd>
<all products from feed A in here>
</div>
<div class=even>
<all products from feed B in here>
</div>

OR do you mean
<div><-root of feed A
<div class=odd ><product one>
..
<div class=even ><product two>
..
<div class=odd ><product three>
..
<div class=even ><product four>
..
</div>
fanzine
Forum Newbie
Posts: 5
Joined: Mon Apr 27, 2009 11:42 am

Re: Help with filtering my XML feed

Post by fanzine »

Hi Yossarian,
I am looking to show items that match the query.
I can't think of an instance where I would want to do both.
This is the basis of my page so the class names can change.
The alternating class names refer to each item in a list of items.There is only one feed.

thanks for taking the time to look at this.

Fanzine
Yossarian
Forum Contributor
Posts: 101
Joined: Fri Jun 30, 2006 4:43 am

Re: Help with filtering my XML feed

Post by Yossarian »

HL every other item, Use a conditional check,

Code: Select all

 
     public function exportAsHTML( $alt= 0 ) {
         $ctr = 1;  // set a counter
 
         foreach($this->Items as $Item) {
 
             if( $alt ){
                  ++$ctr ;  // add one to it
                   echo ( $ctr % 2 )? '<div class="odd">' : '<div class="even">' ;
 
             }else{
                  echo '<div>';  // just do an ordinary div
 
            }
 
             echo '<div><img src="'.$Item["image"].'"></div>
                    <div>&pound;'.$Item["price"].'</div>
                    <div><a href="'.$Item["link"].'">'.$Item["title"].'</a></div>
                   </div>';
 }
 
Tested most of it, use it like so;

$d->exportAsHTML(1); // for alternating class declarations
$d->exportAsHTML(); // as normal

now you see how a conditional works, take as stab at trapping something in the xml file.
Last edited by Benjamin on Mon Apr 27, 2009 8:12 pm, edited 1 time in total.
Reason: Changed code type from text to php.
fanzine
Forum Newbie
Posts: 5
Joined: Mon Apr 27, 2009 11:42 am

Re: Help with filtering my XML feed

Post by fanzine »

Thanks Yossarian,

The alternating html is working exactly as i want, thank you.
I am not sure how to query a particular element in the xml. Could you advise on how I can check for a particular string in an element.
Yossarian
Forum Contributor
Posts: 101
Joined: Fri Jun 30, 2006 4:43 am

Re: Help with filtering my XML feed

Post by Yossarian »

Got to dash out, sorry.
You already have an array of items in your class, just make another method.

Code: Select all

 
removeItems( $attribute, $name ) {
 
  if ( $item[$attribute] == $name ) {
  // set the item to null or remove it
 
  }
}
 
removeExcept( $attribute, $name ) {
 
  if ( $item[$attribute] != $name ) {
  // set the item to null or remove it
 
  }
}
 
$d->removeExcept( 'title' , 'T-shirt') ;
$d->exportAsHTML(1) ;

etc
Last edited by Benjamin on Tue Apr 28, 2009 1:31 pm, edited 1 time in total.
Reason: Changed code type from text to php.
fanzine
Forum Newbie
Posts: 5
Joined: Mon Apr 27, 2009 11:42 am

Re: Help with filtering my XML feed

Post by fanzine »

Hi Yossarian,
I am struggling to add the new method.
I have the alternating divs working exactly I had hoped. But icannot see where I should be adding the new removeExcept method. Would you please advice? Thanks.
My code is now as follows:

Code: Select all

<?php
 
class RSSFeed {
 
    public $items = array();
 
    public function addFeed($rss_url) {
        $xml = simplexml_load_file($rss_url);
        foreach($xml->channel->item as $Item) {
            $this->Items[] = array( "title" => $Item->title,
                                    "link" => $Item->link,
                                    "description" => $Item->description,
                                    "guid" => $Item->guid,
                                    "price" => $Item->price,
                                    "category" => $Item->category,
                                    "image" => $Item->image,
                                    "range" => $Item->range,
                                    );
        }
        sort($this->Items);
        
    }
    
 
    
        public function exportAsHTML( $alt= 0 ) {
     
      $ctr = 1;  // set a counter
        foreach($this->Items as $Item) {
        
          if( $alt ){
                  ++$ctr ;  // add one to it
                   echo ( $ctr % 2 )? '<div class="productbox2">' : '<div class="productbox">' ;
 
             }else{
                  echo '<div class="productbox">';  // just do an ordinary div
 
            }
 
          
            echo '
                   <div class="pattern"><a href="'.$Item["link"].'"><img src="'.$Item["image"].'"></a></div>
                   <div class="price">&pound;'.$Item["price"].'</div>
                   <div class="title"><a href="'.$Item["link"].'">'.$Item["title"].'</a></div>
                  </div>
                  ';
            
            
            
            
        }
    }
 
}
 
$rss = new RSSFeed();
$rss->addFeed("http://localhost/rss");
$rss->exportAsHTML(1);
 
?>
Last edited by Benjamin on Tue Apr 28, 2009 1:32 pm, edited 1 time in total.
Reason: Changed code type from text to php.
Yossarian
Forum Contributor
Posts: 101
Joined: Fri Jun 30, 2006 4:43 am

Re: Help with filtering my XML feed

Post by Yossarian »

Code: Select all

 
<?php
 
class RSSFeed {
 
    public $items = array();
 
    public function addFeed($rss_url) {
   ...     
    }
   
  
   public function exportAsHTML( $alt= 0 ) {
   ... 
   }
 
   public function removeAttribute( $attribute, $name ){
   ...
   }
 
   public function allowAttribute( $attribute, $name ){
   ...
   }
 
}
removeAttribute() and allowAttribute() or just plain remove() and allow() might be slightly less confusing method names.
fanzine
Forum Newbie
Posts: 5
Joined: Mon Apr 27, 2009 11:42 am

Re: Help with filtering my XML feed

Post by fanzine »

Hi Yossarian,
I am getting an undefined variable:item error on the line

Code: Select all

 if ( $item[$attribute] != $name ) {
here is my full code

Code: Select all

<?php
 
class RSSFeed {
 
    public $items = array();
 
    public function addFeed($rss_url) { 
    
    
    
        $xml = simplexml_load_file($rss_url);
        foreach($xml->channel->item as $Item) {
            $this->Items[] = array( "title" => $Item->title,
                                    "description" => $Item->description,
                                    "guid" => $Item->guid,
                                    "price" => $Item->price,
                                    "category" => $Item->category,
                                    "range" => $Item->range,
                                    
                                    );
                                    
                                    
        }
        
        
        
        
        
    }
    
    
    public function exportAsHTML( $alt= 0 ) {
     
      $ctr = 1;  // set a counter
        foreach($this->Items as $Item) {
        
          if( $alt ){
                  ++$ctr ;  // add one to it
                   echo ( $ctr % 2 )? '<div class="productbox '.$Item["category"].'2">' : '<div class="productbox '.$Item["category"].'">' ;
 
             }else{
                  echo '<div class="productbox '.$Item["category"].'">';  // just do an ordinary div
 
            }
 
          
            echo '
                   <div class="pattern"><a href="'.$Item["category"].'.php?range='.$Item["range"].'"><img src="/dinky/personalised-images/'.$Item["category"].'/'.$Item["guid"].'.gif" alt="'.$Item["title"].'" border="0"></a></div>
                   <div class="price">&pound;'.$Item["price"].'</div>
                   <div class="title"><a href="'.$Item["category"].'.php?range='.$Item["range"].'">'.$Item["title"].'</a></div>
                  </div>
                  ';
            
            
            
            
        }
    }
 
    public function removeAttribute( $attribute, $name ){
      if ( $item[$attribute] != $name ) {
     // set the item to null or remove it
 
        }
 
    }
 
    
    
    
    
}
 
$rss = new RSSFeed();
$rss->addFeed("http://localhost/dinky/rss");
$rss->removeAttribute('title','T-shirt') ;
$rss->exportAsHTML(1);
 
 
 
?>
Last edited by Benjamin on Tue Apr 28, 2009 1:33 pm, edited 1 time in total.
Reason: Changed code type from text to php.
Yossarian
Forum Contributor
Posts: 101
Joined: Fri Jun 30, 2006 4:43 am

Re: Help with filtering my XML feed

Post by Yossarian »

You should be working on the array "$this->items" I think.

You probably need to seek out an intro to OOP tutorial, have a bit of a read up.

Use real values when you are developing, just get something to work and itll drive you on.

if( $this->items['title'] == "Sporty T" )
//remove it or whatever
Post Reply