PHP规范化,如何创建适当的循环?

发布于 2024-12-19 22:55:51 字数 4291 浏览 4 评论 0原文

我尝试将我的餐桌预订分成 3 张桌子以实现标准化。 好吧,当我尝试为多个预订生成 1 个 bookingID 时遇到问题。 我对循环感到困惑。

场景:客户预订了2个展位,A01和A02,(A01从第1天到第3天预订,A02从第1天到第2天预订)。 在此处输入图片描述

系统将处理预订,它应该为 A01 和 A02 生成 2 个 bookingID,并为 A01_D1 生成 bookingScheduleID, A01_D2,A01_D3(携带bookingID A01)和A02_D1,A02_D2(携带bookingID A02)。什么我应该做循环并得到以下结果吗?

在此处输入图像描述

我的编码

<?php

include('db.php');

// check array and insert parrentBookingID

// check submitted totalDay
if (isset($_POST['submit']) && isset($_POST['totalDay'])) {

    //check day exist or not
    $cDay=$_POST['totalDay'];

    for ($i=1; $i<=sizeof($cDay); $i++) { //check array size    
        $temp=0;

        foreach ($cDay as $dy) {
            $checkDay = $dy;
            $checkDay = explode(" ", $checkDay);
            echo $checkDay[0]; // boothAlias
            echo $checkDay[1]; // boothID
            echo $checkDay[2]; // day

            $checkDayResult = mysql_query(
                "SELECT * FROM bookingDetail WHERE boothID='$checkDay[1]' 
                 and day='$checkDay[2]' and username='$user'"
            );

            $num_rows_check = mysql_num_rows($checkDayResult);
            if ($num_rows_check) {
                echo "Exist";
                $temp += 1;
            } else {
                $temp += 0;
            }
        }
    } //FOR LOOP CHECK ARRAY SIZE

    echo "temp".$temp;
    if ($temp != 0) {
        echo "Please try again";
    } else {
        // insert parentBookingID
        $parent = mysql_query(
            "INSERT into booking (custNo,eventID,dateBook) 
            VALUES ('$userID','$event',NOW())"
        );
        $parentBookID = mysql_insert_id();

        // End check array and insert parrentBookingID
        // check booth, create bookingDetailID

        $booth = $_POST['totalDay'];

        //for loop check array size for check repeat booth.
        for ($j=1; $j <= sizeof($booth); $j++) { 
            $current = explode (" ", $booth);
        } // end for loop check repeat loop.

        // end check booth.

        $totalDay = $_POST['totalDay'];
        $allBooth = "";

        foreach ($totalDay as $d) { 
            echo $d;

            $bookingInfo  = $d;
            $bookingInfo = explode(" ", $bookingInfo);
            echo $bookingInfo[0]; // boothAlias
            echo $bookingInfo[1]; // boothID
            echo $bookingInfo[2]; // day    

            $result = mysql_query(
                "SELECT * FROM bookingDetail WHERE
                boothID='$bookingInfo[1]' and day='$bookingInfo[2]' 
                and username='$user'"
            );

            $num_rows = mysql_num_rows($result);

            if ($num_rows) {
                echo "Exist";
            } else {    
                $str = "INSERT INTO bookingDetail 
                    (username, custNo, eventID, date, day,
                    boothAlias, boothID, parentBookID) VALUES ('$user',
                    '$userID','$event',NOW(),'$bookingInfo[2]',
                    '$bookingInfo[0]','$bookingInfo[1]','$parentBookID');";

                $res = mysql_query($str);
                if ($res) {
                    echo 'Success';
                } else {
                    echo 'Failure';
                }

                $allBooth = substr($allBooth, 0, -2); 
                echo "<p>Booth(s): <strong>$allBooth</strong>&nbsp;
                    <strong>$user</strong>&nbsp;<strong>$event</strong>
                    <strong>$userID</strong></p>\r\n";
            }
        }
    } // close check $temp

    header("refresh:5;url=mybooking.php");
    echo "<img src='loading16.gif' style='margin-top:8px; float:left'/>";
    echo 'You\'ll be redirected in about 5 secs. If not, click 
        <a href="mybooking.php">here</a>.';     
} else {    
    echo "You do not make any booking";
    header("refresh:5;url=booking2.php");
    echo "<img src='loading16.gif' style='margin-top:8px; float:left'/>";
    echo 'You\'ll be redirected in about 5 secs. If not, click 
        <a href="booking2.php">here</a>.';  
}

