NYCPHP Meetup

NYPHP.org

[nycphp-talk] PHP4 Array/XML sorting question

Peter Sawczynec ps at pswebcode.com
Tue Apr 11 14:34:23 EDT 2006


I believe, XSL Transformations (XSLT) is a correct method for achieving your
end, i.e. use XSL to format and sort the XML.
PHP 4.1+ XSLT library will let you do roughly so...

$xsltHnd = xslt_create();
xslt_set_base($xsltHnd,'');
$html =  xslt_process($xsltHnd, 'my_listings.xml',
'my_listings_format.xsl');
echo $html;


Above, the XML param is your XML file. The XSL file contains a mixture of
XHTML, CSS and XSL.
The XSL allows you to sort and format XML quite effectively.

Your XSL file will contain code like the following snippets:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" omit-xml-declaration="yes"/>

<xsl:template match="/ads">
  <xsl:apply-templates select="ad">
    <xsl:sort select="category"/>
  </xsl:apply-templates>
</xsl:template>


<xsl:template match="ad">
<xsl:choose>
	<xsl:when test="url[.!='']">	
		<td align="left" height="28" valign="top">
		<xsl:element name="a">
			<xsl:attribute name="href"><xsl:value-of
select="@url"/></xsl:attribute>
				<xsl:element name="img">
					<xsl:attribute
name="src">images/ads/<xsl:text></xsl:text><xsl:value-of
select="title"/></xsl:attribute>
					<xsl:attribute
name="border">0</xsl:attribute>
				</xsl:element>
		</xsl:element>
		</td>
	</xsl:when>
	<xsl:otherwise>
		<td align="left" height="28" valign="top">
		<img src="../images/spacer.gif" />
		</td>
	</xsl:otherwise>
</xsl:choose>
</xsl:template> 


</xsl:stylesheet>


Above shows very roughly the technique for using XSL to sort XML and then
push the XML data into some XHTML to create a hyperlinked thumbnail image
for display in the browser. 

Now that is just to give you an idea. 

This technique, of course, has a learning curve. You need to look up PHP
XML/XSLT tutorials and samples on the web and get familiar with XSL in
general. Particularly you need to grasp XSL templates and how to apply to
XML data.

Again, this is just one avenue but once you got it, you can apply this
approach on many tasks with few changes.

Otherwise, look at PHP array sort techniques at:
http://us2.php.net/manual/en/ref.array.php

Warmest regards,
 
Peter Sawczynec,
Technology Director
PSWebcode
_Design & Interface
_Ecommerce
_Database Management
ps at pswebcode.com
718.796.1951
www.pswebcode.com



-----Original Message-----
From: talk-bounces at lists.nyphp.org [mailto:talk-bounces at lists.nyphp.org] On
Behalf Of Tim Boyden
Sent: Tuesday, April 11, 2006 1:29 PM
To: talk at lists.nyphp.org
Subject: [nycphp-talk] PHP4 Array/XML sorting question


Hello,

I am working on a web page that lists some ads stored in an XML file and I'm
having trouble with figuring out how to sort the listings by their
"category" element. Currently I pull the "ad" elements from the XML into an
array and print out the HTML list of ads in a non-sorted manner. I'd like it
to print out with a header for the category followed by a list of ads in
that category. I'm not sure how to code it though. Any help would be
appreciated. Below is a snippet from the XML file:

<?xml version="1.0" encoding="iso-8859-1"?>
<ads>
<ad>
    <franchise>test</franchise>
    <category>Food</category>
    <title>Test 1</title>
    <url>testpage1.pdf</url>
</ad>
<ad>
    <franchise>test</franchise>
    <category>Home Improvement</category>
    <title>Test 2</title>
    <url>testpage2.pdf</url>
</ad>
<ad>
    <franchise>test</franchise>
    <category>Services</category>
    <title>Test 3</title>
    <url>testpage3.pdf</url>
</ad>
</ads>

Thanks,

Tim Boyden

--------------------------- 
Timothy Boyden 
Network Administrator 
tboyden at supercoups.com 
  
SuperCoupsR | 350 Revolutionary Drive | E. Taunton, MA 02718 
508-977-2034  | www.supercoups.com 
--------------------------- 
Local Coupons. Super Savings.R




More information about the talk mailing list