Saving dynamic list items efficiently - php

I have supermarkets stored in my database. For each supermarket, in an admin area the user can create a shopping list with products and keep track of the products by checking (= user has already bought the product) or unchecking them (= user still needs to buy the product). My question is how to efficiently store the shopping list items in MySQL?
My markup with example data:
<form>
<h1>Supermarket: "City Store"</h1>
<p>Create/edit your shopping list:</p>
<ul id="shopping-list">
<li><input type="text">Tomatoes</input> <input type="checkbox"></li>
<li><input type="text">Apples</input> <input type="checkbox" checked="checked"></li>
<li><input type="text"></input> <input type="checkbox"></li>
</ul>
<button id="add-list-item">Add list item</button>
<button id="save-list-items">Save</button>
</form>
$('#add-list-item').on('click', function() {
$('#shopping-list').append('<li> <input type="text"></input><input type="checkbox"></li>');
});
In this scenario, when the user logs in, he sees that in the past he already added two products to the list, tomatoes and apples. As he already has bought apples, he had checked them. If he wants to add products, he can press the button.
In my MySQL environment I already have a table "supermarkets". For storing the shopping list items I want to create an additional table "shopping-list-items" with the following fields:
- id
- id_supermarket
- content
- status (1 = 'checked', 0 = 'still unchecked')
- date_created
- date_updated
- date_checked
As you can see, I want the date of each list item being saved, for when it is being created, updated AND checked.
How can I send the data of the shopping list to my database via POST and distinguish between checked and unchecked items? I basically know how to send data via the name attribute to a database and to save them, but in this case I want to save each entry individually and at the same time consider the checkbox state and I have no idea how to handle that.

Related

How do I store data in mysql database from a form where unique fields are dynamically added?

Other people use "dynamic fields" definition meaning to clone same fields over and over, but that's not how I'm defining "dynamic fields". I have a script where I manage categories and products in a single table, then I use that table to display a form with text fields on a different page. So my question is, how do I create the next table and the php for inserting into that 2nd table so it saves the dynamic fields? Fields may be added or removed at anytime and the new table and insert statement should be able to be flexible and accommodate that.
Here is an example of records in 1st table (categories_products):
id label altname parent sort
1 cat2 cat 0 4
2 subcat1 cat 1 5
3 cat1 cat 0 1
4 subcat2 cat 1 9
5 product 1a product_1a 3 2
6 product 2a product_2a 2 6
7 product 2b product_2b 2 7
8 product 3a product_3a 4 10
9 product 3b product_3b 4 11
10 new product new_product 3 3
Info about cat/prod table: altname ends up being the names of form fields, and the ones with "cat" in alt name never become fields, they are titles of categories and subcategories. here is example of generated form below. i added one extra field (company) which i will also need to save.
<form class='' id='form-id'>
Company: <input type="text" name="company" value="">
<ul class='a'>
<li class="b">
<p><strong>cat1</strong></p>
<ul class='a'>
<li class="c"><p>product 1a: <input type="text" name="product_1a" ></p></li>
<li class="c"><p>new product: <input type="text" name="new_product" ></p></li>
</ul>
</li>
<li class="b">
<p><strong>cat2</strong></p>
<ul class='a'>
<li class="b"><p><strong>subcat1</strong></p>
<ul class='a'>
<li class="c"><p>product 2a: <input type="text" name="product_2a" ></p></li>
<li class="c"><p>product 2b: <input type="text" name="product_2b" ></p></li>
</ul>
</li>
<li class="b">
<p><strong>subcat2</strong></p>
<ul class='a'>
<li class="c"><p>product 3a: <input type="text" name="product_3a" ></p></li>
<li class="c"><p>product 3b: <input type="text" name="product_3b" ></p></li>
</ul>
</li></ul>
</li>
</ul>
<input type="submit" name="submit" value="Submit">
</form>
The point of this is that different companies get different pricing. Prices are what will be typed into form fields. And the point of having all the categories and products generating the form is because all companies get all the same categories and products. Then later admins will think of new products and go in and add the new products, so the new products then need to be part of the new form for saving.
So at this point, I don't know what table structure to use for saving records of company prices. I keep hearing that it's not wise to create and destroy table columns on-the-fly, so what is the better way? Should all form field values get saved into a single table column? but how? how can this be dynamic and what will happen to the data of this column for past rows when new form fields are created?
I started this new table, but this is where it ends and I don't know what to do next or how to store data of dynamic form.
table name: product_pricing
id
company
price
and the following might be an example of a couple records, but I really wouldn't know how to make this happen:
id company price
1 abc {product_1a:0.01,product_2a:0.01,product_2b:0.01,product_3a:0.01,product_3b:0.01,new_product:0.01}
2 xyz {product_1a:0.02,product_2a:0.02,product_2b:0.02,product_3a:0.02,product_3b:0.02,new_product:0.02}
or will this be better since names of products might change? save by ID's instead:
id company price
1 abc {5:0.01,6:0.01,7:0.01,8:0.01,9:0.01,10:0.01}
2 xyz {5:0.02,6:0.02,7:0.02,8:0.02,9:0.02,10:0.02}
I can worry about how I will retrieve and parse this column data later, but I need to get passed this 1st hurdle of how I am going to save/store the data via php and mysql code. Obviously there will be an edit page for the the records to edit prices, so update will need to resave the data properly. this is what I am worried about. how data will always be in correct spot no matter what is done to the first table (adding and removing of products).
Right let's separate this out. You have:-
Categories
Products
Companies
A category can contain many categories and many products, a product can be connected to many companies and each company can have a unique price per product, correct?
If so, what I'd suggest is:-
Category Table:
Category ID, Category Name, Category Alt Name, Parent Category, Order
Product Table:
Product ID, Product Name, Product Alt Name, Category ID, Order
Company Table:
Company ID, Company Name
Company Product Link Table:
Link ID, Company ID, Product ID, Product Price
At this point, you don't need any dynamic columns, as everything has been separated out. Let's say you add a product that you only want certain companies to have/have prices for, you only insert links for those companies in the Link table, then your SQL for the form would be:-
SELECT
cplt.link_id,
pt.product_name,
pt.product_alt_name,
cplt.product_price
FROM
company_product_link_table cplt
INNER JOIN
product_table pt
ON
pt.product_id = cplt.product_id
WHERE
cplt.company_id = PUT COMPANY ID HERE
The names of your input fields for the prices can then just be the link ID in the table, then to update them it's just a case of
UPDATE
company_product_link_table
SET
product_price = PRODUCT PRICE
WHERE
link_id = NAME OF FIELD

