Selecting the 'Current' XML Record

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
millsy007
Forum Commoner
Posts: 78
Joined: Wed Jul 02, 2008 7:00 pm

Selecting the 'Current' XML Record

Post 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>
 
millsy007
Forum Commoner
Posts: 78
Joined: Wed Jul 02, 2008 7:00 pm

Re: Selecting the 'Current' XML Record

Post 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
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Selecting the 'Current' XML Record

Post by RobertGonzalez »

Why not use SimpleXML? It makes traversing an XML tree super easy.
Post Reply