Only echo/print if xml element exists

Greetings all!
I’ve scoured google and can’t really find much help w/ this, though it seems simple enough.
I have an XML document that contains a series of coupons.
My PHP code is using SimpleXML to access and parse the XML, then output as HTML content. It loops through each coupon element and prints out it’s contents.
However, some of the coupons have multiple deals, addresses, or other info. For example, while all of them contain the xml element <deal>. . .</deal>, only a few contain the XML element <deal2>. . .</deal2>.
How can I check for this and only print out the needed HTML only if the XML contains the elements necessary?
Currently, I’m printing off multiple deals in the HTML, but they’re blank unless the XML contains some info for them. I want to not even print out those elements if nothing is there.
My PHP code looks like this, I’m using the id to pin-point nodes and make stuff happen, so maybe that can be incorporated in the solution?:


foreach($coupons->coupon as $coupon) {
      $id = $coupon->attributes()->id;

echo "
<div class=\\"coupon\\">
<a name=\\"$id\\" id=\\"$id\\"></a>
<input type=\\"hidden\\" name=\\"_submit_check\\" value=\\"$id\\"/>
<h4 class=\\"expires\\" title=\\"Expiration Date\\">Expires On:<br />$coupon->expiration</h4> 				
				
<h4><em title=\\"Category\\">$coupon->category</em>
<span title=\\"Add to Print Que\\">
				
<input name=\\"$id\\" type=\\"checkbox\\" value=\\"coupon_$id\\" title=\\"Add to Print Que\\" onClick=\\"javascript:pageTracker._trackPageview('$Company4Analyltics');\\" />
Add Coupon #$id  to my print que</span>
</h4>

<h1><a href=\\"Print-single.php?coupon=$id\\" onClick=\\"javascript:pageTracker._trackPageview('$Company4Analyltics');\\ target=\\"_blank\\" title=\\"Print This Deal\\">$coupon->company</a></h1>
<div class=\\"deals\\">
<img src='../data/assets/h1-black-bigK-small.png' id='KDeals-Left' alt='' />
<img src='../data/assets/h1-black-bigK-small.png' id='KDeals-Right' alt='' />
<h2 title=\\"What a Deal!\\"><strong>$coupon->deal</strong></h2>
<h2>$coupon->deal2</h2>
<h2>$coupon->deal3</h2>
<h2>$coupon->deal4</h2>
<h2>$coupon->deal5</h2>
</div>
<h6 title=\\"Additonal Info\\">$coupon->extra</h6>
<address title=\\"Business Location\\">$coupon->address</address>
<address>$coupon->address2</address>
<address>$coupon->address3</address>
<address>$coupon->address4</address>
<h3 title=\\"Visit Website\\"><a href=\\"http://$coupon->site \\" target=\\"_blank\\">$coupon->site</a></h3>
<h5 title=\\"Business Phone Number\\">$coupon->phone</h5>
<small title=\\"Disclaimer\\">$coupon->legal</small>
<img $coupon->image />
</div>";
      $CouponNumber++; // increment by 1
    }
	#Repeat Print Buttons
    echo   "<form action=\\"Print-multiple.php\\" method=\\"POST\\">
         <img src=\\"../data/assets/print.png\\" alt=\\"\\" class=\\"hidden\\" /> &nbsp; <input type=\\"submit\\" value=\\"Print all of my checked coupons\\" target=\\"_blank\\" /><br /><hr />
</form>";
  } // end else

As you can see, every coupon is being printed out w/ multiple h2 & address elements that are un-necessary b/c there’s no data to populate them.
Dido for the img tag at the end. Not many contain an image, and IE displays a missing image graphic that’s annoying as hell.
I think what I’m asking is simple. I just don’t know how to do it in the middle of the loop, and I’m not skilled enough to figure out how to set it up outside the loop to execute in the loop.
Any help would be greatly appreciated!
Thanks in advance.

Simplest way would be to do something like this:


