Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Selecting multiple values from a database

Featured Replies

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?

 

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 :)
  • 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

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 = 1
Switch 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 by andy9l

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.