Location: PHPKode > projects > Grocery List > edititem.php
<?php
########################################################################
#
# Project: Grocery List
# URL: http://sourceforge.net/projects/grocery-list/
# E-mail: hide@address.com
#
# Copyright: (C) 2010, Neil McNab
# License: GNU General Public License Version 3
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Filename: $URL: https://grocery-list.svn.sourceforge.net/svnroot/grocery-list/releases/1.0/edititem.php $
# Last Updated: $Date: 2010-03-06 00:03:07 -0800 (Sat, 06 Mar 2010) $
# Author(s): Neil McNab
#
# Description:
#   Edit or add a particular item in the database.
#
########################################################################

$title = "Edit Item";
$onload = "document.getElementsByName('upc')[0].focus();";
include_once("include/header.php");

$db = dbconnect();

$id = $_REQUEST['id'];
$prid = $_REQUEST['cat'];
$newcat = $_REQUEST['newcat'];
$itemname = $_REQUEST['itemname'];
$size = $_REQUEST['size'];
$units = measure_unit_normalize($_REQUEST['units']);
$priority = $_REQUEST['priority'];
$upc = $_REQUEST['upc'];

if (isset($_REQUEST['submit'])) {
    $errors = '';
    if (empty($_REQUEST['newcat']) AND empty($_REQUEST['cat'])) {
        $errors .= "<p>ERROR: Missing Generic Item name.</p>";
    }
    print $errors;

    if (strlen($errors) <= 0) {
	// handle product table first
	if (empty($prid)) {
            $prid = add_product($db, $newcat);
	}
        $id = add_update_item($db, $id, $prid, $itemname, $size, $units, $upc, $priority);
        print "<p>Updated item.</p>";
    }
}

$result = array('upc' => $upc, 'productid' => $prid, 'size' => $size, 'units' => $units, 'itemname' => $itemname, 'newcat' => $newcat, 'priority' => $priority);

if (!empty($id)) {
        $tmpresult = get_item_by_itemid($db, $id);
	if ($tmpresult === FALSE) {
            print "query failed id";
	} else {
	    $result = $tmpresult->fetch();
        }
}

if (!empty($upc) AND isset($_REQUEST['lookup'])) {
    $result = get_upc($db, $upc);
    $id = $result['itemid'];
}

print '<script type="text/javascript" src="include/cuecat.js"></script>';

print '<form action="">';
print "<p><input type='hidden' name='id' value='" . $id . "' /></p>\n";
print "<table><tr><td>Bar code:</td><td><input name='upc' value='" . $result['upc'] . "' /><input type='submit' name='lookup' value='Lookup' /></td></tr>\n";
print "<tr><td>Generic Item*:</td><td><select name='cat'>\n";
print get_option("", "(new)", $result['productname']);
foreach (get_products($db) as $row) {
    print get_option($row['productid'], $row['productname'], $result['productid']);
}
print "</select><input name='newcat' value='" . $result['newcat'] . "' /></td></tr>\n";
print "<tr><td>Brand Name:</td><td><input size='40' name='itemname' value='" . $result['itemname'] . "' /></td></tr>\n";
print "<tr><td>Size:</td><td><input name='size' value='" . $result['size'] . "' /></td></tr>\n";
print "<tr><td>Units:</td><td><input name='units' value='" . $result['units'] . "' /></td></tr>\n";
//print "<tr><td>Priority:</td><td><input name='priority' value='" . $result['priority'] . "' /></td></tr>\n";

print '</table>';

print "<p><input type='submit' name='submit' value='Add/Update' /></p>";

print '</form>';

print '<p>* Required Fields</p>';

$db = NULL;

print "<p>Return to <a href='items.php'>Manage Items</a>.</p>\n";

include_once($WEBROOT . "/include/footer.php");

?>
Return current item: Grocery List