php 即使在达到最大订单数后,也会生成订单

kqlmhetl  于 2023-11-16  发布在  PHP
关注(0)|答案(2)|浏览(103)

我写了一个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;
    }
}

bqujaahr

bqujaahr1#

由于您正在应用某种逻辑,生成随机数量的汽车,但您没有建立限制,因此可以尝试使用具有随机概率集的for循环,以确保您有一个限制,以及更现实的行为。
让我们改变一下逻辑,你可以试试这个方法:

foreach ($cars as $car) {
    $id = $car['id'];
    $maxCars = $car['maxCars'];
    $loaded = $car['loaded'];
    $x = 1;
    $typeId = $car['carType'];
    $carsAlreadyOrdered = getCarsOrderd($conn, $id);

    if ($carsAlreadyOrdered < $maxCars) {
    $remainingCars = $maxCars - $carsAlreadyOrdered;
    $carsOrdered = rand(0, $remainingCars);
    if ($carsAlreadyOrdered < $maxCars) {
        $remainingCars = $maxCars - $carsAlreadyOrdered;
        $probability = rand(0, 100) / 100; // Set a random probability
        
        for ($i = 0; $i < $remainingCars; $i++) {
            if (rand(0, 100) / 100 < $probability) { // So, if the number is lees than the probability, +1 car
                $carsAlreadyOrdered++;
                updateCarsOrdered($conn, $id, $carsAlreadyOrdered);
            } else { // If more than the probability, stop.
                break;
            }
        }

        echo '<b>' . $carsAlreadyOrdered . ' Cars ordered for '.$id.'</b> '.$remainingCars.' left<br>';
        $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>";
    }
}

字符串

cbeh67ev

cbeh67ev2#

我能够找到一个解决我的问题的方法,我在代码中添加了一些注解。我相信这是一个问题,$carsOrdered变量被用来计算订购的汽车的数量,并将其添加到运行总数。我用$runningCarsTotal代替了它,这似乎解决了这个问题。

$region_id = 1;
$totalLoadWgt = "";
$totalEmptyWeight = "";

$industries = getIndustryList($conn, $region_id);

foreach ($industries as $industry) {
    $indyId = $industry['id'];
    $industry['industryName'];
    $cars = getIndyCars($conn, $indyId);
    $orderNumber = generateOrderNumber($conn, $indyId);
    echo "<tr><td colspan='4'>";
    echo $industry['industryName'];
    
    foreach ($cars as $car){

            $id = $car['id'];
            $maxCars = $car['maxCars'];
            $loaded = $car['loaded'];
            $x = 1;
            $typeId = $car['carType'];
         
            $carsAlreadyOrdered = getCarsOrderd($conn, $id); // Gets number of cars that have already been ordered
          
            $remainingCars = $maxCars - $carsAlreadyOrdered; // Calcualtes how many cars remain after previous order
        if ($carsAlreadyOrdered <= $maxCars) {
            $carsOrdered = rand(0, $remainingCars);
          
            $runningCarsTotal = $carsAlreadyOrdered+$carsOrdered; // Adds number of cars ordered to the running total
            updateCarsOrdered($conn, $id, $runningCarsTotal); // Updates database with new car count
        }
            $filterCars = getLocalFilterCars($conn, $typeId, $indyId);
        if (is_array($filterCars) && count($filterCars) > 0) {
                for ($x = 0; $x < $carsOrdered; $x++) {
                $randomCarIndex = mt_rand(0, count($filterCars) - 1);
                $randomCar = $filterCars[$randomCarIndex];
                $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); // Adds data to database with the above data added. 
                }
        }           
    }
}

字符串

相关问题