PHP - Array_sum And Trailing Zero's
Hello everything,
I have the following code: while($row = mysql_fetch_array($result)) { $due[] = $row['amount']; $total[] = $row['amount']; } $due = array_sum($due); $total = array_sum($total); My current output is: $due = 23 $total = 100 What I would like is to have .00 at the end of each of those. If either of the variables above naturally have decimals, they appear fine, just when they are integers. Any help is appreciated. Similar Tutorials$sql_get_match = mysql_query("SELECT length FROM Match_1v1 WHERE show_id='$show_id'"); $get_match = array_sum(mysql_fetch_array($sql_get_match)); I have two fields in the database with length's of 31 and 29, but it comes out as a total of 58. What am I doing wrong? <?php error_reporting(0); $numm0 = array(1,2,3,4,5,6); $numm1 = array(1,3,4,5,6); $numm2 = array(1,2,3,4,6); $numm3 = array(1,2,3,5,6); for($i = 0;$i < 4;$i++) { print_r($numm.$i); } for($i = 0;$i < 4;$i++) { $w.$i = array_sum($numm.$i); print_r($w.$i); echo "<br>"; } ?> this code just hangs when executed. how can the syntax of the print_r() function and array_sum() function be modified? need help... I have built a dynamic list of Check Boxes with the query below.
while($row = mysqli_fetch_assoc($result)) { extract($row); $nospaces = str_replace(' ', '_', $life_event); echo "<tr> <td style='width:30px'>$life_event_id</td> <td style='width:300px'>$life_event</td> <td><input type='checkbox' name='$nospaces' value='$Value' /></td> </tr>"; }Each of the checkboxes has a numerical value which i want to add up and echo back to the user when he submits the form (please see code below). I have gotten this far in attempting to extract values when the submit (calculate) button is clicked into an array. Thereafter, i applied array_sum() to return the sum of values in the $stress_score array. However the array_sum($Stress_score) returns zero (0). $Death_of_spouse = 'unchecked'; $Divorce = 'unchecked'; if (isset($_POST['calculate'])) { $Stress_score=array(); if (isset($_POST['Death_of_spouse']) ) { if ($Death_of_spouse == 'checked') { $Stress_score[] = '100'; } } if (isset($_POST['Divorce']) ) { if ($Divorce == 'checked') { $Stress_score[]= '20'; } } echo "Your Stress Score = " . array_sum($Stress_score) . "\n"; }I shall appreciate any thoughts on how to resolve this. Thanks. Hello, everyone: I'm using PHP and Mysql. I've got a table of that has 30 fields that I need to add together. I've got to do this for each record, and there are about 10,000 records. Which would be more efficient? SELECT SUM( field1 + field2...) or to make an array for the fields and then use array_sum() Thanks. I got these 2 rewrites:
RewriteRule ^user/([0-9]+)/(.*)/(.*)/ ./board/user.php?ID=$1&FIRSTNAME=$2&LASTNAME=$3 [L,NC] RewriteRule ^user/([0-9]+)/(.*)/(.*) ./board/user.php?ID=$1&FIRSTNAME=$2&LASTNAME=$3 [L,NC]And i want to combine them but when i do this: RewriteRule ^user/([0-9]+)/(.*)/(.*)(/|$) ./board/user.php?ID=$1&FIRSTNAME=$2&LASTNAME=$3 [L,NC]It doesnt work.. when i type this in the url https://www.mysite/board/users.php?ID=1&FIRSTNAME=Richard&LASTNAME=Grant/ the variables are retrieved like this: (has trailing slash) ID="1" FIRSTNAME="Richard/Grant" LASTNAME="" when i type this in the url https://www.mysite/board/users.php?ID=1&FIRSTNAME=Richard&LASTNAME=Grant the variables are retrieved like this: (doesn't have trailing slash) ID="1" FIRSTNAME="Richard" LASTNAME="Grant" This topic has been moved to mod_rewrite. http://www.phpfreaks.com/forums/index.php?topic=359558.0 This topic has been moved to mod_rewrite. http://www.phpfreaks.com/forums/index.php?topic=325383.0 I am making a price sheet for extended warranties that my company sells and am having a problem with the math. There is a base price of $1380, plus certain surcharges and certain exclusions. The exclusions are working properly, but something funky is happening with the surcharges... This link more clearly explains the problem I am having: http://www.carcityofdanbury.com/New/01/Resources/printable_list_Admin.php Here is the code, then I'll explain the problem Code: [Select] // TMU if ($row['TMU'] == "x") { $extended = 'N/A'; // Salvage Title } elseif ($row['cleantitle'] != "x") { $extended = 'N/A'; // 150,000+ miles } elseif ($row['mileage'] > "150000") { $extended = 'N/A'; // Snowplow } elseif ($row['ftr003'] == "x") { $extended = 'N/A'; // Category 3 } elseif ($row['make'] == "Jaguar" || $row['make'] == "Land Rover") { $extended = 'N/A'; // 10+ cyls } elseif ($row['engine_cylinders'] > "8") { $extended = 'N/A'; // Older than 13 years } elseif ($year < date("Y")-12) { $extended = 'N/A'; // Qualifies for warranty } else { if ($row['mileage'] > "99999" && $row['mileage'] < "125000") {$surcharge[] = 200;} if ($row['mileage'] > "125000" && $row['mileage'] < "150001") {$surcharge[] = 400;} if ($row['drivetrain'] == "AWD" || $row['drivetrain'] == "4x4") {$surcharge[] = 150;} if ($row['fuel'] == "Diesel") {$surcharge[] = 100;} if ($row['make'] == "Audi" || $row['make'] == "BMW" || $row['make'] == "Cadillac" || $row['make'] == "Infiniti" || $row['make'] == "Lexus" || $row['make'] == "Lincoln" || $row['make'] == "Mercedes-Benz" || $row['make'] == "Mini" || $row['make'] == "Saab" || $row['make'] == "Volvo") {$surcharge[] = 550;} if (date("Y")-11 == $year) {$surcharge[] = 50;} if (date("Y")-12 == $year) {$surcharge[] = 100;} if (date("Y")-13 == $year) {$surcharge[] = 200;} if ($surcharge) {$surcharges = array_sum($surcharge);} $extended = '$'.number_format(1380 + $surcharges); } The 9th vehicle on the list: 08 Hyundai Sante Fe SE the warranty price is correct, but the vehicle after, the 08 Honda Accord EX should be 1,380, yet its adding the surcharges from the the Hyundai, and it keeps repeating down the list. Where is the error? I'm setting a date using: $day = date("d")+1; However when I echo this result I get a single character rather than two i.e '7' and not '07', anyone know what could be up with this? I'm sure it's something to do with the '+1' because i create the current date with this code and returns with the format I want. Thanks! |