Inconsistent data when traversing through 4 tables - php

I have 4 tables (with a lot of fields, but only a few important ones in each) that I'm working with.
Customer table
Inventory table
Invoice table
Invoice details table
What I've been doing is getting a customer or item's id.
If it's a customer, I take their id to get their information from the customer table. Now, in order to get the item's they carry I have to look at each invoice with that customer's id. So I grab all of those. Each invoice has and id that links to an invoice detail table, so I take that invoice id and grab the invoice details that match with the invoice. Then the items are in the iteminventory table, so I take the item id from the invoice detail and get all of the items for every invoice associated with that customer. Then I run that data through a loop to get rid of any empty values and pass the item array to the view.
Now if it's an item the actions are performed vice versa. I take the item, look at each invoice detail containing that wine, grab the invoice id from it, and grab the customer id from each invoice respectively.
Somehow I wind up with customer's carrying a product, but when I go to the product to see what customer's carry it, the previously mentioned customer won't be listed under the product.
Is there an easier way to do this?

Most SQL programmers solve this kind of problem with a single query using a series of JOIN directives and an appropriate ORDER BY directive. They write a bit of php code to read the resultset row by row, detect rows in which the invoice id changes, and format the information appropriately.
It seems like a good idea for you to read up on how to use JOIN directives if you don't already know.

Related

PHP tasks for database

I have 2 tables in the Database.
One contains a list of Inventories with name, creation date and ID.
The second table contains individual products, and one column here contains IDs from the Inventory List table.
I write all inventories from the inventory list to the Frontend. There is a detail button next to each listed inventory and after clicking it I need to list all the products with the corresponding inventory ID.
But somehow I can't figure out what to use for it?
Could you please advise me what such a query should contain?

Add a table column into my cart list instead of row

On my website, I have a users table and industry table and industry_overview table. industry_overview contain the overview data of an industry.
I want to have a "Add to Cart" function where user can add the overview detail such as value chain, market share and market price into their cart list.(Cart list item will eventually be merged and generate a report out in pdf form)
May I know what is the simplest way I could get something like that done? I've thought about this but I can't seem to find a solution or idea.
I don't need a ready-made script, just any idea that could get me working then will be great! (Also if you already have an example of such function, I'd be more than happy to have a look at it).
Thanks in advance!
Update:
Industry overview table has 5 column:
overview_id
industry_id
value_chain
market_share
market_price
I would do it using carts and cart_items tables:
carts
=====
- id
- user_id
cart_items
==========
- id
- cart_id
- industry_overview_id
- column
If you want a user to have multiple simultaneous carts you can add a name column, which the user can change.
Any data point (column in industry_overview) selected by the user will result in an entry in the cart_items table.
However, with this method a user's cart items will automatically be "updated" if the value of the overview item changes. If you don't want this you could save the exact values from industry_overview into the cart_items table, either instead of or in addition to the id.

Database logic for online shop orders magament

I am designing as a project a web store (using PHP Laravel and MySQL FYI) and I am at the part where I have to create the logic behind the production system, which goes like this:
-On my Database,
I have 1 ORDER table where I have all the information regarding the shipping, customer, etc.
I have another table called ITEM where are listed all the Items in an order (so if an order has 3 items, there will be 3 lines in the ITEM table with a Foreign Key pointing to the ORDER).
Now I'm creating the PRODUCTION DASHBOARD. Right now I'm able to scan the item ID and get the shipment information on the Dashboard.
After that, for orders with multiple items what I want to do is for the system to tell the user to deposit the item in a numbered box to wait for the rest of the items from the order. That way the user can keep scanning items from other orders and once another item from the ordered stored in X box is produced, he can scan it and the system will then tell him that the other items from the same Order and placed on X box and he can do that until the order is complete.
My question is what would be the best way and logic Database wise (and also Laravel wise if you want to further expand your answer hehe) to implement this BOX system.
I hope my question is clear enough and thank you very much :)
I had a similar system for a project that I was working on. What I did was, was create a database table called temp_orders with a column called items that each item was separated by a line break. Until the order was finalized (100% processed), the order would remain in temp_orders.
Once finalized, it would get deleted from temp_orders and moved over to the orders table. If I needed to check items, I would explode() the data from the items column in temp_orders table using a line break, thus putting them into an array and then using the data however I needed to.
You need to determine when you want to finalize the order. It could be upon credit card payment, or upon user order confirmation, for example.

Some Issue with SQL And PHP

I am currently working on cart system for an assignment.
But, I am facing some difficulties while storing the total item to database.
I have 2 tables, which are Cart and Item table.
Cart table is used to store the Total price of the purchase and payment date.
Item table is used to store the item that the user purchased and the Item table has CartID as a foreign key.
My question is :
while I store the total and payment date to the Cart table, I also need to store the Item that the user purchased to the database. What is the value of the CartID in the Item table? Because, I first store the total and payment date to the database, how to insert the CartID in the table Items?
I have no idea.
The thing you are looking for is "LAST_INSERT_ID()". When you insert the card row you need its ID for the items foreign key.
Your queries should look something like this
INSERT INTO Cart (auto,text)
VALUES(NULL,'text'); # generate ID by inserting NULL
INSERT INTO items(auto,itemTitle,cardID)
VALUES(NULL,'text',LAST_INSERT_ID()); # use ID in second table
If you give us your exact code I can adapt the script to fit your setup but this should give you an guide to what you need.

A script to update all columns in a mysql database

Hi I am using PHP to manipulate information in my MySQL database. However I am looking for a way to update a table (all records need to be updated) based on information within another table.
For example I have a list of products lets say 10 each with a unique id stored in a products table. I have a purchases table which has the same product ID and the amount of purchases done for each product. I want to update each product in the products table to reflect the total purchases made for each product and store it in a column called instock which is part of the products table.
How can this be done?
If I understand your situation correctly, you're dealing with a stock-count. When an item is purchased (represented by a entry in the Products table) then the stock count figure should be decreased. This should happen within the same transaction as the new entry to the Products table to keep your data consistent. I would recommend using a Trigger on the table to implement this. You'll find lots of information about implementing triggers in MySQL on this site. A trigger you could use might look something like this:
CREATE TRIGGER update_stock_count
BEFORE INSERT ON Purchases
FOR EACH ROW
BEGIN
UPDATE Products SET stock_count = stock_count - NEW.quantity_ordered
WHERE product_id = NEW.product_id;
END;
This trigger doesn't take into account that there might not be enough stock of a product, nor does it handle updates or deletes on the Purchases table but it could be modified to do so.

Categories