簡體   English   中英

如何使這個PHP腳本動態?

[英]How to make this php script dynamic?

我目前有一個用PHP編寫的腳本。 這是我要嘗試做的-我有幾個不同的位置,具有不同的時間和日期,例如RSVP。

每個“位置”只有太多的位置(或此處稱為插槽),因此#1最多具有20個插槽。 我在此處包括的腳本的第一部分獲取該位置表中所有預留的總和。 另一部分是表單發布后,它會比較當前已保留的插槽數量,最大插槽以及它們嘗試保留的數量。

頁面上有一個下拉菜單,他們可以在其中選擇要保留的插槽數。 因此,假設已經保留了15個插槽,它們會嘗試保留6個插槽,但由於最大為20個插槽,因此不應傳遞並發送到另一頁。 一切正常,但是由於其余部分都是動態的,所以我不想繼續添加,並且每次添加新位置時都保持清潔,如果您明白我的意思了嗎? 我當時在考慮某種形式的foreach,但不確定如何使它工作。 有一個稱為locations的表,具有最大的插槽和所有信息。 每個人都有一個ID,該ID與預訂的表有關。 如果有人可以提供幫助,或者有任何想法,我將在接下來的幾個小時內完成,我將不勝感激! 謝謝

$result1 = $db->query("SELECT SUM(SLOTS) AS value_sum FROM `1`");
$row1 = $result1->fetch(PDO::FETCH_ASSOC);
$slots1 = $row1['value_sum'];
if(!isset($row1['value_sum'])) {
    $slots1 = '0';
}

$result2 = $db->query("SELECT SUM(SLOTS) AS value_sum FROM `2`");
$row2 = $result2->fetch(PDO::FETCH_ASSOC);
$slots2 = $row2['value_sum'];
if(!isset($row2['value_sum'])) {
    $slots2 = '0';
}

$result3 = $db->query("SELECT SUM(SLOTS) AS value_sum FROM `3`");
$row3 = $result3->fetch(PDO::FETCH_ASSOC);
$slots3 = $row3['value_sum'];
if(!isset($row3['value_sum'])) {
    $slots3 = '0';
}

$result4 = $db->query("SELECT SUM(SLOTS) AS value_sum FROM `4`");
$row4 = $result4->fetch(PDO::FETCH_ASSOC);
$slots4 = $row4['value_sum'];
if(!isset($row4['value_sum'])) {
    $slots4 = '0';
}
if (isset($_POST['submit'])) {
    $id=$_POST['id'];
    $passed = false;
    if ($id == '1'){
        $query = "SELECT maxslots FROM locations WHERE id = 1";
        $stmt = $db->prepare($query); 
        $stmt->execute();
        $slotamount = $stmt->fetchColumn();
        $slots_all = $slots1 + $_POST['slots'];
        if ($slots_all > $slotamount) {
            header("Location:index.php?toomany=1");
        } else {
            $passed = true;
        }
    }

    if ($id == '2'){
        $query = "SELECT maxslots FROM locations WHERE id = 2";
        $stmt = $db->prepare($query); 
        $stmt->execute();
        $slotamount = $stmt->fetchColumn();
        $slots_all = $slots2 + $_POST['slots'];
        if ($slots_all > $slotamount) {
            header("Location:index.php?toomany=2");
        } else {
            $passed = true;
        }
    }

    if ($id == '3'){
        $query = "SELECT maxslots FROM locations WHERE id = 3";
        $stmt = $db->prepare($query); 
        $stmt->execute();
        $slotamount = $stmt->fetchColumn();
        $slots_all = $slots3 + $_POST['slots'];
        if ($slots_all > $slotamount) {
            header("Location:index.php?toomany=3");
        } else {
            $passed = true;
        }
    }

    if ($id == '4'){
        $query = "SELECT maxslots FROM locations WHERE id = 4";
        $stmt = $db->prepare($query); 
        $stmt->execute();
        $slotamount = $stmt->fetchColumn();
        $slots_all = $slots4 + $_POST['slots'];
        if ($slots_all > $slotamount) {
            header("Location:index.php?toomany=4");
        } else {
            $passed = true;
        }
    }
}
<?php

function getSlotSum($id) {
    $result = $db->query("SELECT SUM(SLOTS) AS value_sum FROM $id");
    $row = $result->fetch(PDO::FETCH_ASSOC);
    $slots = $row['value_sum'];
    if(!isset($row['value_sum'])) {
       $slots = '0';
    }    
    return $slots;
}

if (isset($_POST['submit'])) {
    $id=$_POST['id'];
    $passed = false;
    $query = "SELECT maxslots FROM locations WHERE id = $id";
    $stmt = $db->prepare($query); 
    $stmt->execute();
    $slotamount = $stmt->fetchColumn();
    $slots_all = getSlotSum($id) + $_POST['slots'];
    if ($slots_all > $slotamount) {
        header("Location:index.php?toomany=$id");
    } else {
        $passed = true;
    }
}

?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM