Porting XML node info into other applications

Hello all, and thanks to those who give their time to help out those in need!
That being said, unfortunately I’m one of the latter at the moment, and would appreciate some guidance and advice on how to do what I need to do.
In a nutshell, here’s the situation:

I have a non-profit website that gives local businesses the ability to create and advertise coupons for their business for free.
Currently, I have everything working via DreamWeaver’s SPRY Javascript framework, and I’m looking to ease my work load by converting the system to PHP for ease of maintenance, along w/ SEO benefits. And I do not want to do this w/ a Database, as the coupons only have a lifespan of 2 weeks then they’re gone.

So, I’m trying to do it all w/ just XML.

That being said, I need to figure out how to port individual coupon/node info into a new dynamic page when clicked, or allow it to be emailed as an HTML email, or possibly allow it to be shared on Social Media.

So, I have a webpage that will use SimpleXML to read and output HTML from a file called “Coupons.xml”, which will have all the weekly coupons in it in XML format of course.

Each coupon’s business name will be a link/button/submit that when clicked, will use xPath and somehow collect all of that coupon’s node info only and pass it along to the next thing, be it an individual webpage created dynamically to be printed or emailed, or whatever I may need to do w/ the info.

I’ve came to the conclusion that each coupon actually needs to be treated as a form, with the Company Business Name link actually being a submit button that will collect the node info for that coupon only and pass it along. I think an invisible field should do the trick to pass the info along, though I’m open to suggestions.

The user will have a couple of options for now:

  1. Click the Business name » script is ran to dynamically produce a print page that will have just that coupon on it only, and user can print individual coupon.
  2. Each coupon will also contain a checkbox and actually be part of a larger form. If the user checks off a series of coupons, then hits a designated submit button, a dynamic page is produced w/ only those selected coupons on it for print.
  3. Using the same methods, the user will also have the option to email an individual coupon, or the custom print sheet of coupons they created.

That being said, since each coupon will actually be a form, I’m not sure how to designate the checkboxes to be able to retrieve the node info and pass it along to the next step as part of the larger form.

Also, I’m not sure how to make the Business Name submit button to extract and pass along the node info to the next step. I know it will probably have to use xPath, which I have working, though I’m new to it and don’t know how to use it to pass the info to the next page, which is where the invisible field will come in I’m thinking.

I hope this makes sense. Below is my code. Someone please help me sort this out and be able to pin-point coupon node info and pass it along.

Thanks in advance for any and all help!
Abs0lute

Here’s some sample code of what I have thus far:
XML Coupons:


<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<coupons>

<coupon id="1">
<expiration>Feb. 26, 2010</expiration>
<category>Fitness</category>
<company>Some Business</company>
<deal>Lorem ipsum dolor sit amet, consectetur adipiscing elit. </deal>
<deal2>Nullam ut diam eget metus scelerisque pulvinar ut a ipsum. </deal2>
<deal3>Nullam ut sagittis justo. Praesent massa lacus, scelerisque sit amet mollis a, lobortis at dolor.</deal3>
<extra>Some Extra Info will go here if needed</extra>
<address>123 Some Street, Someplace, World, 12345</address>
<address2>123 Some Street, Someplace, World, 12345</address2>
<site>www.somewebsite.org</site>
<phone>123-123-1234</phone>
<legal>Not valid with any other offer or any other location.</legal>
</coupon>

<coupon id="2">
<expiration>March 26, 2010</expiration>
<category>Automotive</category>
<company>Whatchamacallits</company>
<deal>Suspendisse at lectus lectus, at blandit metus.</deal>
<deal2>Cras varius fermentum aliquet. Pellentesque habitant morbi tristique</deal2>
<address>123 Some Street, Someplace, World, 12345</address>
<site>www.somewebsite.org</site>
<phone>123-123-1234</phone>
<legal>Not valid with any other offer or any other location.</legal>
</coupon>

<coupon id="3">
<expiration>April 26, 2010</expiration>
<category>Health</category>
<company>Jane Doe's</company>
<deal>Sed ullamcorper sodales luctus. Duis ut lacus at ante malesuada ultrices.</deal>
<extra>Some Extra Info will go here if needed</extra>
<address>123 Some Street, Someplace, World, 12345</address>
<site>www.somewebsite.org</site>
<phone>123-123-1234</phone>
<legal>Not valid with any other offer or any other location.</legal>
</coupon>

</coupons>

Below is my HTML code. xPath is working at the bottom of the page, but I need it to be sending individual coupon info to the next page/step, not printing on this page.

