Jump to content

Searching xml files


bishmedia

Recommended Posts

ok all you phpfreaks, im hoping one of you can help me :-)

 

ive started a simple xml file and php file to read and view as below....

 

<?xml version="1.0" encoding="ISO-8859-1"?>
<datas>
<comp>
<result>
<year>2013</year>
<compName>Butlins Mineworkers</compName>
<bandName>GUS Virtuoso</bandName>
<position>1st</position>
<points>2</points>
</result>
<result>
<year>2013</year>
<compName>Butlins Mineworkers</compName>
<bandName>GUS Virtuoso</bandName>
<position>2nd</position>
<points>4</points>
</result>
<result>
<year>2013</year>
<compName>Butlins Mineworkers</compName>
<bandName>Carlton Main Frickley Colliery</bandName>
<position>3rd</position>
<points>6</points>
</result>
</comp>
</datas>

 

 

<?php
$xml = simplexml_load_file("xmltestdata.xml")
or die("Error: Cannot create object");

foreach($xml->children() as $result){
foreach($result->children() as $result => $data){
echo $data->year;
echo $data->compName;
echo $data->bandName;
echo $data->position;
echo $data->points;
echo "<br />";
}
}

?>

 

In time the xml will grow and my question is, what if a user wanted to view results for a specific band 'bandName' via say a 'Drop Down Box??

 

I do hope theres someone out there in cyber who can help

 

Many Thanks in advance.

 

Bish

Link to comment
https://forums.phpfreaks.com/topic/273978-searching-xml-files/
Share on other sites

<?php
$xml = simplexml_load_file('xmltestdata.xml');

$search = 'GUS Virtuoso';

$results = $xml->xpath("//result[bandName='$search']");

foreach ($results as $res) {
   echo "$res->compName $res->year $res->position <br>";
}

RESULTS

Butlins Mineworkers 2013 1st
Butlins Mineworkers 2013 2nd

Link to comment
https://forums.phpfreaks.com/topic/273978-searching-xml-files/#findComment-1409845
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.