<?php

/*

CREATE TABLE flashStore_products (
  `id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(25),
  `category` int(3) NOT NULL DEFAULT 0,
  `description` TEXT,
  `thumbnail` varchar(200) NOT NULL DEFAULT "",
  `active` int(1) NOT NULL DEFAULT 1,
  PRIMARY KEY (`id`)
) 
 
 */

include 'dbconn.php';

//error_reporting(0);

function getProducts()
{
	global $link;
	
	$result = mysql_query("SELECT * FROM flashStore_products WHERE active=1", $link);
		
	$xml = "<products>\n";
	
	while($row = mysql_fetch_array($result))
	{
		$xml .= "<product id=\"" . $row['id'] . 
			"\" name=\"" . $row['name'] . 
			"\" desc=\"" . $row['description'] . 
			"\" thumbnail=\"" . $row['thumbnail'] . "\" />\n";
	}
	
	$xml .= "</products>";
	
	return $xml;
}

if($_GET['action'] == 'getproducts')
{	
	print getProducts();
}
else if($_GET['action'] == 'checkout')
{
	$xml = new SimpleXMLElement($_GET['xml']);
	
	$nodeCount = count($xml->product);
	
	print "<table border=\"1\" cellpadding=\"4\" cellspacing=\"4\" width=\"500\">";
	print "<tr><td><strong>Quantity</strong></td><td><strong>Product</strong></td><td><strong>Price</strong></td></tr>";
	
	for($x=0; $x < $nodeCount; $x++)
	{
		print "<tr style=\"background-color:#eeeeee;\"><td style=\"width:60px;text-align:center;\">" . $xml->product[$x]->quantity . "</td>";
		print "<td>" . $xml->product[$x]->name . "</td>";
		print "<td>" . $xml->product[$x]->price . "</td></tr>";
	}

		print "<tr><td style=\"text-align:right;\" colspan=\"3\">Total: <strong>" . getAttribute($xml, 'total') . "</strong></td></tr>";
	
	print "</table>";
	print "<br />Checkout code goes here<br />";
}

function getAttribute($xml, $name)
{
	foreach($xml->attributes() as $key=>$val)
	{
		if($key == $name)
		{
			return (string)$val;
		}
	}
	return false;
}

?>