HTML w/ PHP using SimpleXML to render coupons:


<html>
<head>
<title>Coupons using XML &amp; PHP</title>
<style type="text/css">
#coupon {
	width:500px;
	fontsize: 93.8%;
	margin:0 0 1.5em;
	padding:0.5em;
	border: dotted 1px #000;
	display: block;
	list-style:none inside;
	background-color:#fff!important;
	position:relative;
}
 #coupon h5 {width:100%;margin:0;padding:.5em 0;text-align:center;font-size: .75em;border:none;color:#777;}
 #coupon h5.watermark {visibility:hidden;display:none;width:0;height:0;}
 #coupon h4.exp {font-size:.6em;margin: 1em 0 -.75em 1em;text-align:right;padding: 0.75em;background-color: #fff;border: none;color: #666;-webkit-border-radius: 1em;border:solid 1px #222;position:absolute;left:80%;top:5%;}

 #coupon h4 {margin:.5em 0 0 1em;padding:0;color:#777; vertical-align: top;}
#coupon h4 input[type=checkbox] {vertical-align: middle;margin: 0 0 0 1em;}
#coupon h4 em {font-size:.5em;margin:0 0 0 2em;padding:1em 0 0;border-left: solid 1px #888;color:#000;vertical-align:bottom;}
#coupon h1 input[type=submit], 
#coupon h1 input[type=button] {padding:.25em;font: normal 900 .75em/1.5 'Arial Black',Helvetica,Arial,sans-serif!important;color:#ccc;padding:.25em 1em;display:block;width:100%;text-align:left;margin:-.5em 0 .5em;
	border:solid 1px #000;
	-webkit-border-radius: .5em;
	box-shadow: 0.3em 0.3em 0.3em #888;-webkit-box-shadow: 0.1em 0.1em 0.1em #888; /* x,y,z coordinages (z is blur/depth)  */
	background-image: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#666), to(#222));
	-webkit-transition:all 500ms ease-in;}
	#coupon h1 input a {color:#336;font-weight: bold;}
	#coupon h1 input[type=submit]:link, #coupon h1 input[type=button]:link {color:#ccc;}
	#coupon h1 input[type=submit]:visited, #coupon h1 input[type=button]:visited {color: #888;}
	#coupon h1 input[type=submit]:hover, #coupon h1 input[type=button]:hover {color: #000;text-decoration:underline;box-shadow: none;-webkit-box-shadow: none;
	background-image: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#888), to(#ccc));
	border:solid 1px #666;}
	#coupon h1 input[type=submit]:active, #coupon h1 input[type=button]:active {color:#900;}
 #coupon h1#info {font-family: 'Arial Narrow',Helvetica,Arial,sans-serif;color:#fff;padding: .8em 1em;font-size:.8em;line-height:1.75;}
 #coupon h2 {display:block;margin:0;padding:0 0 0 1em;font-size:100%;border:none;font-weight: normal;}
 address {display:block;margin:0;padding:0;font-size:100%;text-align: center;border:none;}
 #coupon h3 {display:block;margin:0;padding:0;font-size:95%;text-align: center;border:none;}
 #coupon h6 {display:block;margin:0 auto;padding:0;font-size:85%;text-align: center;border:none;font-weight:light;}
</style>
</head>
<body>
<?php 
    if (file_exists('coupon.xml')) { 
      $coupons = simplexml_load_file('coupon.xml'); 
      $CouponNumber = 1; #Defines the html coupons page starting at 1. 

      foreach($coupons->coupon as $coupon) { 
        $id = $coupon->attributes()->id; 
        echo "<form action=\\"+coupon.php\\" method=\\"post\\" name=\\"Single Coupon\\">
				<fieldset id=\\"coupon\\">
                	<input type=\\"hidden\\" value=\\"$id\\" /> 
                <h4 class=\\"exp\\">$coupon->expiration</h4> 
                <h5 class=\\"watermark\\">KingsportDeals.com | Click to Save, Click to Win!</h5> 
                <h4>$coupon->category
                	<em> 
					<input name=\\"Select\\" type=\\"checkbox\\" value=\\"\\" />
					Add Coupon #$CouponNumber to my print que
					<em> 
				</h4>
				<h1><input type=\\"submit\\" value=\\"$coupon->company\\" /></h1>
                <h2>$coupon->deal</h2> 
                <h2>$coupon->deal2</h2> 
                <h2>$coupon->deal3</h2> 
                <h2>$coupon->deal4</h2> 
                <h2>$coupon->deal5</h2> 
                <h5>$coupon->extra</h5> 
                <address>$coupon->address</address> 
                <address>$coupon->address2</address> 
                <address>$coupon->address3</address> 
                <address>$coupon->address4</address> 
                <h3><a href=\\"http://$coupon->site \\" target=\\"_blank\\">$coupon->site</a></h3> 
                <h5>$coupon->phone</h5> 
                <h6>$coupon->legal</h6>
				</fieldset>
              </form>";              
        $CouponNumber++; // increment by 1     
      } 

      // this gets the node with the coupon with id = 3 
      $result = $coupons->xpath("//coupon[@id='3']");  
      list( , $node) = each($result); 
      echo $node->attributes()->id; 
      if(isset($node)) { 
	  	echo '<div id=\\"coupon\\">';
		echo '<h4 class=\\"exp\\">' . $coupon->expiration . '</h4>';
		
		echo '</div>';
               
        echo "<h4>". $node->category."</h4>";
		echo "COM:". $node->company."<br>"; 
		
      } 
    } // if (file_exists('coupons.xml')) 
