Location: PHPKode > projects > Ars Lab > arsLab-1.4.1/admin/cats.php
<html>
<link href="../lab.css" rel="stylesheet" type="text/css">
<?
include("../cfg.php");
?>
<script language="JavaScript">
<!--
function verifyForm(form)
{
  if( form.cname.value == "" )
  {
    alert('Category must have a name');
	form.cname.focus();
	return false;
  }
  if( form.info.value == "" )
  {
    alert('Category must have a description');
	form.info.focus();
	return false;
  }
  return true;
}
-->
</script>
<body>
<p align=right class=adm9>
<a href="<?=$SCRIPT_NAME;?>">New Category</a> &nbsp;
<a href="<?=$SCRIPT_NAME;?>?show=1">Category List</a> &nbsp;
</p>
<center>
<?
if( isset($cmd) )
{
  if( empty($cname) ) die("<font color=red><b>Category Name is required!</b></font>\n");
  if( empty($info) ) die("<font color=red><b>Category Description is required!</b></font>\n");
  if( $cmd == "Add" )
  {
    $end = "";
	$start = "insert into";
	$verb = "add";
  }
  else if( is_numeric($edit) )
  {
    $end = " where cid = $edit";
	$start = "update";
	$verb = "edit";
  }
  $db = mysql_pconnect($ars[host],$ars[user],$ars[pass]);
  mysql_select_db($ars[dbname]);
  $Q = $start . " category set cname='$cname', info='$info'" . $end;
  if( mysql_query($Q) )
  {
    echo "<p class=adm9b><font color=\"#008000\">Category $verb succeeded</font></p>\n";
	$show = 2;
  }
  else
    die("<p class=adm9b><font color=red>Error: " . mysql_error() . "</font></p>\n");
}

// delete a record
if( isset($del) & is_numeric($del) )
{
  $db = mysql_pconnect($ars[host],$ars[user],$ars[pass]);
  mysql_select_db($ars[dbname]);
  if( mysql_query("delete from category where cid = $del") )
    echo "<p class=adm9b><font color=\"#008000\">Category deleted</font></p>\n";
  else
    echo "<p class=adm9b><font color=\"red\">Delete failed</font></p>\n";
  $show = 3;
}

// get a record to edit
if( isset($get) & is_numeric($get) )
{
  $db = mysql_pconnect($ars[host],$ars[user],$ars[pass]);
  mysql_select_db($ars[dbname]);
  if( $res = mysql_query("select * from category where cid = $get") )
    $C = mysql_fetch_array($res);
  else
    die("<p class=adm9b><font color=\"red\">Could not get category: " . mysql_error() . "</font></p>\n");
}

if( isset($show) )
{
  // get a list of categories
  $db = mysql_pconnect($ars[host],$ars[user],$ars[pass]);
  mysql_select_db($ars[dbname]);
  if( $res = mysql_query("select * from category order by cname") )
  {
    echo "<p class=adm9b><u> &nbsp; Category List &nbsp; </u></p>\n";
	echo "<table border=1 cellspacing=0 cellpadding=4 class=adm9 bordercolor=\"#000080\">\n";
	echo "<tr align=center>\n";
	echo "  <td class=adm9b>Cat. Name</td>\n";
	echo "  <td class=adm9b>Description</td>\n";
	echo "  <td class=adm9b>Action</td>\n";
	echo "</tr>\n";
	$i  = 0;
	while( $r = mysql_fetch_array($res) )
	{
	  $i++;
	  echo "<tr>\n";
	  echo "  <td>$r[cname]</td>\n";
	  echo "  <td>$r[info]</td>\n";
	  echo "  <td>\n";
	  echo "    <a href=\"$SCRIPT_NAME?get=$r[cid]\">Edit</a> &nbsp;\n";
	  echo "    <a href=\"$SCRIPT_NAME?del=$r[cid]\" onClick=\"return confirm('Are you sure you want to delete this Category?');\">Delete</a>\n";
	  echo "  </td>\n";
	  echo "</tr>\n";
	}
	echo "<tr><td colspan=3 align=center>";
	if( $i ) echo "$i records displayed";
	else "No records to display";
	echo "</td></tr>\n";
	echo "</table>\n";
	echo "<p class=\"adm9\" align=right><a href=\"#\" onClick=\"window.close();\">Close Window</a></p>\n";
	die("</body></html>\n");
  }
  else 
    die("<font color=red><b>Error: " . mysql_error() . "</b></font>\n");
}
?>
<form method="post" action="<?=$SCRIPT_NAME;?>" onSubmit="return verifyForm(this);">
<!-- sorrounding table -->
<table border="1" cellspacing="0" cellpadding="4" bordercolor="#800000">
<tr><td>
<?
if( isset($C[cid]) && is_numeric($C[cid]) )
{
  $verb = "Modify";
  echo "<input type=hidden name=edit value=$C[cid]>\n";
}
else
  $verb = "Add";
?>
<!-- Inner Table -->
	<table border="0" cellspacing="0" cellpadding="4" class="adm9">
	<tr>
	  <td colspan="2" align="center"><b><?=$verb;?> Category</b><hr width="100%" color="#800000"></td>
	</tr>
	<tr>
	  <td class="adm9b">Category Name:</td>
	  <td><input type="text" name="cname" value="<?=$C[cname];?>" class="adm9b"></td>
	</tr>
	<tr>
	  <td class="adm9b">Category Description:</td>
	  <td><input type="text" name="info" value="<?=$C[info];?>" class="adm9b"></td>
	</tr>
	<tr>
	  <td colspan="2" align="center">
	    <input type="reset" value="Close" class="adm9b" onClick="window.close();"> &nbsp;
		<input type="submit" name="cmd" value="<?=$verb;?>" class="adm9b">
	  </td>
	</tr>
	</table>
</td></tr>
</table>
</form>
</center>
</body>
</html>
Return current item: Ars Lab