Jump to content

Simple Grocery List


xlxprophetxlx

Recommended Posts

Hello everyone.  Looking for a little help here.  I am trying to make a simple grocery list.  I got it to print out the full array but I need another item to be added each time I add a new one then the ability to delete/modify the item.

 

Can anyone lead me in the right direction?

 

<?php 
session_start(); 
$id = $_POST['id'];
$category = $_POST['category'];
$product_name = $_POST['product_name'];
$note = $_POST['note'];
$qty = $_POST['qty'];
$_SESSION['id'] = $id;
$_SESSION['category'] = $category;
$_SESSION['product_name'] = $product_name;
$_SESSION['note'] = $note;
$_SESSION['qty'] = $qty;

function addCustomProductToSession() {
if(isset($_POST['id'])){
	$custom_items = array($_SESSION['id'], $_SESSION['category'], $_SESSION['product_name'], $_SESSION['note'], $_SESSION['qty']);
	foreach ($custom_items as $key => $custom_item) {
		$custom_items[$key] = $custom_item;
	}	
	print_r($custom_items);
}
}	
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Grocery List</title>
<script type="text/javascript">
<!--
function formsubmit(form) {
    form.action = 'index.php';
    return true;
}
//-->
</script>
</head>
<body>	
<p>Add Your Custom Product</p>
<form action="" method="post" id="cart" onsubmit="formsubmit(this);">
<div><input name="usedb" id="usedb" value="0" type="hidden" /></div>
<div><input name="id" id="id" value="<?php echo rand(400, 5000); ?>" type="hidden" /></div>
<div><input name="category" id="category" value="18" type="hidden" /></div>
<p><input size="15" id="product_name" name="product_name" onfocus="this.value=''" value="Enter Product Name" type="text" /></p> 
    <p><input size="15" id="note" name="note" onfocus="this.value=''" value="Enter Note Here" type="text" /></p> 
<p><input name="qty" id="qty" maxlength="2" size="1" value="1" type="text" /></p>
<p><input type="submit"  name="submit" value="submit" /></p>
</form>
<?php 
echo "<br /><br /><br />";
addCustomProductToSession();
?>
<p><a href="#" onclick="destroy()">Destroy Session</a></p>
</body>
</html>

 

Thanks for the help in advance!

 

-Mike

Link to comment
https://forums.phpfreaks.com/topic/202164-simple-grocery-list/
Share on other sites

You will have to store the data somewhere else besides a session var(s). So that is either a DB or a flat file. Once you have written to storage you then can use a form to add then you can:

1. list the items with a checkbox to go to edit/remove

2. you can list each entry into form inputs so you can edit/re-save multiple items at once.

 

 

HTH

Teamatomic

Link to comment
https://forums.phpfreaks.com/topic/202164-simple-grocery-list/#findComment-1060244
Share on other sites

Try this:

 