?>

</body>
</html>

Thanks for the post oddz.
I kinda get it not really?
It seems like alot for something simple.
I was able to do the same thing w/ alot less code in plain ol’ php


<?php 
  if (file_exists('coupons.xml')) { 
    $coupons = simplexml_load_file('coupons.xml'); 
    $CouponNumber = 1; #Defines the html coupons page starting at 1. 
   
   echo "<form action=\\"Print-multiple.php\\" method=\\"POST\\">
   		    <input type=\\"submit\\" value=\\"Print all of my checked coupons\\" /> ";
   
    foreach($coupons->coupon as $coupon) { 
      $id = $coupon->attributes()->id; 
      echo "<div id=\\"coupon\\">
                <input type=\\"checkbox\\" name=\\"coupon_$id\\" value=\\"coupon_$id\\" /> 
                <input type=\\"hidden\\" name=\\"_submit_check\\" value=\\"coupon_$id\\"/> 
                    <h4 class=\\"float\\">$coupon->expiration</h4> 
                <h4>$coupon->category</h4> 
                <h1><a href=\\"Print-single.php?coupon=$id\\" target=\\"_blank\\">$coupon->company</a></h1> 
                <h2>$coupon->deal</h2> 
                <h2>$coupon->deal2</h2> 
                <h2>$coupon->deal3</h2> 
                <h2>$coupon->deal4</h2> 
                <h2>$coupon->deal5</h2> 
                <h5>$coupon->extra</h5> 
                <address>$coupon->address</address> 
                <address>$coupon->address2</address> 
                <address>$coupon->address3</address> 
                <address>$coupon->address4</address> 
                <h3><a href=\\"http://$coupon->site \\" target=\\"_blank\\">$coupon->site</a></h3> 
                <h5>$coupon->phone</h5> 
                <h6>$coupon->legal</h6>
				</div>";
      $CouponNumber++; // increment by 1     
    } 
    echo   "</form>";              
  } // if (file_exists('coupons.xml')) 
?> 

Unfortunately, I’m unable to get the single coupon page working via the Company link in each coupon:


<h1><a href=\\"Print-single.php?coupon=$id\\" target=\\"_blank\\">$coupon->company</a></h1>

This is where I was stuck to begin with, maybe you can help me finish what I need off here. As you can see, the link passes the node id in the url. I’m needing the single coupon page to access the node id via the url, load the xml document and find that node, then output it in coupon format, something like this:


<?php
$NodeNumber = $_GET['coupon']; 
$coupons = simplexml_load_file('coupons.xml'); 
$result= $coupons->xpath("//coupons/coupon[@id='$NodeNumber']"); 
echo "<div id=\\"coupon\\">"; 
echo "<h4 class=\\"float\\">". $node->expiration . "</h4> "; 
echo "<h5>KingsportDeals.com | Click to Save, Click to Win!</h5> "; 
echo "<h4>". $node->category . "</h4> "; 
echo "<h1><a href=\\"Print-single.php?coupon_$id\\" target=\\"_blank\\">". $node->company . "</a></h1> "; 
echo "<h2>". $node->deal . "</h2> "; 
echo "<h2>". $node->deal2 . "</h2> "; 
echo "<h2>". $node->deal3 . "</h2> "; 
echo "<h2>". $node->deal4 . "</h2> "; 
echo "<h2>". $node->deal5 . "</h2> "; 
echo "<h5>". $node->extra . "</h5> "; 
echo "<address>". $node->address . "</address> "; 
echo "<address>". $node->address2 . "</address> "; 
echo "<address>". $node->address3 . "</address> "; 
echo "<address>". $node->address4 . "</address> "; 
echo "<h3><a href=\\"http://". $node->site . " \\" target=\\"_blank\\">". $node->site . "</a></h3> "; 
echo "<h5>". $node->phone . "</h5> "; 
echo "<h6>". $node->legal . "</h6>"; 
echo "</div>";
?>

