homepage Welcome to WebmasterWorld Guest from 54.224.214.109
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Foreach newbie
paor




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

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




msg:4502481
 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




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

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




msg:4502936
 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




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

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




msg:4503016
 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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved