我写了一个PHP脚本,生成一个随机数的汽车订单,直到它达到最大汽车订单限制。该代码在第一次运行时运行良好,但subsiqent运行它生成更多的汽车比最大允许。一般约两倍,我需要什么,我一直在试图弄清楚这一点,但它不断出现相同的结果。下面是代码
foreach ($industries as $industry) {
$indyId = $industry['id'];
$cars = getIndyCars($conn, $indyId);
//$lastOrderNumber = getLastOrderNumber($conn, $indyId);
$orderNumber = generateOrderNumber($conn, $indyId);
$industryOrders = [];
foreach ($cars as $car) {
$id = $car['id'];
$maxCars = $car['maxCars'];
$loaded = $car['loaded'];
$x = 1;
$typeId = $car['carType'];
$carsAlreadyOrdered = getCarsOrderd($conn, $id);
/*
* CARS ARE ORDERED ON A RANDOM SCALE BETWWEN THE MAX CARS AND CARS ALREADY ORDERED THEY WILL KEEP ORDERING BETWEEN THE AMOUNT OF CARS LEFT AND THE MAX CARS
* EVENTUALLY ORDERES WILL BE PLACED ON A SCHEDULE BUT THAT WILL COME LATER.
*/
if ($carsAlreadyOrdered < $maxCars) {
$remainingCars = $maxCars - $carsAlreadyOrdered;
$remainingCars -= $carsOrdered;
$carsOrdered = rand(0, $remainingCars);
if ($carsOrdered + $remainingCars > $maxCars) {
$carsOrdered = 0;
}
$carOrders = [];
echo '<b>' . $carsOrdered . ' Cars ordered for '.$id.'</b> '.$remainingCars.' left<br>';
if ($carsOrdered !== 0) {
$carsOrdered = $carsAlreadyOrdered + $carsOrdered;
updateCarsOrdered($conn, $id, $carsOrdered);
}else{
updateCarsOrdered($conn, $id, $carsOrdered);
}
$filterCars = getLocalFilterCars($conn, $typeId, $indyId);
if (is_array($filterCars) && count($filterCars) > 0) {
echo "<td>";
for ($x = 0; $x < $carsOrdered; $x++) {
$randomCarIndex = mt_rand(0, count($filterCars) - 1);
$randomCar = $filterCars[$randomCarIndex];
//echo $randomCar['filteredCars'];
$carId=$randomCar['filteredCars'];
$carWeight = 0;
if ($loaded == 1) {
$payload = getCarPayload($cdbconn, $randomCar['filteredCars']);
$carLoad = rand(1, $payload);
$carWeight = $carLoad;
$totalLoadWgt = (int)$totalLoadWgt + (int)$carLoad;
} elseif ($loaded == 0) {
$tareWeight = getCarEmptyWeight($cdbconn, $randomCar['filteredCars']);
$carWeight = $tareWeight;
$totalEmptyWeight = (int)$totalEmptyWeight + (int)$tareWeight;
}
addOrder($conn, $orderNumber, $indyId, $id, $carId, $loaded, $carWeight);
/* $carOrders[] = [
'car' => $randomCar['filteredCars'],
'loaded' => $loaded,
'payload' => $carWeight
];*/
}
}
/* if (!empty($carOrders)) {
$industryOrders[] = [
'industryTypeId' => $id,
'orders' => $carOrders
];
}*/
}//IF MAX CARS ORDERED
else{
echo "No cars ordered for".$id."<br>";
}
}//CarForeach*/
if (!empty($industryOrders)) {
///$jsonIndustryOrders = json_encode($industryOrders);
//addOrder($conn, $indyId, $id, $carId, $loaded, $carWeight);
//addOrder($conn, $indyId, $jsonIndustryOrders);
}
}
字符串
函数-第一个函数从数据库中获取当前车辆计数,第二个函数在生成订单时更新车辆计数。
function getCarsOrderd($conn, $indyId){
$sql = "SELECT * FROM industry_cars WHERE id =?;";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
header("location: details.php?error=stmtfailed");
exit();
}
mysqli_stmt_bind_param($stmt, "s", $indyId);
mysqli_stmt_execute($stmt);
$resultData = mysqli_stmt_get_result($stmt);
if ($row = mysqli_fetch_assoc($resultData)){
return $row['carsOrdered'];
}else{
$result = false;
return $result;
}
}
function updateCarsOrdered($conn, $indyId, $carsOrdered){
$sql = "UPDATE industry_cars SET carsOrdered = ? WHERE id=?;";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
header("location: ../details.php?error=stmtfailed");//ssssss
exit();
}
mysqli_stmt_bind_param($stmt, "ss", $carsOrdered, $indyId);
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
}
function getIndustryOrders($conn, $indyId){
$sql = "SELECT * FROM industryorders WHERE industryId = ?;";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
header("location: details.php?error=stmtfailed");
exit();
}
mysqli_stmt_bind_param($stmt, "s", $indyId);
mysqli_stmt_execute($stmt);
$resultData = mysqli_stmt_get_result($stmt);
while ($result = mysqli_fetch_all($resultData, MYSQLI_ASSOC)) {
return $result;
}
}
型
2条答案
按热度按时间bqujaahr1#
由于您正在应用某种逻辑,生成随机数量的汽车,但您没有建立限制,因此可以尝试使用具有随机概率集的
for
循环,以确保您有一个限制,以及更现实的行为。让我们改变一下逻辑,你可以试试这个方法:
字符串
cbeh67ev2#
我能够找到一个解决我的问题的方法,我在代码中添加了一些注解。我相信这是一个问题,
$carsOrdered
变量被用来计算订购的汽车的数量,并将其添加到运行总数。我用$runningCarsTotal
代替了它,这似乎解决了这个问题。字符串