<?php
########################################################################
#
# Project: Grocery List
# URL: http://sourceforge.net/projects/grocery-list/
#
# Copyright: (C) 2007, James Campbell
# 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/delGLitem.php $
# Last Updated: $Date: 2010-03-06 00:03:07 -0800 (Sat, 06 Mar 2010) $
# Author(s): James Campbell
#
########################################################################
$title = 'Delete Items';
include_once("include/site.php");
$ida = isset($_GET["ids"]) ? $_GET["ids"] : "";
$numchx = isset($_GET["numchx"]) ? $_GET["numchx"] : "";
$confirm = isset($_GET["confirm"]) ? $_GET["confirm"] : "";
if ($confirm=="Cancel")
{
$loc = "GroceryList.php";
header("Location: $loc");
exit;
}
//$ida = $ids;
$connection = dbconnect();
$start = 0;
for($i = 0; $i<$numchx; $i++)
{
$end = strpos($ida, " ", $start);
$id = substr($ida, $start, $end-$start);
$start = $end+1;
$sql = "delete from groceries where itemid = $id";
echo $sql;
//$sql_result = mysql_query($sql, $connection) or die ("Couldn't execute query.");
$sql_result = $connection->query($sql);
//mysql_free_result($sql_result);
}
$sql = "select count(*) from groceries";
//$sql_result = mysql_query($sql, $connection) or die ("Couldn't execute query.");
$sql_result = $connection->query($sql);
$row = $sql_result->fetch();
//$row = mysql_fetch_array($sql_result);
//mysql_free_result($sql_result);
$cnt = $row[0];
if($cnt == 0)
{
$sql = "truncate groceries";
//$sql_result = mysql_query($sql, $connection) or die ("Couldn't execute query.");
$sql_result = $connection->query($sql);
//mysql_free_result($sql_result);
}
else
{
//reset list so that item ids are consecutive begining at 1
$sql = "select * from groceries";
//$sql_result = mysql_query($sql, $connection) or die ("Couldn't execute query.");
$sql_result = $connection->query($sql);
$i = 0;
foreach ($sql_result as $row)
{
$itemid[$i] = $row["itemid"];
$quan[$i] = $row["quantity"];
$name[$i] = $row["itemname"];
$size[$i] = $row["size"];
$units[$i] = $row["units"];
$mfctr[$i] = $row["manufacturer"];
$notes[$i] = $row["notes"];
$time[$i] = $row["time"];
$i = $i + 1;
}
$sql = "truncate groceries";
//$sql_result = mysql_query($sql, $connection) or die ("Couldn't execute query.");
$sql_result = $connection->query($sql);
for($j = 0; $j<$i; $j++)
{
if($size[$j] == NULL)
{
$sql = "insert into groceries (quantity, itemname, units, manufacturer, notes, time) values ($quan[$j], \"$name[$j]\", \"$units[$j]\", \"$mfctr[$j]\", \"$notes[$j]\", \"$time[$j]\")";
}
else
{
$sql = "insert into groceries (quantity, itemname, size, units, manufacturer, notes, time) values ($quan[$j], \"$name[$j]\", $size[$j], \"$units[$j]\", \"$mfctr[$j]\", \"$notes[$j]\",\"$time[$j]\")";
}
//$sql_result = mysql_query($sql, $connection) or die ("Couldn't execute query.");
$sql_result = $connection->query($sql);
}
}
//mysql_close($connection);
$connection = NULL;
if ($sql_result)
{
$loc = "GroceryList.php";
header("Location: $loc");
exit;
}
include_once($WEBROOT . "/include/footer.php");
?>