<?php
require_once("config.php");

if ( 
$_POST['truncate'] )
{
  
$table = new Shopper(); $table->truncate();
  
$table = new Product(); $table->truncate();
  
header("Location: example.php?message=".urlencode("Tables truncated"));
  exit;
}

if ( 
$_POST['newshopper'] )
{
  
$aShopper = new Shopper();
  
$aShopper->setField('name'$_POST['newshopper'] );
  
$aShopper->save();
  
header("Location: example.php?message=".urlencode("New shopper added"));
  exit;
}

if ( 
$_POST['newproduct'] )
{
  
$aProduct = new Product();
  
$aProduct->setField('name'$_POST['newproduct'] );
  
$aProduct->save();
  
header("Location: example.php?message=".urlencode("New product added"));
  exit;
}

if ( 
$_POST['shopper'] && $_POST['product'] )
{
  
$aShopper_Product = new Shopper_Product();
  
$aShopper_Product->setField('sid'$_POST['shopper']);
  
$aShopper_Product->setField('pid'$_POST['product']);
  
$aShopper_Product->setField('date_added'date("Y-m-d H:i:s"));
  
$aShopper_Product->save();
  
header("Location: example.php?message=".urlencode("New association added"));
  exit;
}

$aShoppers Shopper::getAll();
$aProducts Product::getAll();

?>
<html>
<head>
<title>DbTable Example</title>
</head>
<body>
<h1>DbTable Example</h1>

<p>The write up on this lives here: <a href="http://www.koopman.me/2008/11/dbtable-and-all-its-glory/">http://www.koopman.me/2008/11/dbtable-and-all-its-glory/</a></p>

<p><?=htmlspecialchars($_GET['message'])?></p>

<b>Truncate all data:</b>
<form method="POST" action="example.php">
Feel free, this is just an example, you can truncate, then enter some data.  Keep in mind that another user could be using this at the same time that you
are, which will lead to sudden truncation, or more data showing up than what you're entering.
<br />
<input type="submit" value="Truncate" name="truncate">
</form>

<table width="100%" cellspacing="5"><tr>
<td>

  <b>Add shopper:</b>
  <form method="POST" action="example.php">
  Name: <input type="text" name="newshopper" /> <br />
  <input type="submit" value="Add">
  </form>

</td><td>

  <b>Add Product:</b>
  <form method="POST" action="example.php">
  Name: <input type="text" name="newproduct" /> <br />
  <input type="submit" value="Add">
  </form>

</td><td>

  <b>Add Association</b>
  <form method="POST" action="example.php">
  <select name="shopper">
  <?php
  
foreach($aShoppers as $aShopper)
    print 
'<option value="'.$aShopper->getField('sid').'">'.htmlspecialchars($aShopper->getField('name')).'</option>';
  
?>
  </select>
  <select name="product">
  <?php
  
foreach($aProducts as $aProduct)
    print 
'<option value="'.$aProduct->getField('pid').'">'.htmlspecialchars($aProduct->getField('name')).'</option>';
  
?>
  </select><br />
  <input type="submit" value="Add">
  </form>

</td>
</tr></table>

<b>List of Shoppers</b>
<table cellpadding=3 cellspacing=0 border=1>
<tr>
  <td><b>Name</b></td>
  <td><b>Products</b></td>
</tr>
<?php
foreach ($aShoppers as $aShopper)
{
  print 
"<tr>\n";
  print 
"  <td>".htmlspecialchars($aShopper->getField('name'))."</td>\n";
  print 
"  <td>";
  
$products $aShopper->getProducts();
  foreach(
$products as $aProduct)
    print 
htmlspecialchars($aProduct->getField('name')).", ";
  print 
"  &nbsp;</td>\n";
  print 
"</tr>\n";
}
?>
</table>
<br /><br />

<b>List of Products</b>
<table cellpadding=3 cellspacing=0 border=1>
<tr>
  <td><b>Name</b></td>
  <td><b>Shoppers</b></td>
</tr>
<?php
foreach ($aProducts as $aProduct)
{
  print 
"<tr>\n";
  print 
"  <td>".htmlspecialchars($aProduct->getField('name'))."</td>\n";
  print 
"  <td>";
  
$shoppers $aProduct->getShoppers();
  foreach(
$shoppers as $aShopper)
    print 
htmlspecialchars($aShopper->getField('name')).", ";
  print 
"  &nbsp;</td>\n";
  print 
"</tr>\n";
}
?>
</table>


</body>
</html>