test for existence of variable id in xml file

I have an xml file that contains a number of similarly named items which relate to features found in a holiday cottage. Only difference is the id:

[code]

​Pub near by​



​Garden​ [/code] I want to test if a varcatitem with a certain id exists in the xml file and if it doesn't then print something. For example one of the variables is Pets Allowed. If it exists then I want to do something eg echo "Pets Allowed" or show an image and if it doesn't exist then I want to do something else eg echo "No Pets" or show a different image.

What do I need to do this? I’m fairly new to this and nothing I try works!
Help gratefully received!

How large are the xml files?

Fairly small…

[php]<?php

$id = ‘variableID24260’;
$data = file_get_contents(‘data.xml’);

if (strpos($data, $id)) {
echo ‘Pets Allowed’;
} else {
echo ‘No Pets’;
}[/php]

Thanks for the reply. It didn’t work but my fault not yours for not supplying enough information.

It’s a wordpress site and I’m trying to amend an old plugin that is no longer supported. The xml file is read in already via a functions file. I’ve made some progress using this:

[php]foreach ($property->variables->varcat->varcatitems->varcatitem as $variablecat):

if($variablecat->variable[“id”] != “24024”)

		{
			$summaryStr .= 'item not found';
			$summaryStr .= 'etc';
			$summaryStr .= '<br />';
		}
		else
		{
			$summaryStr .= 'found';
			$summaryStr .= 'etc...';
			$summaryStr .= '<br />';		
		}
	
	endforeach;[/php]

This at least works and prints out ‘not found’ for each line, however I only want this text once. Can I use a counter within the foreach? If I set a counter to 0 and increment it only if it finds the matching item then can I test for greater than 0 and print my text?

I’ve tried a number of ways but so far not succeeding! I’m learning fast but some help would be greatfully received!

Using an incrementing number should work, you can also break out of loops by adding
[php]break;[/php]

Thanks - I finally got what I wanted by using a counter. Tried break but I couldn’t get it to work. Out of interest, where would I put it in the example below?

Sponsor our Newsletter | Privacy Policy | Terms of Service