I know how to get the node id and what it’s output will look like, just don’t know how to scan the xml doc and locate that id, then feed it to the output.

Don’t get me wrong, your work has been superb, and I know OOP is awesome, but at this time, I don’t see a balanced trade off b/c of OOP’s complexity. I’d like to hear your thoughts on the matter in this circumstance.

Thanks again for all of your help!
Abs0lute

This is in no way a complete example and I don’t condone using functions for display purposes. However, the below code will build out the form and provide a way to submit to another page that shows the coupons for print that were selected. Again, the reason I am using functions is to keep things as straight-forward as possible without going into a full blown MVC discussion. So analyze the below code and see if you can’t grasp how to pass the selected coupons to the printed page in multiples using for own work flow.


<?php
/*
* Coupon data entity
*/
class Coupon {
	public	
	$id
	,$expiration
	,$category
	,$company
	,$deal
	,$deal2
	,$deal3
	,$extra
	,$address
	,$address2
	,$site
	,$phone
	,$legal;	
}

/*
* Coupon object relational mapper
*/
class ORMCoupon {

	private
	
	$_xml;
	
	public function __construct() {
		
		$this->_xml = simplexml_load_file('coupons.xml');
		
	}
	
	/*
	* Get coupon by id
	*
	* @param int id
	* @return obj coupon
	*/
	public function fetchById($id) {
		$coupon = array_pop($this->_xml->xpath("coupon[@id=$id]"));		
		return $coupon === null?null:$this->_toCoupon($coupon);
	}
	
	/*
	* List coupons
	*
	* @param array list of coupon ids
	* @return array collection of coupon objects
	*/
	public function listAll($ids=null) {
		
		/*
		* Build xpath query
		*/
		if(!empty($ids)) {
			$query = 'coupon[@id='.implode(' or @id=',$ids).']';
		} else {
			$query = 'coupon';
		}
		
		/*
		* Run query
		*/
		$nodes = $this->_xml->xpath($query);
		
		/*
		* Collect result set
		*/
		$coupons = array();
		foreach($nodes as $node) {
			$coupons[] = $this->_toCoupon($node);
		}
		
		return $coupons;
		
	}
	
	/*
	* Convert XML coupon node to Coupon object
	*
	* @param obj coupon node
	* @return obj coupon object
	*/
	private function _toCoupon($xml) {
		
		$coupon = new Coupon();
		
		$coupon->id    			= $this->_getNodeId($xml);
		$coupon->expiration 	= (string) $xml->expiration;
		$coupon->category 		= (string) $xml->category;
		$coupon->company		= (string) $xml->company;
		$coupon->deal			= (string) $xml->deal;
		$coupon->deal2			= (string) $xml->deal2;
		$coupon->deal3			= (string) $xml->deal3;
		$coupon->extra			= (string) $xml->extra;
		$coupon->address		= (string) $xml->address;
		$coupon->address2		= (string) $xml->address2;
		$coupon->site			= (string) $xml->site;
		$coupon->phone			= (string) $xml->phone;
		$coupon->legal			= (string) $xml->legal;
		
		return $coupon;
		
	}
	
	/*
	* Get the id of a coupon xml node
	*
	* @param obj coupon node
	* @return int id
	*/
	private function _getNodeId($xml) {
	
		foreach($xml->attributes() as $name=>$value) {
			if(strcmp('id',$name) === 0) {
				return (int) $value;
			}
		}
	
	}

}

function unit_test() {

	$orm = new ORMCoupon();

	/*
	* Test One: List coupons with ids 1 and 2
	*/
	$coupons = $orm->listAll(array(1,2));

	echo '<p>Test One: List coupons 1 and 2</p>';
	echo '<pre>',print_r($coupons),'</pre>';

	/*
	* Test Two: Get coupon 3
	*/
	$coupon = $orm->fetchById(3);

	echo '<p>Test Two: Get coupon 3</p>';
	echo '<pre>',print_r($coupon),'</pre>';

	/*
	* Test Three: Pass coupon id through Query string
	* ?id=2
	*/
	$coupon = $orm->fetchById(isset($_GET['id'])?$_GET['id']:0);

	echo '<p>Test Three: Pass Coupon id through URL</p>';
	echo $coupon === null?'<p>No Coupon - ex. ?id=2</p>':'<pre>',print_r($coupon),'</pre>';

	/*
	* Test Four: Pass a bunch of coupons through url
	* ?list[]=1&list[]=2&list[]=3
	*/

	$coupons = $orm->listAll(isset($_GET['list'])?$_GET['list']:array(0));

	echo '<p>Test Four: List multiple coupons via query string</p>';
	echo empty($coupons)?'<p>No Coupons - ex. ?list[]=2&list[]=3</p>':'<pre>',print_r($coupons),'</pre>';

}

