PHP - Grouping And Adding Form Values
Hi, I would like to know if there's a way to quickly collect and sort form data, then use that data in an if statement. I've tried for a while now and i just can't seem to figure it out.
I would like to do this with radio buttons. Hopefully this will make it clear: Code: Code: [Select] <?php if (isset($_POST['submit'])) { // I don't know how to do this but maybe something like // if $_POST values == "Yes" // put them into an array // then count how many are in the array, e.g. 2 if 2 "Yes" radio buttons are selected $count = count($array) . " Yes values selected"; echo $count; //else if do the same for "No" values // hopefully this is understandable } ?> <!-- i want to take these values and group them, so all the "Yes" values selected together and the "no" values together --> <form method="post" action=""> <input type="radio" name="first" value="Yes" /> <input type="radio" name="first" value="No" /> <input type="radio" name="second" value="Yes" /> <input type="radio" name="second" value="No" /> <input type="submit" name="submit"> </form> Similar TutorialsI'm using smarty and php to collect parameters from a user defined tag in cmsms that looks like this... Code: [Select] {schedule date="2010-11-13" time="7:05 p.m." location="Evansville" home="Oakland City" vs="Evansville" winner="University of Evansville" results="82-42"} {schedule date="2010-11-15" time="7:00 p.m." location="Oakland City" home="Oakland City" vs="OSU-Lima" winner="OSU-Lima" results="68-67 2OT"} etc... As you can see each {schedule tag above has the same home="" value. Thats because its schedule dates just for THAT team (i.e. Oakland City). So I may have 10 of these tags on the same page. But I also have other places where the schedule tag is used with a different $home name for another team. So basically I'm calling ALL these schedule tags into one for loop like this... Code: [Select] for ($i=0; $i < count($params['date']); $i++) { $date= $params['date']; $time=$params['time']; $location= $params['location']; $home= $params['home']; $vs= $params['vs']; $winner= $params['winner']; $results= $params['results']; $gameinfo = array("date", "time","location","home","vs","winner","results"); $game_info = compact($gameinfo); } All I want to do is spit back out the total wins for each $home team. I am pulling out my hair out trying to get this thing to work but no luck yet, even though I see a little light. This is what I got so far... Code: [Select] for ($i=0; $i < count($params['date']); $i++) { $date= $params['date']; $time=$params['time']; $location= $params['location']; $home= $params['home']; $vs= $params['vs']; $winner= $params['winner']; $results= $params['results']; $gameinfo = array("date", "time","location","home","vs","winner","results"); $game_info = compact($gameinfo); if($home == $winner) { $wins = array('team' => $winner, 'win' => count($winner)); } } $smarty->append('mwins',$wins); print '<pre>'; print_r($wins); print '</pre>'; and I get this... Code: [Select] Array ( [0] => Array ( [team] => Oakland City [win] => 1 ) [1] => Array ( [team] => Oakland City [win] => 1 ) [2] => Array ( [team] => Oakland City [win] => 1 ) [3] => Array ( [team] => Oakland City [win] => 1 ) [4] => Array ( [team] => Oakland City [win] => 1 ) [5] => Array ( [team] => Oakland City [win] => 1 ) [6] => Array ( [team] => Oakland City [win] => 1 ) [7] => Array ( [team] => Oakland City [win] => 1 ) [8] => Array ( [team] => Oakland City [win] => 1 ) [9] => Array ( [team] => Oakland City [win] => 1 ) [10] => Array ( [team] => Oakland City [win] => 1 ) [11] => Array ( [team] => Oakland City [win] => 1 ) [12] => Array ( [team] => Oakland City [win] => 1 ) [13] => Array ( [team] => Oakland City [win] => 1 ) [14] => Array ( [team] => Oakland City [win] => 1 ) [15] => Array ( [team] => Oakland City [win] => 1 ) [16] => Array ( [team] => Oakland City [win] => 1 ) [17] => Array ( [team] => Southern Indiana [win] => 1 ) [18] => Array ( [team] => Southern Indiana [win] => 1 ) [19] => Array ( [team] => Southern Indiana [win] => 1 ) [20] => Array ( [team] => Southern Indiana [win] => 1 ) [21] => Array ( [team] => Southern Indiana [win] => 1 ) [22] => Array ( [team] => Southern Indiana [win] => 1 ) [23] => Array ( [team] => Southern Indiana [win] => 1 ) [24] => Array ( [team] => Southern Indiana [win] => 1 ) [25] => Array ( [team] => Southern Indiana [win] => 1 ) [26] => Array ( [team] => Southern Indiana [win] => 1 ) [27] => Array ( [team] => Southern Indiana [win] => 1 ) [28] => Array ( [team] => Southern Indiana [win] => 1 ) [29] => Array ( [team] => Southern Indiana [win] => 1 ) [30] => Array ( [team] => Southern Indiana [win] => 1 ) [31] => Array ( [team] => Southern Indiana [win] => 1 ) [32] => Array ( [team] => Southern Indiana [win] => 1 ) [33] => Array ( [team] => Southern Indiana [win] => 1 ) [34] => Array ( [team] => Southern Indiana [win] => 1 ) [35] => Array ( [team] => Southern Indiana [win] => 1 ) [36] => Array ( [team] => Southern Indiana [win] => 1 ) [37] => Array ( [team] => Southern Indiana [win] => 1 ) etc... I want to take all the [win] values for each [team] and add them up this way my final array looks like this... Code: [Select] Array ( [0] => Array ( [team] => Oakland City [win] => 18 ) [1] => Array ( [team] => Southern Indiana [win] => 24 ) I know I'm so close but don't know how to solve the condition I need. Any suggestions would be great!!! bh Hey! How've you been? This should be simple but I can't make it work. I have a few database values from a user entry. The page queries the database and I can print them no problem. I want to add them and display the total of these values. I've tried about two dozen ways of doing and I just can't get them to add. Where I have "fee_total" I want it to be the sum of the values "fee+upgrade7+tax". This is the snippet of what I'm working on. I won't attach any of what I have tried because, well, there are too many attempts and nothing worked $sql = ("SELECT * FROM Teamregister WHERE pkSoloregisterID=$pkSoloregisterID"); $query = mysql_query($sql); $result = mysql_query($sql); $myrow = mysql_fetch_array($result); printf ('<form action="https://www.mysite.com/cgi-bin/webscr" method="post"> <p>Your registration fee is <strong>$%s</strong>.</p>', $myrow['total_fee']); echo ("<input type='hidden' name='item_name_1' value='Registration Fee'>"); printf ('<input type="hidden" name="amount_1" value="%s">', $myrow['total_fee']); echo ("<br /><div align='center'> <input type='submit'' value='Pay Online'> </form>'); Make sense? Like I said, the values are already there but the math isn't happening. To check I tried using individual DB values instead of "total_fee" and the numbers show up. Just cannot get them to add. Hey guys: If I some multi-dimensional arrays, like so: Code: [Select] array { array { [0]=> "Path1" [1]=> "10" } } array { array { [0]=> "Path2" [1]=> "16" } array { [0]=> "Path3" [1]=> "110" } } How can I add all of the values of key 1? I know its a foreach loop, but I can't figure it out... would it be this? I have a store number that i want to add and echo. This resembles an invoice so i my table has unit_price and total_value which is defined by by multiplying unit_price and pieces. Now that i want to print invoice i want to know how to add all items that are from that invoice_id Code: [Select] //get the total value for order $sql_value = "SELECT total_value FROM `".TABLE_ORDERDETAILS."`WHERE order_id = $record_order[order_id]"; $rows_value = $db->fetch_all_array($sql_value); foreach($rows_value as $record_value){ $value = $record_value[total_value]; } I have the following multidimensional array where each sub-array represents quantity[0] and item #[1]: Array ( => Array ( => 1 [1] => 12113 ) [1] => Array ( => 2 [1] => 21200 ) [2] => Array ( => 4 [1] => 10200 ) [3] => Array ( => 1 [1] => yyyy ) [4] => Array ( => 20 [1] => xxxxx ) [5] => Array ( => 5 [1] => 21200 ) I'm trying to consolidate like items. For example I need to create a new array that reflects any duplicate part numbers with a combined quantity. (ie. ... => 7 [1] => 21200 ... Code: [Select] $product_id=$_GET['product']; $sql500="SELECT * FROM $tbl_name3 WHERE product_id='$product_id'"; $result500=mysql_query($sql500); $num_rows500=mysql_num_rows($result500); while($row500=mysql_fetch_array($result500)){ extract($row500); $review_product_rating_total=$review_product_rating; //What do I do here? } $average_rating=$review_product_rating_total/$num_rows500; I need a way to take a column of product ratings (0-5 in 1/2 increments), add them together, and divide by the number of rows taken from the database. I know how to get the number of rows with mysql_num_rows. But how would I add together the data from database? hello, how would i add the values off all results in the $pamount array? Code: [Select] $result2 = mysql_query("SELECT * FROM pobook WHERE jobnumber = '$jobid'"); while($row2 = mysql_fetch_array($result2)) { $pamount=$row2['amount']; echo "<tr>"; echo "<td>" . $pamount . "</td>"; echo "</tr>"; echo "<tr>"; echo "<td>VALUE OF ALL RETURNED $pamount HERE</td>"; echo "</tr>"; Hi, I have a database with some rows. in each row there is a number such as: 0.0023 or 0.0135... I have the following code to loop though them and add the numbers up but it always returns 0? Code: [Select] $sql="SELECT * FROM tickets WHERE `round` = '$round' AND `confirmed` = 'yes' ORDER BY id ASC"; $result=mysql_query($sql); $row=mysql_fetch_array($result); while($row = mysql_fetch_array( $result )) { $balance = $balance + $row['amount']; } Hi, I currently have a string with some values in it: $a_random_string="some random text, goes here - $123<br>some more random text goes here - $53 (text 123 )<br>ANother line of text - $126"; Now I want to add all the values that have a '$' before the value eg; $123. So in this case I would end up with: 302 How would I do this? Thanks, mme Hi I am gathering numbers in an array and then want to add together all the numbers attached to the same email so I end up with the Grand totals of each email address. Below is the code I am using to pull out the email and attached values Code: [Select] $getcashdonationsdb = $wpdb->get_results("SELECT * FROM cashdepo WHERE cause='$causename' AND status='2' ORDER BY user DESC"); $i = 0; while($i < $count2) { foreach($getcashdonationsdb as $getcashdonationsdb){ $dataa[$i]['email'] = $getcashdonationsdb->email; $dataa[$i]['cost'] = $getcashdonationsdb->cost; } } Anyone have any suggestions how I can achieve this most efficiently. Good day guyz... I dont know why this code does not work... the connection is fine, the database is also fine it works with the localhost, when i upload it on free hosting site like 000webhost and infinityfree this is not working.. the database on free hosting site are also working also the connection is code, just only this code i cant figure it out.. else{ $sql = "INSERT INTO .$name (TrxID, ProductName, Price, ProductImage, PurchasedDate, PurchasedStatus) VALUES (?, ?, ?, ?, ?, ?)"; $stmt = mysqli_stmt_init($conn2); if(!mysqli_stmt_prepare($stmt, $sql)){ echo 'connection error'; } mysqli_stmt_bind_param($stmt, "isssss", $val, $PName, $Price, $PImage, $PDate, $PStatus); mysqli_stmt_execute($stmt); mysqli_stmt_close($stmt); header("location: ../customer/cart.html"); exit(); } }
Hope someone can help, I am trying to add up all the values of a variable in a PHP while loop and have tried several approaches but none seem to work. Hopefully someone can point out what the correct method is. I've set up some code to pull the pay rate from a MySQL table, and then calculate that person's earnings based on the amount of time they spend on a particular job. That works fine:
However there may be a number of jobs for different people with different rates, so I want to add all of these up and output the total. So for example if I had values of 125.00, 35.50 and 22.75 I want to show 183.25 as the total. So I tried a few things I found on forums on the subject:
That didn't work so I tried another method that someone suggested:
The 2nd method outputted just the last individual value, the 1st method didn't work at all. Does anyone have a working method that can be used to do this? Thanks Hi here is my problem, i have a link like : index.php?id=1 When user click on it, a cookie array is set with a value of 1 when user clicks on another link with an id of 2, the same cookie is set with another value of 2, at the end , the cookie should read: Code: [Select] array( [0] => 1 [1] => 2 ) Can someone please explain how to do this? im struggling :S Hello, I am trying to add all rows in the same column together. So here is the basic scheme: id l column1 1 l 1 2 l 2 3 l 3 Second table id l variable1 1 l 3 2 l 5 3 l 6 I basically want to add 5+3+6 and set it to a variable named '$total'. Would this be done using while loops? Here is the code I have been messing with. $sql="SELECT column1 from table1 where id='$userid'"; // userid would be set near the top of the page $sql2=mysql_query($sql,$conn); while($rows=mysql_fetch_array($sql2)){ $getid = $rows['column1']; $sql3="SELECT variable1 FROM table2 where id='$getid'"; $sql4=mysql_query($sql3,$conn); while($items=mysql_fetch_array($sql4)){ // Then I'm getting lost... } } Can you at least see what I am trying to do? I'm not even sure if this is the right approach, any help would be appreciated! I am trying to get info from a table, add rows in that table, and put the added value back into the table in another field...I want to add field1 and field2 of every row and put the total in the total row. Heres what the echo gives me Susan 14 Mary 18 Bob 13 Sam 21 heres the database mysql> SELECT total_val FROM table; +-----------+ | total_val | +-----------+ | 21 | | 21 | | 21 | | 21 | +-----------+ heres the code.... <?php require("connection.php"); mysql_select_db("database", $connection); echo "<br />"; $result = mysql_query("SELECT * FROM table"); while($row = mysql_fetch_array($result)) { $values = ($row['field1'] + $row['field2']); $sql=mysql_query("UPDATE table SET total = '$values'"); echo $row['user_name'] . " " . $values; echo "<br />"; } ?> also note that if I put a INT in place of $values - the echo changes to match...why?? PLEASE help...I have been working on this for 2 entire days.... CREATE TABLE `members` ( `id` int(4) NOT NULL auto_increment, `username` varchar(65) NOT NULL default '', `password` varchar(65) NOT NULL default '', PRIMARY KEY (`id`) ) TYPE=MyISAM AUTO_INCREMENT=2 ; Hi, i would like to add values automattically from a form that my users fill out that corresponds to their login details. The form templete is above. Thanks! Hi all, I'm pretty much a noob, and I've tried looking in the manual, but I don't know enough to even look in the right direction. Implode function perhaps? I'm pulling values from a MySQL database... for example: $food $drink $price If the array has a value in it , I want it to report the value and then add a separator (like a comma) between the values. If the array is empty, I don't want the orphaned comma. For example: $food = "" $drink = "beer" $price = "five dollars" Right I can print: beer five dollars even: , beer, five dollars But what I want is: beer, five dollars Thanks. ~Wayne This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=313325.0 Hi, I'm inserting hours and minutes per user into a database where they have their own fields. (userid, hours, mins) I've a small issue when displaying the data. When I run my query I sum the total hours and minutes per user which results in data such as the following userid1 - 2 hours 15 mins userid2 - 1 hour 100 mins The query orders by hours and then mins desc When I'm displaying the data (as I'm looping through the results array) I perform a calculation to convert the mins to hours so it now reads userid1 - 2 hours 15 mins userid2 - 2 hours 40 mins so the webpage displays userid1 first when i want userid2 to be the first record displayed (Hours desc) Can anyone recommend a solution to this ? Will I need to create another table and update it as hours and minutes are being entered and display results from that table instead ? Can I order the data after I carry out the mins to hours calculation ? many thanks in advance for any suggestions.... tmfl Hello, I have a family website with an address book. I have it set so that you can select people based on who's family they are part of. So everyone in the family is part of my grandmother so when you select her group then everyone should display. Then I have it so that when you select my aunt or uncle then it will display only their families and so forth. so everyone except my grandmother has more than one family_id. I have a master_id specific to each master_name, then each master_id can have up to 3 family_id's. my issue is when I select all (my grandmother's group) i need it to group by my aunt's and uncle's families. and right now it is just in order of how I entered it into the database. My query selects all family_id's that = my $_POST[fam_id] from a dropdown list. So when I select my grandmother's group it will only return family_id "1". but i need to group by family_id 2-15, whichever is assigned to my aunt or uncle under my grandmother. since the query does not ask for it obviously i cannot sort by it. If I ask for everyone, grouped by family_id then I cannot select only by aunt or uncle as well. i'm at a total loss. please help. Here is my basic query right now: $sel_id= "SELECT master_id FROM master_family where family_id = '$_POST[fam_id]' ORDER BY master_id"; Please know this is my first website so I am very new to this and hope my question is not too lame. |