How to make a user wish list in PHP

So, basically I'm making a little site that has some pages with video game's info and media. I have a user account system setup, but there's something I've been wanting to know how to add. I'm still new to PHP, and I made the user account system only by following a tutorial. Anyways, here's the HTML code that shows on a game info page when a user is logged in:
<form name="wishlistAdd" action="wish_list.php">
<input type="checkbox" name="wagakkiwa_wish" id="wagakkiwa_wish" />
<label for=wagakkiwa_wish>Add <b>WAGAKKIWA</b> to your Wish List?</label><br />
<input type="submit" class="button" value="Add to List" /> This is a alpha feature. It won't work currently.
</form></div>
Basically, what I'm wanting to achieve is if the user logged in checks the box then clicks 'Add to List' it will add it to a list in their User Account Home page under the section 'Wishlist.' For example, if I added that game to my wishlist and I went back to the /user/ section of the site, in the right column under 'Wishlist' it would show 'WAGAKKIWA' as an a tag to that page. Then, if I did it for another game it would add it ontop.
So, what would I need to do to accomplish this? Do I have to add a certain column to the user's table in my DB and have the posted items for the wishlist submitted and retrieved?
Any and all answers are appreciated, thank you.
for the table wishlist, the fields are : Id,UserIP, ProductsID
Id only autoincrement ID
UserIP to record visitor's IP
ProductsID to record ProductsID which cliked by visitor
After you click submit, then you must insert record to table wishlist, then to display you must select all table wishlist and left outer join with table product with ProductsID.
Hopefully you understand with my short explanation

Can't get my $_SESSION right