function insertProduct($id, $categoryId, $productName, $note, $quantity) {
  if ($id != abs($id) || $categoryId != abs($categoryId) || $quantity != abs($quantity))
    return FALSE;
  
  $id = (int) $id;
  $categoryId = (int) $categoryId;
  $quantity = (int) $quantity;
  
  if (0 === $id || 0 === $categoryId || 0 === $quantity)
    return FALSE;
  
  $productName = databaseEscape($productName);
  $note = databaseEscape($note);
  
  $affectedRows = databaseQuery("INSERT INTO table (id, category_id, product_name, note, quantity)
    VALUES ($id, $categoryId, '$product_name', '$note', $quantity)");
  
  if (1 !== $affectedRows)
    return FALSE;
  
  insertSession('cart', array('id' => $id, 'category_id' => $categoryId,
    'product_name' => $productName, 'note' => $note, 'quantity' => $quantity));
  
  return databaseLastInsertId();
}

function insertSession($namespace, $data) {
  if (!array_key_exists($namespace, $_SESSION))
    $_SESSION[$namespace] = array();
  
  $_SESSION[$namespace][] = $data;
}

 

And use:

 

if (array_key_exist(array('id', 'category_id', 'product_name', 'note', 'quantity'), $_POST)) {
  $insert = insertProduct($_POST['id'], $_POST['category_id'], $_POST['product_name'],
    $_POST['note'], $_POST['quantity']);
  
  if (FALSE !== $insert) {
    //success
  }
}

 

To check if something is submitted.

Link to comment
https://forums.phpfreaks.com/topic/202164-simple-grocery-list/#findComment-1060250
Share on other sites

You will have to store the data somewhere else besides a session var(s). So that is either a DB or a flat file. Once you have written to storage you then can use a form to add then you can:

1. list the items with a checkbox to go to edit/remove

2. you can list each entry into form inputs so you can edit/re-save multiple items at once.

 

 

HTH

Teamatomic

Try this:

 

function insertProduct($id, $categoryId, $productName, $note, $quantity) {
  if ($id != abs($id) || $categoryId != abs($categoryId) || $quantity != abs($quantity))
    return FALSE;
  
  $id = (int) $id;
  $categoryId = (int) $categoryId;
  $quantity = (int) $quantity;
  
  if (0 === $id || 0 === $categoryId || 0 === $quantity)
    return FALSE;
  
  $productName = databaseEscape($productName);
  $note = databaseEscape($note);
  
  $affectedRows = databaseQuery("INSERT INTO table (id, category_id, product_name, note, quantity)
    VALUES ($id, $categoryId, '$product_name', '$note', $quantity)");
  
  if (1 !== $affectedRows)
    return FALSE;
  
  insertSession('cart', array('id' => $id, 'category_id' => $categoryId,
    'product_name' => $productName, 'note' => $note, 'quantity' => $quantity));
  
  return databaseLastInsertId();
}

function insertSession($namespace, $data) {
  if (!array_key_exists($namespace, $_SESSION))
    $_SESSION[$namespace] = array();
  
  $_SESSION[$namespace][] = $data;
}

 

And use:

 

if (array_key_exist(array('id', 'category_id', 'product_name', 'note', 'quantity'), $_POST)) {
  $insert = insertProduct($_POST['id'], $_POST['category_id'], $_POST['product_name'],
    $_POST['note'], $_POST['quantity']);
  
  if (FALSE !== $insert) {
    //success
  }
}

 

To check if something is submitted.

 

Instead of involving a database would it be possible to use a multi-dimensional array or just have a cookie that stores the information?  This doesn't need to be secure, it just for random users to create a quick grocery list.

 

Like array(array1, array2, array3, array4, ect...) and each array contains the id, category, product_name, note, qty of each custom product added.  Possibly have a mini cart that loops through the arrays and shows the product name, qty and a delete next to it.  The delete would use unset what ever the that array would be.  Under the mini cart would have a view whole cart which would be exactly like the mini cart but with the addition of showing the note.

 

Thanks for the help.

Link to comment
https://forums.phpfreaks.com/topic/202164-simple-grocery-list/#findComment-1060595
Share on other sites

You can use sessions/cookies but:

 

1) if you use session's and forget to set cookie lifetime the user loses his list upon closing his browser

2) if the user has set (or if in a corporate network the policy is set) to remove all cookies upon browser closing then again he will lose his list.

 

Nothing else but a database (in any form: flat file, dbms) can certify data persistence.

Link to comment
https://forums.phpfreaks.com/topic/202164-simple-grocery-list/#findComment-1060636
Share on other sites

You can use sessions/cookies but:

 

1) if you use session's and forget to set cookie lifetime the user loses his list upon closing his browser

2) if the user has set (or if in a corporate network the policy is set) to remove all cookies upon browser closing then again he will lose his list.

 

Nothing else but a database (in any form: flat file, dbms) can certify data persistence.

 

Correct.  There really is no need for the data to be saved after the browser is closed.  I'm just looking for an instance.  Adding a lifespan of a day on the cookie would be the longest time period I foresee needed if any.  With the multi-dimensional array I was thinking each array would increment by one (i++) with a loop: 0 => Array1,  1 => Array2, 2=>Array3 ect..  Just unsure the exact order to write the loop to allow items to be added.

Link to comment
https://forums.phpfreaks.com/topic/202164-simple-grocery-list/#findComment-1060645
Share on other sites

session_set_cookie_params(86400);//1 day
session_start();

if (!array_key_exists('list', $_SESSION))
  $_SESSION['list'] = array();

$requiredFields = array();
if (array_key_exists($requiredFields, $_POST)) {
  ..
}

 

session_set_cookie_params() needs to be called before session_start() as the session cookie may expire and we want to be sure each created session cookie will have a lifespan of 1 day.

Link to comment
https://forums.phpfreaks.com/topic/202164-simple-grocery-list/#findComment-1060664
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.