September 21, 201313 yr Ok let me see how to explain this: I have a database with shoes and sock with a cost. The user goes thru and a series of steps in a form and it saves to the database. I wrote calculations that calculates the cost of a shoe and the cost of a socks but i worked it out on a sheet called 'total.php'. $nike, $reebok and for socks $blue, $red So a person can choose a nike shoe and blue sock, or a nike shoe and red sock. Or the person can choose a reebok shoe and a blue sock or a reebok shoe and red sock. So i use a select statement to recall what was chosen : SELECT * FROM Choice where memberid = $userid; and this works. What do i do next: Do i use an if, else statement?? My predicament is how do i go about selecting the cost of a shoe and sock and echoing the results on a separate sheet?
September 21, 201313 yr Assuming you have a database structure as such: Table: shoes +--------+---------------+----------+ | ShoeID | Name | Cost | +--------+---------------+----------+ | 1 | Nike Shoe One | 9.99 | | 2 | Nike Shoe Two | 49.99 | +--------+---------------+----------+ Table: socks+--------+------------+----------+ | SockID | Name | Cost | +--------+------------+----------+ | 1 | Red Socks | 4.99 | | 2 | Blue Socks | 5.99 | +--------+------------+----------+ Table: choices+----------+--------+--------+----------+--+ | ChoiceID | ShoeID | SockID | MemberID | | +----------+--------+--------+----------+--+ | 1 | 1 | 2 | 1 | | | 2 | 1 | 1 | 2 | | | 3 | 1 | 1 | 1 | | | 4 | 2 | 1 | 5 | | +----------+--------+--------+----------+--+You can use JOIN to select the appropriate costs and sum them up
September 21, 201313 yr Author I built the tables but in the choice table i used the names instead not the shoeid or sockid. I can rework this. The reason i used the names is for when the admin logs in they can view by the orderid or memberid and the exact details will show. I built the calculations separate using php on a page called total.php. My question is how do i calculate the total based on the choice from the table. e.g choice1 in ur table how do i select the shoe price e.g SELECT * FROM Choice where choiceid = $memberid; then i echo the result. Do i use an IF/ELSE statement ... but i am stuck here i dont know what to do. I failed my final yr project and i really need some help. Thanks for the response and help
September 21, 201313 yr Ideally, you should always use an auto_increment ID on table items. Even if your not using it, it will come in handy at one point or another. See below for examples on sql joins. http://www.w3schools.com/sql/sql_join.asp
September 21, 201313 yr Using my table structure, you would use a query along the lines of: SELECT shoes.name as Shoe, socks.name as Sock, (shoes.cost+socks.cost) as TotalOrderCost, choices.MemberID as MemberID FROM choices JOIN shoes on choices.ShoeID = shoes.ShoeID JOIN socks on choices.SockID = socks.SockID WHERE choices.MemberID = 1Switch the MemberID accordingly. That will return the following result: +---------------+------------+----------------+----------+ | Shoe | Sock | TotalOrderCost | MemberID | +---------------+------------+----------------+----------+ | Nike Shoe One | Blue Socks | 15.98 | 1 | | Nike Shoe Two | Red Socks | 14.98 | 1 | +---------------+------------+----------------+----------+ Edited September 21, 201313 yr by andy9l
Create an account or sign in to comment