/*
* Display coupon for print
*
* @param obj coupon
*/
function theme_coupon_print($coupon) {

	echo '<h4 class="exp">' . $coupon->expiration . '</h4> ';
	echo '<h1>'.$coupon->company . '</h1>';
	echo '<h2>' . $coupon->deal . '</h2>';
	echo '<h2>' . $coupon->deal2 . '</h2>';
	echo '<h2>' . $coupon->deal3 . '</h2>';
	echo '<h5>' . $coupon->extra . '</h5>';
	echo '<address>' .  $coupon->address . '</address>';
	echo '<address>' .  $coupon->address2 . '</address>';
	echo '<h3>'.$coupon->site . '</h3> ';
	echo '<h5>' . $coupon->phone . '</h5>';
	echo '<h6>' . $coupon->legal . '</h6>'; 

}

/*
* Display coupon default
*
* @param obj coupon
*/
function theme_coupon($coupon) {

	echo '<input type="hidden" value="' . $coupon->id  . '" /> ';
	echo '<h4 class="exp">' . $coupon->expiration . '</h4> ';
	echo '<h4>' . $coupon->category . '<em><input name="list[]" type="checkbox" value="'.$coupon->id.'" />Add Coupon #' . $coupon->id . ' to my print que<em></h4>';
	echo '<h1><input type="submit" target="_blank" value="' . $coupon->company . '" /></h1>';
	echo '<h2>' . $coupon->deal . '</h2>';
	echo '<h2>' . $coupon->deal2 . '</h2>';
	echo '<h2>' . $coupon->deal3 . '</h2>';
	echo '<h5>' . $coupon->extra . '</h5>';
	echo '<address>' .  $coupon->address . '</address>';
	echo '<address>' .  $coupon->address2 . '</address>';
	echo '<h3><a href="http://' . $coupon->site . ' " target="_blank">' . $coupon->site . '</a></h3> ';
	echo '<h5>' . $coupon->phone . '</h5>';
	echo '<h6>' . $coupon->legal . '</h6>'; 

}

/*
* Print selected coupons display
*/
function print_coupons($orm) {

	// Get coupons from URL
	$coupons = $orm->listAll(isset($_GET['list'])?$_GET['list']:array());

	// show coupon list
	echo '<ul>';
	foreach($coupons as $coupon) {

		echo '<li>';
		theme_coupon_print($coupon);
		echo '</li>';

	}
	echo '</ul>';

}

/*
* View available coupons (form)
*/
function coupons_form($orm) {

	// Get coupons from URL
	$coupons = $orm->listAll(isset($_GET['list'])?$_GET['list']:array());

	// show form
	printf('<form action="&#37;s" method="GET">',$_SERVER['PHP_SELF']);
	echo '<fieldset><legend>Coupons<legend>';
	echo '<ul>';
	foreach($coupons as $coupon) {

		echo '<li>';
		theme_coupon($coupon);
		echo '</li>';

	}
	echo '</ul>';
	echo '<input type="submit" name="print" value="Print" />';
	echo '</fieldset>';
	echo '</form>';

}

$route = isset($_GET['print'])?'print_coupons':'coupons_form';
call_user_func_array($route,array(new ORMCoupon()));


?>

Okay, so I reverted back to your original code, then started tinkering w/ Tests #3 & #4 to output the xml nodes in HTML format.
#3 works like a charm in producing 1 coupon, here’s my code for that test:


/*
* Test Three: Pass coupon id through Query string
* ?id=2
*/
$coupon = $orm->fetchById(isset($_GET['id'])?$_GET['id']:1);

echo '<h1>Test Three: Pass Coupon id through URL</h1>';
#echo $coupon === null?'<p>No Coupon - ex. ?id=2</p>':'<pre>',print_r($coupon),'</pre>';

