Welcome to WebmasterWorld Guest from 54.163.35.238

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Foreach newbie

     

paor

11:29 am on Oct 1, 2012 (gmt 0)

5+ Year Member



Form etc...

$hct=$_POST['amm'];
foreach($hct as $key1 => $value1){echo $key1 ."--". $value1;echo "<br>";}

$bev=$_POST['alc'];
foreach($bev as $key2 => $value2){echo $key2 ."--". $value2;echo "<br>";}

The result (hectolitre, kind of drink) is:

0--2.78
1--1.21
2--0.8
3--0.34
4--0.92
5--1.79
6--3.86
7--2.6

0--beer
1--wine
2--whiskey
3--votka
4--whiskey
5--juice
6--beer
7--juice
Please php code (or tutorial) for calculate how many of each beverage was drink.

Tnx

MinosTheNinth

2:11 pm on Oct 1, 2012 (gmt 0)



In this case i would use for cycle instead of foreach.

$arrSize = count($_POST['amm']);

for($i = 0; $i < $arrSize; $i++) {
$bev = $_POST['alc'][$i];
$amm = $_POST['amm'][$i];

echo $bev, ' -- ', $amm, '<br>';
}

This should work, but I can't test it right now.

For your future projects, it would be better to use an database.

paor

4:42 pm on Oct 1, 2012 (gmt 0)

5+ Year Member



Thank you very much!

Your code works very well, but I need the total amount for each type of beverage...

like:

beer--7.60
juice--6.62
whiskey--1.72
wine--1.21
votka--0.34

(Data is coming from the form and are only for temporary purposes)

Tnx

MinosTheNinth

1:01 pm on Oct 2, 2012 (gmt 0)



Is there any given set of predefined drinks, or is it variable by input from the form?

paor

2:50 pm on Oct 2, 2012 (gmt 0)

5+ Year Member



It's variable by input from the form...meanwhile...problem is solved creating temporary_table and then fetching data


$arrSize = count($_POST['pov']);
for($i = 0; $i < $arrSize; $i++) {

$amm = $_POST['pov'][$i];
$bev = $_POST['xxx'][$i];

$in="INSERT INTO Sales (pov, xxx) VALUES ('$amm', '$bev')";
mysql_query ( $in ) or ( "Error " . mysql_error () ) ;}


$result = mysql_query("SELECT pov, xxx, SUM(pov) FROM Sales GROUP BY `xxx`");etc...

MinosTheNinth thank you very much once again!

MinosTheNinth

3:03 pm on Oct 2, 2012 (gmt 0)



Wow, clever solution. I have thinking of some solutions in advance but this even crossed my mind. Thank you for the idea :)

I'm glad, that finally i was able to actually help someone here.
 

Featured Threads

Hot Threads This Week

Hot Threads This Month