I try to break my table booking into 3 tables to enable normalization.
Well, I encounter problem when I try to generate 1 bookingID for multiple booking.
I am confuse with looping.

Scenario: customer reserved 2 booths, A01 and A02, (A01 booked from day 1 to day 3, while A02 booked from day 1 to day 2).
enter image description here

System will process the booking, it should generate 2 bookingIDs for A01 and A02, and generate bookingScheduleID for A01_D1, A01_D2, A01_D3 (carry bookingID A01) and A02_D1, A02_D2 (Carry bookingID A02).What should I do to loop and get the following result?

enter image description here

My coding

<?php

include('db.php');

// check array and insert parrentBookingID

// check submitted totalDay
if (isset($_POST['submit']) && isset($_POST['totalDay'])) {

    //check day exist or not
    $cDay=$_POST['totalDay'];

    for ($i=1; $i<=sizeof($cDay); $i++) { //check array size    
        $temp=0;

        foreach ($cDay as $dy) {
            $checkDay = $dy;
            $checkDay = explode(" ", $checkDay);
            echo $checkDay[0]; // boothAlias
            echo $checkDay[1]; // boothID
            echo $checkDay[2]; // day

            $checkDayResult = mysql_query(
                "SELECT * FROM bookingDetail WHERE boothID='$checkDay[1]' 
                 and day='$checkDay[2]' and username='$user'"
            );

            $num_rows_check = mysql_num_rows($checkDayResult);
            if ($num_rows_check) {
                echo "Exist";
                $temp += 1;
            } else {
                $temp += 0;
            }
        }
    } //FOR LOOP CHECK ARRAY SIZE

    echo "temp".$temp;
    if ($temp != 0) {
        echo "Please try again";
    } else {
        // insert parentBookingID
        $parent = mysql_query(
            "INSERT into booking (custNo,eventID,dateBook) 
            VALUES ('$userID','$event',NOW())"
        );
        $parentBookID = mysql_insert_id();

        // End check array and insert parrentBookingID
        // check booth, create bookingDetailID

        $booth = $_POST['totalDay'];

        //for loop check array size for check repeat booth.
        for ($j=1; $j <= sizeof($booth); $j++) { 
            $current = explode (" ", $booth);
        } // end for loop check repeat loop.

        // end check booth.

        $totalDay = $_POST['totalDay'];
        $allBooth = "";

        foreach ($totalDay as $d) { 
            echo $d;

            $bookingInfo  = $d;
            $bookingInfo = explode(" ", $bookingInfo);
            echo $bookingInfo[0]; // boothAlias
            echo $bookingInfo[1]; // boothID
            echo $bookingInfo[2]; // day    

            $result = mysql_query(
                "SELECT * FROM bookingDetail WHERE
                boothID='$bookingInfo[1]' and day='$bookingInfo[2]' 
                and username='$user'"
            );

            $num_rows = mysql_num_rows($result);

            if ($num_rows) {
                echo "Exist";
            } else {    
                $str = "INSERT INTO bookingDetail 
                    (username, custNo, eventID, date, day,
                    boothAlias, boothID, parentBookID) VALUES ('$user',
                    '$userID','$event',NOW(),'$bookingInfo[2]',
                    '$bookingInfo[0]','$bookingInfo[1]','$parentBookID');";

                $res = mysql_query($str);
                if ($res) {
                    echo 'Success';
                } else {
                    echo 'Failure';
                }

                $allBooth = substr($allBooth, 0, -2); 
                echo "<p>Booth(s): <strong>$allBooth</strong> 
                    <strong>$user</strong> <strong>$event</strong>
                    <strong>$userID</strong></p>\r\n";
            }
        }
    } // close check $temp

    header("refresh:5;url=mybooking.php");
    echo "<img src='loading16.gif' style='margin-top:8px; float:left'/>";
    echo 'You\'ll be redirected in about 5 secs. If not, click 
        <a href="mybooking.php">here</a>.';     
} else {    
    echo "You do not make any booking";
    header("refresh:5;url=booking2.php");
    echo "<img src='loading16.gif' style='margin-top:8px; float:left'/>";
    echo 'You\'ll be redirected in about 5 secs. If not, click 
        <a href="booking2.php">here</a>.';  
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文