PHP规范化,如何创建适当的循环?
我尝试将我的餐桌预订分成 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>
<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>.';
}
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).
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?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论