if($coupon->deal2) {
  echo "<h2>" . $coupon->deal2 . "</h2>";

You just check if it exists and if it does, add the whole thing. You’ll have to break your echo up into multiple pieces, but that’s probably for the best. :wink:

Thanks samanime!
That’s what I thought, however, I should have phrased my question better.
I realized the XML was formatted crazy, so I’ve adjusted each deal to be a child of deals, like this:


<deals>
    <deal>. . .</deal>
    <deal>. . .</deal>
   <deal>. . .</deal>
</deals>

What I’m struggling with is how to execute the operation to determine if there are multiple deals/addresses, and if so to print them. I’m new to PHP, so my understanding is limited. But seems like I’d have to terminate the foreach statement, check and echo multiple deals, then continue the foreach statement. I don’t know how to do that. Would a while() statement fit the bill?
Any advice or direction you can give would be a great help!
Thanks

Okay, I was going about that all wrong. I’ve taken a different approach but still having slight issue.
My XML now contains multiple deals & addresses, and an image tag nested, like this:


<coupon id="1">									
<expiration>	Mar 31, 2011	</expiration>							
<category>	Some Category Name	</category>							
<company>	Some Company Name	</company>							
<deals>									
<deal>	A Deal   </deal>	
<deal>    Another Deal   </deal>
<deal>    And another Deal   </deal>						
		</deals>							
<addresses>									
<address>	Some Address	</address>
<address>	Another Address	</address>
<address>	And another Address	</address>							
		</addresses>							
<phone>	123-123-1234	</phone>							
<legal>	Not valid with any other offer or any other location.	</legal>
<image>  ../images/couponimage.png   </image>						
</coupon>	

And my php code looks like this now:


$XML = '../data/Coupons+.xml';
  if (file_exists($XML)) {
    $coupons = simplexml_load_file($XML);
    $CouponNumber = 1; #Defines the html coupons page starting at 1.

	# Show XML Errors, if any
	libxml_use_internal_errors(true);
	$sxe = $coupons;
	if (!$sxe) {
    echo "Failed loading XML Coupons: <br />\
";
    foreach(libxml_get_errors() as $error) {
        echo "\	", $error->message,' = Line ',$error->line, '<br />';
    	}
	}
	
	#Check if Elements exist, such as deal2. If not, don't print un-needed HTML code.
	/*if ($sxe->getElementsByTagName("image")->length != 0) {
    $foo = $sxe->createElement('image');
    $image->appendChild($foo);
}
*/
	
	#Print coupons
   	echo "<form action=\\"Print-multiple.php\\" method=\\"POST\\">
		<h4 id=\\"Directions\\">Simply check off which coupons you wish to print,<br />then click the button below.</h4>
   		    <img src=\\"../data/assets/print.png\\" alt=\\"\\" class=\\"hidden\\" /> &nbsp; <input type=\\"submit\\" value=\\"Print all of my checked coupons\\" target=\\"_blank\\" /><br /><hr />";
			
	

    foreach($coupons->coupon as $coupon) {
      $id = $coupon->attributes()->id;
	#Remove white space for analytics tracking functions
	$Company = $coupon->company;
 	$sPattern = '/\\s*/m'; #Regular Expression to find spaces
	$sReplace = '';#replace spaces
	$Company4Analyltics = preg_replace( $sPattern, $sReplace, $Company );#return company without spaces
	
	#Check for Mulitple Deals
	$SingleDeal = '';
	$MultipleDeal = '';
	if ($coupon->deals->deal > 1) {
		$MultipleDeals = '<h2>' . $coupon->deals->deal . '</h2>';
	} else {
		$SingleDeal = '<h2 title=\\"What a Deal!\\"><strong>' . $coupon->deals->deal . '</strong></h2>';
	}
	
	#Check for Mulitple Addresses
	$SingleAddress = '';
	$MultipleAddresses = '';
	if ($coupon->addresses->address > 1) {
		$MultipleAddresses = '<address>' . $coupon->addresses->address . '</address>' ;
	} else {
		$SingleAddress = '<address tile=\\"Business Address\\">' . $coupon->addresses->address . '</address>';
	}
	
	#Check for Additional Image Coupon provided by business
	$ImageCoupon = '';
	if ($coupon->image > 0) {
		$ImageCoupon = '<img src=\\"' . $coupon->image . ' \\" alt=\\"' . $coupon->company . ' \\" title=\\"Additional Published Coupon\\" />';
	} else {
		$ImageCoupon = '';
	}

	
      echo "
<div class=\\"coupon\\">
<a name=\\"$id\\" id=\\"$id\\"></a>
<input type=\\"hidden\\" name=\\"_submit_check\\" value=\\"$id\\"/>
<h4 class=\\"expires\\" title=\\"Expiration Date\\">Expires On:<br />$coupon->expiration</h4>
				
<!--	*	Email Friends & Family	*	-->
<a href=\\"Email.php?coupon=$id\\" title=\\"Email this Coupon\\" class=\\"email\\" onClick=\\"javascript:pageTracker._trackPageview('/UserEmailed/$Company4Analyltics');\\">Email it!</a>
				
<h4><em title=\\"Category\\">$coupon->category</em>
<span title=\\"Add to Print Que\\">
				
<!--	* Google Analytics Tracked Checkboxes	*	-->
<input name=\\"$id\\" type=\\"checkbox\\" value=\\"coupon_$id\\" title=\\"Add to Print Que\\" onClick=\\"javascript:pageTracker._trackPageview('$Company4Analyltics');\\" />

Add Coupon #$id  to my print que</span>
</h4>

<h1><a href=\\"Print-single.php?coupon=$id\\" onClick=\\"javascript:pageTracker._trackPageview('$Company4Analyltics');\\ target=\\"_blank\\" title=\\"Print This Deal\\">$coupon->company</a></h1>
<div class=\\"deals\\">
<img src='../data/assets/h1-black-bigK-small.png' id='KDeals-Left' alt='' />
<img src='../data/assets/h1-black-bigK-small.png' id='KDeals-Right' alt='' />
$SingleDeal
$MultipleDeals
</div>
<h6 title=\\"Additonal Info\\">$coupon->extra</h6>
$SingleAddress
$MultipleAddresses
<h3 title=\\"Visit Website\\"><a href=\\"http://$coupon->site \\" target=\\"_blank\\">$coupon->site</a></h3>
<h5 title=\\"Business Phone Number\\">$coupon->phone</h5>
<small title=\\"Disclaimer\\">$coupon->legal</small>
$ImageCoupon
</div>";

There’s a great chance many coupons will have multiple deals, small chance they’ll have multiple addresses, and little chance they’ll have an image.
That being said, with this code, every coupon gets printed with 2 deals, it’s actual deal and a second deal that’s the first coupon’s deal #1, it just repeats in every coupon as a second deal.

Dido for addresses. Image tag’s not even printing, so I’m not sure at all about it.
Any advice would be greatly appreciated!:sick:

Okay, thanks for your help samanime,
I figured out the odd problem I had. Turns out I needed and “echo” after the foreach that was contained in the main foreach.