# MY CODE_________________
echo '<form action="coupon-OOP.php" method="post" name="Single Coupon">';
echo '<fieldset id="coupon">';
echo '<input type="hidden" value="' . $coupon->id  . '" /> ';
echo '<h4 class="exp">' . $coupon->expiration . '</h4> ';
echo '<h4>' . $coupon->category . '<em><input name="Select" type="checkbox" value="' . $coupon->id . '" />Add Coupon #' . $coupon->id . ' to my print que<em></h4>';
echo '<h1><input type="submit" target="_blank" value="' . $coupon->company . '" /></h1>';
echo '<h2>' . $coupon->deal . '</h2>';
echo '<h2>' . $coupon->deal2 . '</h2>';
echo '<h2>' . $coupon->deal3 . '</h2>';
echo '<h5>' . $coupon->extra . '</h5>';
echo '<address>' .  $coupon->address . '</address>';
echo '<address>' .  $coupon->address2 . '</address>';
echo '<h3><a href="http://' . $coupon->site . ' " target="_blank">' . $coupon->site . '</a></h3> ';
echo '<h5>' . $coupon->phone . '</h5>';
echo '<h6>' . $coupon->legal . '</h6></fieldset></form>';

However, w/ that setup on Test #4, it just produces the typical results of your code, but for mine, it just repeats Test #3 info.
How can it loop through and present all coupons for test #4?

Also, how would you recommend using test #3 on the dynamic individual coupon page? That is, how can I pass the node id to the dynamic page so it can render that coupon? Session? Cookie? Hidden Form Field? Not needed?
I was thinking link or input field, and pass the value that way. Any suggestions?
That’s were I’m really struggling, along with test #4:


$coupons = $orm->listAll(isset($_GET['list'])?$_GET['list']: array (1,2,3));

echo '<h1>Test Four: List multiple coupons via query string</h1>';
echo empty($coupons)?'<p>No Coupons - ex. ?list[]=1&list[]=2&list[]=3</p>':'<pre>',print_r($coupons),'</pre>';

# MY CODE_________________
echo '<form action="coupon-OOP.php" method="post" name="Single Coupon">';
echo '<fieldset id="coupon">';
echo '<input type="hidden" value="' . $coupon->id  . '" /> ';
echo '<h4 class="exp">' . $coupon->expiration . '</h4> ';
#echo '<h5 class="watermark">KingsportDeals.com | Click to Save, Click to Win!</h5>';
echo '<h4>' . $coupon->category . '<em><input name="Select" type="checkbox" value="' . $coupon->id . '" />Add Coupon #' . $coupon->id . ' to my print que<em></h4>';
echo '<h1><input type="submit" target="_blank" value="' . $coupon->company . '" /></h1>';
echo '<h2>' . $coupon->deal . '</h2>';
echo '<h2>' . $coupon->deal2 . '</h2>';
echo '<h2>' . $coupon->deal3 . '</h2>';
echo '<h5>' . $coupon->extra . '</h5>';
echo '<address>' .  $coupon->address . '</address>';
echo '<address>' .  $coupon->address2 . '</address>';
echo '<h3><a href="http://' . $coupon->site . ' " target="_blank">' . $coupon->site . '</a></h3> ';
echo '<h5>' . $coupon->phone . '</h5>';
echo '<h6>' . $coupon->legal . '</h6></fieldset></form>';

Why couldn’t I have worded it that simply to begin with!
So can you oddz?
Except for the SPAMMING part that would be great. However, this is a small project with no spamming, all opt-in.
Being written is OOP is awesome I think. Just starting to learn it, and oddz is a major help. Doing my due diligence here to put it together myself and learn something.
I don’t think xsl is what I’m looking for here dsims. Just need a way to port the xml node info between pages.
Thanks Oddz, you’re the man.
A.

Placing HTML inside the _toCoupon() method mixes presentation with business logic, this is a big no no when it comes to MVC. Presentation of the data should be applied after fetching it from the relational mapper, not within it. Placing HTML within the ORM pretty much defeats the purpose of having a ORM in the first place.

With that said, the reason tests 3 and 4 don’t work is because query string data isn’t being passed. Both those tests use data coming from a query string as shown by the info following ex (example). If nothing is passed via the query string no data will be returned.

I would recommend passing the data via the query string, though I’m finding it a little difficult to see what your after. Do you have any mock-ups of what the intended views should look like? The form with a bunch of headings isn’t really making much sense to me. Normally when you place things in a form it is to allow users to create or edit data.

Thank you so much oddz! You’re a great programmer!
This seems like it’s on the right path for what I’m trying to do.
However, Tests 3 and 4 don’t do anything, just print out the PHP code it seems:

