php calculate two columns to equal total
Say I have two drop down boxes, and I have two columns then after I select the two columns that pulls from the stock tables to give me a total.. Whats the best way or does anyone have an example?
Ex Boxes - Yellow box - Red box = All the yellow and red boxes in stock ...
ex of a table boxes -
yellow box red box - 6 in stock
yellow box pink box - 7 in stock
yellow box red box 8 in stock
two drop downsthen = total
total <red > <yellow boxes> = 14 boxes
any suggestions or pointers would be greatly appreicated.
Thanks
xbat
Junior Poster in Training
83 posts since Jun 2012
Reputation Points: -4
Solved Threads: 0
Skill Endorsements: 0
If that is correct, you have two options.
Do the math in the database
Is that what you're
"SELECT col1, col2, (SELECT SUM(col1, col2)) as total FROM mytable WHERE boxcolor='whatever';"
Do the math after the database
$sql= "SELECT yellowbox, redbox , SUM(yellowbox,redbox) as TOTAL from table_name where boxcolor = 'SELECTED COLOR'";
then do the PHP code:
then do the PHP code:
> while ($row =mysql_fetch_assoc($sql)){
> $total = $row['TOTAL'];
> // If this ain't going to work used this
> //$total = intval($row['yellowbox'] + $row['redbox']);
> //Lets assume your input are all integer.
> echo $total;
> }
**Thank you.. but with this it wouldn't be dynamic. I need something where people can select what box color, then after selecting the box colors then it would total the amount of selected box colors through the database.
**
> $sql= "SELECT yellowbox, redbox , SUM(yellowbox,redbox) as TOTAL from table_name where boxcolor = 'SELECTED COLOR'";
>
> then do the PHP code:
>
> while ($row =mysql_fetch_assoc($sql)){
> $total = $row['TOTAL'];
> // If this ain't going to work used this
> //$total = intval($row['yellowbox'] + $row['redbox']);
> //Lets assume your input are all integer.
> echo $total;
> }
thank you by with this it would only total yellowbox and redbox, I'm looking at drop down to select the colors, then it would calulate the total that is in stock of the colors selected. I was thinking I would be able to get away with php but I think I might have to do javascript.... my javascript is rusty.
xbat
Junior Poster in Training
83 posts since Jun 2012
Reputation Points: -4
Solved Threads: 0
Skill Endorsements: 0
xbat
Junior Poster in Training
83 posts since Jun 2012
Reputation Points: -4
Solved Threads: 0
Skill Endorsements: 0
xbat
Junior Poster in Training
83 posts since Jun 2012
Reputation Points: -4
Solved Threads: 0
Skill Endorsements: 0
This has confused the hell out of me.
Could you give an example of the dropdown contents and your table structure with a couple of datarows as an example?
diafol
Keep Smiling
10,665 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,513
Skill Endorsements: 57
If I have understood you correctly, then you need to retrieve the total stock value from the database for all boxes that match either the first, or second colour selected.
Without seeing your database schema, I cannot write an exact query, however hopefully the following will point you in the right direction.
// This assumes two valid colours have been selected
$colour_1 = 'red';
$colour_2 = 'yellow';
// Find and sum stock value for rows where colour is red or yellow
$sql = sprintf("SELECT SUM(`stock`) AS `stock` FROM `table` WHERE `colour` IN ('%s', '%s')", $colour_1, $colour_2);
blocblue
Practically a Posting Shark
837 posts since Jan 2008
Reputation Points: 272
Solved Threads: 161
Skill Endorsements: 12
diafol - Sorry.. yes now that I read it months later I was like man what was I writing... But Blocblue yes you are correct... Thats what i was looking for... Thank you..
I needed to add up all the different but matching itmes say for example I have a chair factory..
so I have the options of -
Here is my table -
Column 1 Leg color - Column 2 Seat Color - Column 3 chair handle color - Stock
brown - black - pink - 4
brown - white - pink - 5
brown - white - pink - 7
brown - black - yellow - 9
Now lets say I have a gaint database and I just want to add the totoal number for all brown white and pink witch will total - 12
Thats all I was trying to explain.
Thanks... I'm not sure if there is a better way of doing this.. but I was using drop down select menus.
xbat
Junior Poster in Training
83 posts since Jun 2012
Reputation Points: -4
Solved Threads: 0
Skill Endorsements: 0
Anyone? Even If I can do two that would be wonderful...
xbat
Junior Poster in Training
83 posts since Jun 2012
Reputation Points: -4
Solved Threads: 0
Skill Endorsements: 0
I figured this one out...Turns out I was just missing some basic where item = thing AND item = thing
xbat
Junior Poster in Training
83 posts since Jun 2012
Reputation Points: -4
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 6 Months Ago by
diafol,
blocblue,
cigoL..:)
and 1 other