Page 1 of 1

Selecting the 'Current' XML Record

Posted: Wed Dec 03, 2008 5:20 am
by millsy007
I have php code that uses an XML file to display in an HTML page the 'Next' and 'Previous' pages for that page.

The code currently selects ALL of the XML records and then displays them on the page. What I would like to be able to do is have the HTML only display the next and previous links for the current page.

What would be the best way to do this?

Perhaps Select only where the XML Title = the HTML page title?

:?:

XML

Code: Select all

<?xml version="1.0" encoding="ISO-8859-1"?>
<Listings>
<Count>2</Count>
<Listing>
<Title><![CDATA[Food and Drink]]></Title>
<PREVURI>http://www.page.nl</PREVURI>
<NEXTURI>http://www.page.nl/info/Location.php</NEXTURI>
</Listing>
<Listing>
<Title><![CDATA[Location]]></Title>
<PREVURI>http://www.page.nl/Food-and-Drink/index.php</PREVURI>
<NEXTURI>http://www.page.nl/info/Facilities.php</NEXTURI>
</Listing>
</Listings>
PHP

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
 
<body>
 
 
<?php
 
// read xml file into string
$text = file_get_contents("sample.xml");
 
if(!$text) {
die("ERROR: Unable to read file");
}
 
// parse the xml into structure
$p = xml_parser_create();
xml_parser_set_option($p, XML_OPTION_CASE_FOLDING, 0);
xml_parse_into_struct($p, $text, $vals, $index);
xml_parser_free($p);
 
// read data of interest from structure into array:
for($i=0; $i<$index['Listings'][1]; $i++)
{
foreach(array('Title', 'PREVURI', 'NEXTURI') as $key)
{
$data[$i][$key] = $vals[$index[$key][$i]]['value'];
}
}
 
// output HTML:
foreach($data as $val)
{
echo <<<XML
<a href="{$val['PREVURI']}"><small>PREVIOUS</small></a>
<h3>{$val['Title']}</h3>
<a href="{$val['NEXTURI']}"><small>NEXT</small></a>
<br>
XML;
}
?>
 
</body>
</html>
 

Re: Selecting the 'Current' XML Record

Posted: Thu Dec 04, 2008 8:39 am
by millsy007
I changed line 31 to bring through a particular XML record:

From

Code: Select all

$data[$i][$key] = $vals[$index[$key][$i]]['value'];
To

Code: Select all

$data[$i][$key] = $vals[$index[$key][1]['value'];
But it displays it twice!? I am confused

Re: Selecting the 'Current' XML Record

Posted: Thu Dec 04, 2008 9:18 am
by RobertGonzalez
Why not use SimpleXML? It makes traversing an XML tree super easy.