Test Three: Pass Coupon id through URL

No Coupon - ex. ?id=2

1
Test Four: List multiple coupons via query string

No Coupons - ex. ?list=2&list=3

Array ( ) 1

My apologies for my ineptitude in OOP programming, but you seem to be very well endowed on that front. So, pardon my tinkering here, but I took code and changed the private function ‘_toCoupon($xml)’ to include the HTML markup as well:

private function _toCoupon($xml) {
        
        $coupon = new Coupon();
        
        $coupon->id                = $this->_getNodeId($xml);
        $coupon->expiration     = (string) '<form action="+coupon.php" method="post" name="Single Coupon">
											<fieldset id="coupon">
                							<input type="hidden" value="' . $coupon->id  . '" /> 
                							<h4 class="exp">' . $xml->expiration . '</h4> 
                							<h5 class="watermark">KingsportDeals.com | Click to Save, Click to Win!</h5>';
											
        $coupon->category         = (string) '<h4>' . $xml->category . '<em><input name="Select" type="checkbox" value="' . $coupon->id  . '" />Add Coupon #' . $coupon->id . ' to my print que<em></h4>';
        $coupon->company        = (string) '<h1><input type="submit" value="' . $xml->company . '" /></h1>';
        $coupon->deal            = (string) '<h2>' . $xml->deal . '</h2>';
        $coupon->deal2            = (string) '<h2>' . $xml->deal2 . '</h2>';
        $coupon->deal3            = (string) '<h2>' . $xml->deal3 . '</h2>';
        $coupon->extra            = (string) '<h5>' . $xml->extra . '</h5>';
        $coupon->address        = (string) '<address>' .  $xml->address . '</address>';
        $coupon->address2        = (string) '<address>' .  $xml->address2 . '</address>';
        $coupon->site            = (string) '<h3><a href="http://' . $xml->site . ' " target="_blank">' . $xml->site . '</a></h3> ';
        $coupon->phone            = (string) '<h5>' . $xml->phone . '</h5>';
        $coupon->legal            = (string) '<h6>' . $xml->legal . '</h6></fieldset></form>';
        
        return $coupon;
        
    }

Works great and is going in the right direction. However, it is also printing out the xml node’s child names as well. Is there any way to prevent that?
Here’s a pic, note the xml names:

Also, how would you recommend either passing the node info into it’s own html doc, or opening a new html doc and pin-point the indicated node id and present it?

Thanks again for your expertise. This is awesome.
Abs0lute

You could use a separate data access layer and than treat if just like you were pulling stuff from the database. Just allow the ORM to hide the implementation details. This way so long as you have the coupon id the other information can be retrieved without a whole bunch of repetition.


<?php
/*
* Coupon data entity
*/
class Coupon {
	public	
	$id
	,$expiration
	,$category
	,$company
	,$deal
	,$deal2
	,$deal3
	,$extra
	,$address
	,$address2
	,$site
	,$phone
	,$legal;	
}

/*
* Coupon object relational mapper
*/
class ORMCoupon {

	private
	
	$_xml;
	
	public function __construct() {
		
		$this->_xml = simplexml_load_file('coupon.xml');
		
	}
	
	/*
	* Get coupon by id
	*
	* @param int id
	* @return obj coupon
	*/
	public function fetchById($id) {
		$coupon = array_pop($this->_xml->xpath("coupon[@id=$id]"));		
		return $coupon === null?null:$this->_toCoupon($coupon);
	}
	
	/*
	* List coupons
	*
	* @param array list of coupon ids
	* @return array collection of coupon objects
	*/
	public function listAll($ids) {
		
		/*
		* Build xpath query
		*/
		$query = 'coupon[@id='.implode(' or @id=',$ids).']';
		
		/*
		* Run query
		*/
		$nodes = $this->_xml->xpath($query);
		
		/*
		* Collect result set
		*/
		$coupons = array();
		foreach($nodes as $node) {
			$coupons[] = $this->_toCoupon($node);
		}
		
		return $coupons;
		
	}
	
	/*
	* Convert XML coupon node to Coupon object
	*
	* @param obj coupon node
	* @return obj coupon object
	*/
	private function _toCoupon($xml) {
		
		$coupon = new Coupon();
		
		$coupon->id    			= $this->_getNodeId($xml);
		$coupon->expiration 	= (string) $xml->expiration;
		$coupon->category 		= (string) $xml->category;
		$coupon->company		= (string) $xml->company;
		$coupon->deal			= (string) $xml->deal;
		$coupon->deal2			= (string) $xml->deal2;
		$coupon->deal3			= (string) $xml->deal3;
		$coupon->extra			= (string) $xml->extra;
		$coupon->address		= (string) $xml->address;
		$coupon->address2		= (string) $xml->address2;
		$coupon->site			= (string) $xml->site;
		$coupon->phone			= (string) $xml->phone;
		$coupon->legal			= (string) $xml->legal;
		
		return $coupon;
		
	}
	