I hope someone can help me with some troubles I'm having. Currently I'm trying to make a simple webshop. First some information:
I have 2 kinds of products, product a and product b. Both of the products are available in 18 colours and 3 sizes. When you want to buy product a, you simply go to the page of product a, select your colour and your size, and then you add it to your basket.
The problem I'm having is that I can't manage to store the colour and size in the session as wel. Right now I'm storing the id of the product and the quantity in a session like this:
if (isset($_GET['add'])) {
$_SESSION['cart_'.(int)$_GET['add']]+='1';
}
The outcome of the session is: cart_1 1. It means that of the product with the ID 1, 1 has been added to the shopping basket. But what I want to do is that my session also can tell me which colour and size has been submitted. ($_POST['colour'] and $_POST['size']. The problem is that when I store it like this, I will only be able to order 1 of product a and 1 of product b because whenever I want to add another one of product a the 1 from cart_1 will be the same as the one with a different colour.
Thanks in advance!
Assuming that you got all options wrapped in a proper form then you can give each input an id.
Once you're on the current page which handles the form then you can easily access the inputs by referring to their ids.
Example:
<form id="mainForm" method="POST" action="scriptUrl.php">
<input type="text" id="color" value="" />
<input type="text" id="size" value="" />
<input type="submit" id="submitFormButton" value="Submit" />
</form>
Then you can easily access the values as such (once form submitted):
$color = isset($_GET['color']) ? $_GET['color'] : "";
$size = isset($_GET['size']) ? $_GET['size'] : "";
using the ternary operator.
And you can add this to your sessions if you want, it's preferred to have a single session for a single value, though. That is, if you feel that you really have to use sessions; there are other options, sometimes more suitable.
I know you're not using input texts but you can easily modify your example as such.
It differs between people whether they use GET or POST, I prefer POST.

How to make a database fill out parts of a form?

I am looking for information on how I can use a database to fill in parts of a form.
I have a Notify Me form that will be available on two different pages. On the first page, the customer will have to fill out the entire form. (Ensuring I know what item they want to notified about.)
On another page is a database in which I will have all the items listed plus item that are not yet available. (The database entry will be seen before the item is available and the notify me form will be a marketing tool the item.)
I want to include a link to the same Notify Me form, as seen on the first page, in the database, however, I would like this form to check the database to see if the item is already entered, and if so, fill out that information in the form.
Notify Me form
Field 1: Customer Name
Field 2: Email
Field 3: Model Number of item
MySQL Database
Field 1: Type
Field 2: Model Number
Field 3: Price
Field 4: Link to form
So, as you can see the Model Number is in both the form and the database. If my customer clicks on the link for the form from within the database (Field 4), I want Field 3 of the Notify Me form to be automatically filled out with the value in Field 2 of the database with in the form opens.
Not quite sure what you're asking, but maybe this is what you need?
<?php
$modelNumber = mysql_fetch_array(mysql_query("SELECT ModelNumber FROM ? WHERE ?"));
?>
<input type="text" name="modelNum" value="<?php if ( $modelNumber != "") { echo $modelNumber; } ?>"/>

HTML form with multiple submit options

I'm trying to create a small web app that is used to remove items from a MySQL table. It just shows the items in a HTML table and for each item a button [delete]:
item_1 [delete]
item_2 [delete]
...
item_N [delete]
To achieve this, I dynamically generate the table via PHP into a HTML form. This form has then obviously N [delete]-buttons. The form should use the POST-method for transfering data.
For the deletion I wanted to submit the ID (primary key in the MySQL table) of the corresponding item to the executing php skript. So I introduced hidden fields (all these fields have the name='ID' that store the ID of the corresponding item.
However, when pressing an arbitrary [delete], it seems to submit always just the last ID (i.e. the value of the last ID hidden field).
Is there any way to submit just the ID field of the corresponding item without using multiple forms? Or is it possible to submit data from multiple forms with just one submit-button? Or should I even choose any completly different way?
The point why I want to do it in just one single form is that there are some "global" parameters that shall not be placed next to each item, but just once for the whole table.
<input type="submit" name="delete[1]" value="delete">
if (isset($_POST['delete'])) $id=key($_POST['delete']);
it seems to submit always just the last ID
It submits all of them, but since the name doesn't end with [], PHP discards all by the last.
Is there any way to submit just the ID field of the corresponding item without using multiple forms?
No. At least not without some unfortunate JavaScript. All (non-disabled) hidden inputs (with names and values) will be successful. You can't limit based on proximity to a clicked input element.
If I understand your goals correctly, you have two main options.
Put one form per row (in the cell with the delete button)
Encode the id value into the name of the submit button
You could get rid of the hidden fields and name your submit buttons like this:
<input type="submit" name="delete[1]" />
<input type="submit" name="delete[2]" />
<input type="submit" name="delete[3]" />
and then
<?php
if (isset($_POST['delete'])) {
$toDeleteId = key($_POST['delete']);
}

Categories