What I have is a script that calculates sales figures for facilities in an online game. The last part of this script that I need to work out is how to copy the financial data into a second table, which will be used as a ledger to display sales revenue for each facility. Everything works fine except that the insert query won't recognize the $revenue variable. It inserts a new row in the table for each facility like it's supposed to, with all the correct info, except the amount shows up as $0.00 instead of $revenue. If I change this line in the query:
$amount = "$revenue";
to something generic like:
$amount = "11";
It will work correctly, and insert $11.00 as the revenue for each facility. So the problem is somewhere with the $revenue variable, and I can't seem to work it out. The $revenue variable works fine in other areas of the script, such as updating the company's money.
Here's the entire script:
<?php $currentTime = time(); // current time (seconds since UNIX epoch) $update = "facility_sales"; require_once($_SERVER['DOCUMENT_ROOT'] . '/includes/dbconn.php'); /* Grab the company facility info from the company_facilities table */ $stmt = $dbh->query("SELECT * FROM company_facilities"); while($data = $stmt->fetch()) { /* Get average wages for all companies */ $sth = $dbh->prepare("SELECT AVG(employee_wage) FROM company_facilities"); $sth->execute(); $avg_wage = $sth->fetchColumn(); /* Get wages from user's company */ $sth = $dbh->prepare("SELECT employee_wage FROM company_facilities WHERE id = ?"); $sth->execute(array($data['id'])); $wage = $sth->fetchColumn(); /* Get employees from user's company */ $sth = $dbh->prepare("SELECT employees FROM company_facilities WHERE id = ?"); $sth->execute(array($data['id'])); $employees = $sth->fetchColumn(); /* Get managers from user's company */ $sth = $dbh->prepare("SELECT managers FROM company_facilities WHERE id = ?"); $sth->execute(array($data['id'])); $managers = $sth->fetchColumn(); /* Get equipment from user's company */ $sth = $dbh->prepare("SELECT equipment FROM company_facilities WHERE id = ?"); $sth->execute(array($data['id'])); $equipment = $sth->fetchColumn(); /* Get price from user's company */ $sth = $dbh->prepare("SELECT unit_price FROM company_facilities WHERE id = ?"); $sth->execute(array($data['id'])); $price = $sth->fetchColumn(); /* Get units_in_stock from user's company */ $sth = $dbh->prepare("SELECT units_in_stock FROM company_facilities WHERE id = ?"); $sth->execute(array($data['id'])); $units_in_stock = $sth->fetchColumn(); /* Get average unit_price for all companies */ $sth = $dbh->prepare("SELECT AVG(unit_price) FROM company_facilities WHERE unit_price > 0 AND specialty = '".$data['specialty']."'"); $sth->execute(); $avg_price = $sth->fetchColumn(); /* Calculate employee to equipment ratio */ $ratio = ($equipment/$employees); if($ratio > 1){ $ratio = 1; } else{ $ratio = $ratio; } /* Calculate the difference between wage and average wage */ $difference = ($wage-$avg_wage); /* Calculate Wage Modifier */ $wage_modifier = (($difference/$avg_wage)*1.8); if($wage_modifier > 0){ $mod = $difference/$avg_wage; $wage_modifier = (pow(sqrt($mod),1.2))*1.4; } // Wage modifier only affects production, not sales, for the following facilities. if($data['field'] == "Mining" || $data['field'] == "Farming" || $data['field'] == "Production"){ $wage_modifier = 0; } /* Get the units per hour number */ if($data['field'] == "Mining"){ $UPH = 0.672; } if($data['field'] == "Farming"){ $UPH = 0.672; } if($data['specialty'] == "Aluminum" || $data['specialty'] == "Steel" || $data['specialty'] == "Glass" || $data['specialty'] == "Rubber" || $data['specialty'] == "Fabric" || $data['specialty'] == "Plastic"){ $UPH = 0.336; } if($data['specialty'] == "Leather" || $data['specialty'] == "Chassis" || $data['specialty'] == "Body" || $data['specialty'] == "Window" || $data['specialty'] == "Tires" || $data['specialty'] == "Interior" || $data['specialty'] == "Engine"){ $UPH = 0.168; } if($data['specialty'] == "Vehicle"){ $UPH = 0.084; } if($data['field'] == "Retail"){ $UPH = 0.042; } /* Calculate preliminary sales number */ $pre_sales = ($UPH*$employees); /* Calculate the equipment modifier */ $equipment_modifier = ($ratio/4); // Equipment modifier only affects production, not sales, for the following facilities. if($data['field'] == "Mining" || $data['field'] == "Farming" || $data['field'] == "Production"){ $equipment_modifier = 0; } /* Calculate the Manager modifier */ $manager_modifier = sqrt(($pre_sales*(0.1*$managers))); $manager_mod_max = $employees/10; if($manager_modifier > $manager_mod_max){ $manager_modifier = $manager_mod_max; } // Manager modifier only affects production, not sales, for the following facilities. if($data['field'] == "Mining" || $data['field'] == "Farming" || $data['field'] == "Production"){ $manager_modifier = 0; } /* Calculate the price modifier */ $price_modifier = ((($price-$avg_price)*0.25)*-1); /* Finally, calculate the total sales */ $sales = floor(($pre_sales+$equipment_modifier+$wage_modifier+$price_modifier+$manager_modifier)); if($units_in_stock < $sales){ $sales = floor($units_in_stock); } $revenue = $sales*$price; /* Update facility units */ $sth = $dbh->prepare("UPDATE company_facilities SET units_in_stock = units_in_stock - ?, sold_last_hour = ? WHERE id = ?"); $sth->execute(array($sales,$sales,$data['id'])); /* Update company money */ $sth = $dbh->prepare("UPDATE companies SET money = money + ? WHERE id = ?"); $sth->execute(array($revenue,$data['company_id'])); } $sth = $dbh->prepare("SELECT * FROM company_facilities"); $sth->execute(); $datas = $sth->fetchALL(); foreach($datas as $data2) { /* Insert into company finance ledger */ $stmt = $dbh->prepare("INSERT INTO company_finance (date, company_id, company_name, type, amount, description) VALUES (?, ?, ?, ?, ?, ?)"); $stmt->bindParam(1, $date); $stmt->bindParam(2, $company_id); $stmt->bindParam(3, $company_name); $stmt->bindParam(4, $type); $stmt->bindParam(5, $amount); $stmt->bindParam(6, $description); // insert one row $date = "$currentTime"; $company_id = "".$data2['company_id'].""; $company_name = "".$data2['company_name'].""; $type = "revenue"; $amount = "$revenue"; $description = "Product Sales (".$data2['facility_name'].")"; $stmt->execute(); } ?>