	/*
	* Get the id of a coupon xml node
	*
	* @param obj coupon node
	* @return int id
	*/
	private function _getNodeId($xml) {
	
		foreach($xml->attributes() as $name=>$value) {
			if(strcmp('id',$name) === 0) {
				return (int) $value;
			}
		}
	
	}

}

$orm = new ORMCoupon();


/*
* Test One: List coupons with ids 1 and 2
*/
$coupons = $orm->listAll(array(1,2));

echo '<p>Test One: List coupons 1 and 2</p>';
echo '<pre>',print_r($coupons),'</pre>';

/*
* Test Two: Get coupon 3
*/
$coupon = $orm->fetchById(3);

echo '<p>Test Two: Get coupon 3</p>';
echo '<pre>',print_r($coupon),'</pre>';

/*
* Test Three: Pass coupon id through Query string
* ?id=2
*/
$coupon = $orm->fetchById(isset($_GET['id'])?$_GET['id']:0);

echo '<p>Test Three: Pass Coupon id through URL</p>';
echo $coupon === null?'<p>No Coupon - ex. ?id=2</p>':'<pre>',print_r($coupon),'</pre>';

/*
* Test Four: Pass a bunch of coupons through url
* ?list[]=1&list[]=2&list[]=3
*/

$coupons = $orm->listAll(isset($_GET['list'])?$_GET['list']:array(0));

echo '<p>Test Four: List multiple coupons via query string</p>';
echo empty($coupons)?'<p>No Coupons - ex. ?list[]=2&list[]=3</p>':'<pre>',print_r($coupons),'</pre>';

?>

Nowhere do you define what $node is. You grab an array of matching coupons in $result so you could use $node = $result[0]; (it’s probably best to also check if any matching coupons were found).

Have you read up on XSL, this would help in converting the xml to html.

Doesn’t appear that the link worked. Here’s another one.
Thanks again to any and all who might be able to offer me some assistance!
Abs0lute

Am I just being a dumb ass here? I really don’t understand why that code for a single coupon doesn’t work - or the missing link it needs to work.
Anybody know how to hook it up?
Thanks
Abs0lute

oddz, your a great programmer. I’m sorry I don’t understand OOP and your so endowed, can you write a mvc non-profit coupon spamming app for me?:lol:

Thanks oddz. I’ll work towards keeping this thing OOP oriented as you suggested.
That being said, the only reason I was thinking forms was to be able to run a script when the business name is clicked. That script would identify the node, port that info to a dynamic page and use it to present a single coupon w/ that info in it.
So, you have a list of coupons on a page. Each coupon has 2 links: the business name and website if available. When the business name is clicked, I want to be able to grab that xml node’s info for that coupon and put it in a dynamically created page, which will use that info to display 1 coupon that the user can print.
This is my current method, but you can only print 1 coupon per sheet of paper, so it’s a waste of paper and I’m trying to improve this for the user.

Here’s how: I will need to put all coupons in a form, and each coupon will have a checkbox. After the user checks off several coupons they want, they can hit a print button (submit) that will run a script that will identify all checked coupons, get their node info, then create a dynamic page w/ all checked coupons only on it - then the user can print off 1 sheet of their chosen coupons.

If I can get these 2 things figured out, I think I’ll be able to run the coupons through an RSS feed or make them where the user can email a coupon to their friends and family - but that’s further down the road. Just looking to make my updating easier now.

I sent you a personal message with greater detail.
Just let me know if that’s making sense now, or I need to clarify further.
Cheers.
Abs0lute

Also, I’m attaching a PDF that visually demonstrates what’s going on.
Abs0lute

Had to pull off of this project for a bit, but I’m back on it now.
Everything is working now except one odd little thing. When I check off multiple coupons to print, then submit to get a print page of only the coupons I want, the coupons render correctly up to coupon #9. Any coupon after that is looped back to the single digit coupons! For example, if I check off coupon #10 and hit submit, it gives me coupon #1. Coupon #11 = Coupon #2, and so on.
Any ideas on why that’s happening?
Thanks in advace!

Figured it out. Changed the substr from -1 to -3. Works perfectly.
Thanks to all for their help!
:smiley: