Link to home
Start Free TrialLog in
Avatar of Gary
GaryFlag for Ireland

asked on

PHP Loop condition not hitting

Can anyone see something wrong with the below code.
The only conditions met are internet, health (one condition from 2 it should hit) and parking (but only 1 condition from 4)
I know they are not being hit because they just show up in the main list (the else...)


foreach($hotel->PropertyAmenities[0] as $amenities) {
	$amenity=strtolower(trim($amenities->amenity));
	if (strpos($amenity,"number of buildings") ===false && strpos($amenity,"number of rooms")===false && strpos($amenity,"number of floors")===false) {
		if(strpos($amenity,"internet")){
			$internet.='<li>'.$amenities->amenity.'</li>'."\n";
		}
		elseif(strpos($amenity,"parking")){
			$parking.='<li>'.$amenities->amenity.'</li>'."\n";
		}
		elseif(strpos($amenity,"billiards") || strpos($amenity,"pool table") || strpos($amenity,"fitness") || strpos($amenity,"gym") || strpos($amenity,"health")){
			$activities.='<li>'.$amenities->amenity.'</li>'."\n";
		}
		elseif(strpos($amenity,"sauna") || strpos($amenity,"steam room") || strpos($amenity,"swimming") || strpos($amenity,"spa ") || strpos($amenity,"massage")){
			$activities.='<li>'.$amenities->amenity.'</li>'."\n";
		}
		else{
			echo '<li>'.$amenities->amenity.'</li>'."\n";
		}
	}
}

Open in new window


This is from an xml file, sample here
<PropertyAmenities size="51"><PropertyAmenity><amenityId>3184708</amenityId><amenity>24-hour front desk </amenity></PropertyAmenity><PropertyAmenity><amenityId>3871936</amenityId><amenity>Accessible bathroom </amenity></PropertyAmenity><PropertyAmenity><amenityId>1125113</amenityId><amenity>Air-conditioned public areas                    </amenity></PropertyAmenity><PropertyAmenity><amenityId>3184702</amenityId><amenity>Audio-visual equipment </amenity></PropertyAmenity><PropertyAmenity><amenityId>7182368</amenityId><amenity>Babysitting or child care </amenity></PropertyAmenity>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Robert Schutt
Robert Schutt
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I'm with Robert on this one:

$name="chris";

//This will echo 'Not Found' because strpos() returns a position of 0, which also equates to FALSE!
if (strpos($name, "chris")) {
     echo "Found";
} else {
     echo "Not Found";
}

//You need an identical comparison for it to work.
if (strpos($name, "chris") !== FALSE) {
     echo "Found";
} else {
     echo "Not Found";
}

Open in new window

Please see http://www.laprbass.com/RAY_temp_garyc123.php
<?php // RAY_temp_garyc123.php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);


$xml = '<PropertyAmenities size="51"><PropertyAmenity><amenityId>3184708</amenityId><amenity>24-hour front desk </amenity></PropertyAmenity><PropertyAmenity><amenityId>3871936</amenityId><amenity>Accessible bathroom </amenity></PropertyAmenity><PropertyAmenity><amenityId>1125113</amenityId><amenity>Air-conditioned public areas                    </amenity></PropertyAmenity><PropertyAmenity><amenityId>3184702</amenityId><amenity>Audio-visual equipment </amenity></PropertyAmenity><PropertyAmenity><amenityId>7182368</amenityId><amenity>Babysitting or child care </amenity></PropertyAmenity>';
$obj = SimpleXML_Load_String($xml);
var_dump($obj);

Open in new window

It would be enormously useful to us (and therefore also useful to you) if you would prepare the SSCCE example that enables us to try to duplicate the error!  That would include your actual test data - something that we could parse with SimpleXML.  If we get that we can usually show you a code example that demonstrates the correct principles.
Avatar of Gary

ASKER

Absolutely right, just added a space to the beginning of the string to sort it out.
just added a space

It make more sense to do an identical comparison to distinguish between 0 (integer) and 0 (boolean false). Adding a zero seems a little like a hack ;)
Avatar of Gary

ASKER

But adding one space is easier (and makes more sense to me since it is just a string) than adding !== FALSE for each of the 50+ values
OK. If you know you will always be creating the XML files manually and you can guarantee you'll always add a space before the string, then go with what works for you. To me, it looks like this may come back and bite you at some point in the future - I guess you'll cross that bridge when you come to it.