PHP - Adding Db Values
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. Similar Tutorials
Table Issue - Multiple Location Values For User Pushes Values Out Of Row Instead Of Wrapping In Cell
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 ... 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']; } 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]; } 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>"; 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? 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 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? 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! 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(); } }
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> 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! 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.... 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. 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 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 Dear All Members here is my table data.. (4 Columns/1row in mysql table)
id order_no order_date miles How to split(miles) single column into (state, miles) two columns and output like following 5 columns /4rows in mysql using php code.
(5 Columns in mysql table) id order_no order_date state miles 310 001 02-15-2020 MI 108.53 310 001 02-15-2020 Oh 194.57 310 001 02-15-2020 PA 182.22
310 001 02-15-2020 WA 238.57 ------------------my php code -----------
<?php
if(isset($_POST["add"]))
$miles = explode("\r\n", $_POST["miles"]);
$query = $dbh->prepare($sql);
$lastInsertId = $dbh->lastInsertId(); if($query->execute()) {
$sql = "update tis_invoice set flag='1' where order_no=:order_no"; $query->execute();
} ----------------- my form code ------------------
<?php -- Can any one help how to correct my code..present nothing inserted on table
Thank You Edited February 8, 2020 by karthicbabuHi, My company has 240+ locations and as such some users (general managers) cover multiple sites. When I run a query to pull user information, when the user has multiple sites to his or her name, its adds the second / third sites to the next columns, rather than wrapping it inside the same table cell. It also works the opposite way, if a piece of data is missing in the database and is blank, its pull the following columns in. Both cases mess up the table and formatting. I'm extremely new to any kind of programming and maybe this isn't the forum for this question but figured I'd give it a chance since I'm stuck. The HTML/PHP code is below: <table id="datatables-column-search-select-inputs" class="table table-striped" style="width:100%"> <thead> <tr> <th>ID</th> <th>FirstName</th> <th>LastName</th> <th>Username</th> <th>Phone #</th> <th>Location</th> <th>Title</th> <th>Role</th> <th>Actions</th> </tr> </thead> <tbody> <?php //QUERY TO SELECT ALL USERS FROM DATABASE $query = "SELECT * FROM users"; $select_users = mysqli_query($connection,$query);
// SET VARIABLE TO ARRAY FROM QUERY while($row = mysqli_fetch_assoc($select_users)) { $user_id = $row['user_id']; $user_firstname = $row['user_firstname']; $user_lastname = $row['user_lastname']; $username = $row['username']; $user_phone = $row['user_phone']; $user_image = $row['user_image']; $user_title_id = $row['user_title_id']; $user_role_id = $row['user_role_id'];
// POPULATES DATA INTO THE TABLE echo "<tr>"; echo "<td>{$user_id}</td>"; echo "<td>{$user_firstname}</td>"; echo "<td>{$user_lastname}</td>"; echo "<td>{$username}</td>"; echo "<td>{$user_phone}</td>";
//PULL SITE STATUS BASED ON SITE STATUS ID $query = "SELECT * FROM sites WHERE site_manager_id = {$user_id} "; $select_site = mysqli_query($connection, $query); while($row = mysqli_fetch_assoc($select_site)) { $site_name = $row['site_name']; echo "<td>{$site_name}</td>"; } echo "<td>{$user_title_id}</td>"; echo "<td>{$user_role_id}</td>"; echo "<td class='table-action'> <a href='#'><i class='align-middle' data-feather='edit-2'></i></a> <a href='#'><i class='align-middle' data-feather='trash'></i></a> </td>"; //echo "<td><a href='users.php?source=edit_user&p_id={$user_id}'>Edit</a></td>"; echo "</tr>"; } ?>
<tr> <td>ID</td> <td>FirstName</td> <td>LastName</td> <td>Username</td> <td>Phone #</td> <td>Location</td> <td>Title</td> <td>Role</td> <td class="table-action"> <a href="#"><i class="align-middle" data-feather="edit-2"></i></a> <a href="#"><i class="align-middle" data-feather="trash"></i></a> </td> </tr> </tbody> <tfoot> <tr> <th>ID</th> <th>FirstName</th> <th>LastName</th> <th>Username</th> <th>Phone #</th> <th>Location</th> <th>Title</th> <th>Role</th> </tr> </tfoot> </table>
|