Avatar of Gary
Gary
Flag 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

PHP

Avatar of undefined
Last Comment
Chris Stanyon

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Robert Schutt

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Chris Stanyon

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

Ray Paseur

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.
Gary

ASKER
Absolutely right, just added a space to the beginning of the string to sort it out.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Chris Stanyon

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 ;)
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
Chris Stanyon

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.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.