How to sort an ARRAY in a DOM doc?

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
User avatar
becky-atlanta
Forum Commoner
Posts: 74
Joined: Thu Feb 26, 2009 6:26 pm
Location: Atlanta, GA

How to sort an ARRAY in a DOM doc?

Post by becky-atlanta »

I am able to put the XML data into an array. I want the output be sorted by the number (value). The XML file look like this:

[text]
<item>
<couponid>16130394</couponid>
<link>http://www.coupons.com/alink.asp?go=139 ... =yes</link>
<description>$0.75 off Kraft 2% Milk Cheese Singles</description>
<image>http://cdn.coupons.com/insight.coupons. ... gif</image>
<activedate>4/1/2011</activedate>
<shutoff>6/30/2011 11:59:00 PM</shutoff>
<expiration>6/30/2011 11:59:00 PM</expiration>
<majorCategory>Foods</majorCategory>
<minorCategory>Cheese</minorCategory>
<brand>KRAFT Singles</brand>
<value>0.75</value>
<geotarget>0</geotarget>
</item>
[/text]

My code is like this:

Code: Select all

<table width="100%" border="0">
<?php
        $doc = new DOMDocument();
        $doc->load('http://rss.coupons.com/xmlserve.asp?go=13903xh2010&bid=136610001');
        $arrFeeds = array();
        foreach ($doc->getElementsByTagName('item') as $node) {
                $itemRSS = array (
                        'val' => $node->getElementsByTagName('value')->item(0)->nodeValue,
                        'image' => $node->getElementsByTagName('image')->item(0)->nodeValue,
                        'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
                        'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
                        'exp' => $node->getElementsByTagName('expiration')->item(0)->nodeValue
                        );
                array_push($arrFeeds, $itemRSS);
                $expdate = explode(" ", $itemRSS["exp"]);
        echo "<tr>";
        echo "<td>";
        echo "<a href=\"".$itemRSS["link"]."\" class=\"print-title\" target=\"_blank\">";
        echo "<img align=\"left\" src=\"".$itemRSS["image"]."\" width=\"50\" hspace=\"10\">";
        echo $itemRSS["desc"];
        echo "<br> Expire: ";
        echo $expdate[0];      
        echo "</a>";
        echo "</td>";
        echo "</tr>";
        }
?>
</table>


I have looked at many examples for days and could not figure out how. Can it be sorted with a simple PHP built-in function?

Thank you for your help!

Becky
klandaika
Forum Newbie
Posts: 19
Joined: Wed Mar 30, 2011 3:25 pm
Location: New York

Re: How to sort an ARRAY in a DOM doc?

Post by klandaika »

Code: Select all

<table width="100%" border="0">
<?php
function compareFeeds($a, $b){ //this function will compare the "val" array element of each $itemRSS 
	if ($a["val"] == $b["val"]) {
        return 0;
    }
    return ($a["val"] < $b["val"]) ? -1 : 1;	
}

        $doc = new DOMDocument();
        $doc->load('http://rss.coupons.com/xmlserve.asp?go=13903xh2010&bid=136610001');
        $arrFeeds = array();
        foreach ($doc->getElementsByTagName('item') as $node) {
                $itemRSS = array (
                        'val' => $node->getElementsByTagName('value')->item(0)->nodeValue,
                        'image' => $node->getElementsByTagName('image')->item(0)->nodeValue,
                        'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
                        'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
                        'exp' => $node->getElementsByTagName('expiration')->item(0)->nodeValue
                        );
                array_push($arrFeeds, $itemRSS);
		}
		usort($arrFeeds, compareFeeds);
		
foreach($arrFeeds as $itemRSS){
       $expdate = explode(" ", $itemRSS["exp"]);
        echo "<tr>";
        echo "<td>";
        echo "<a href=\"".$itemRSS["link"]."\" class=\"print-title\" target=\"_blank\">";
        echo "<img align=\"left\" src=\"".$itemRSS["image"]."\" width=\"50\" hspace=\"10\">";
        echo $itemRSS["desc"];
        echo "<br> Expire: ";
        echo $expdate[0];      
        echo "</a>";
        echo "</td>";
        echo "</tr>";
}
?>
</table>
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: How to sort an ARRAY in a DOM doc?

Post by McInfo »

klandaika wrote:

Code: Select all

usort($arrFeeds, compareFeeds);
There should be quotes around compareFeeds because it is supposed to be a string. Without quotes, it is an undefined constant that will trigger an E_NOTICE error.

Code: Select all

usort($arrFeeds, 'compareFeeds');
klandaika
Forum Newbie
Posts: 19
Joined: Wed Mar 30, 2011 3:25 pm
Location: New York

Re: How to sort an ARRAY in a DOM doc?

Post by klandaika »

Sorry, was rushing.
McInfo is correct, there should be quotes.
User avatar
becky-atlanta
Forum Commoner
Posts: 74
Joined: Thu Feb 26, 2009 6:26 pm
Location: Atlanta, GA

Re: How to sort an ARRAY in a DOM doc?

Post by becky-atlanta »

Thank you to both of you! It works great. Besides solving my problem, it is also a great lesson for me to learn. ARRAY and Function is always hard for me to understand. THANK YOU AGAIN!!!
alyssasmommy
Forum Newbie
Posts: 1
Joined: Thu Dec 20, 2012 9:42 pm

Re: How to sort an ARRAY in a DOM doc?

Post by alyssasmommy »

After spending countless hours and days trying to figure this out I came across this post. I am trying to do the same thing, but there's just not enough details for me here lol Sorry I'm not to familiar with php, xml, etc. I also have an affiliate link through coupons.com, my smlserve.asp file gives me errors when trying to upload thru a feed reader. Can anyone please explain where I need to put this php code and how to call the php code to a slide show widget or feed reader? Something that will deplay the images with links...
Post Reply