[英]How to search a CSV file with php to prevent double booking
I made a previous post that was too vague. 我以前的帖子太含糊了。 I've done a lot of research and think I can be more specific.
我做了很多研究,认为我可以更具体。
/Ready to Save? Store all data into variables
if (isset($_POST['submit'])) {
$ID = $_POST['gameID'];
$GameName = $_POST['gameName'];
$GameCost = $_POST['gameCost'];
$Days = $_POST['days'];
$Total = $_POST['total'];
$WantedDate = $_POST['reservationStart'];
$DateTo = $_POST['DateEnd'];
$StudentName = $_POST['studentName'];
while (!feof($file_handle))
{
$loansinfo = fgetcsv($file_handle);
// Make sure we only check data for the game we posted. $WantedDate is also a posted variable
if($loansinfo[0]==$ID) {
$referenceDate = $WantedDate;
$fromDate = "$loansinfo[5]";
$toDate = "$loansinfo[6]";
// Convert dates to timestamps (strings to integers)
$referenceTimestamp = strtotime( $referenceDate );
$fromTimestamp = strtotime( $fromDate );
$toTimestamp = strtotime( $toDate );
$isBetween = $referenceTimestamp >= $fromTimestamp and $referenceTimestamp <= $toTimestamp;
if($isBetween == true) {
//refuse booking
echo('<script type="text/javascript">alert("Game Already Booked");</script>');
exit;
}
}
}
otherwise execute save code
Problem is, I always get 'Game already booked'. 问题是,我总是收到“游戏已经预订”。 Why?
为什么?
This is your code cleaned up with anything not actually used in the code removed: 这是您的代码,其中包含未在删除的代码中实际使用的任何内容:
if(isset($_POST['submit']))
{
$referenceTimestamp = strtotime($_POST['reservationStart']);
while(!feof($file_handle))
{
$loansinfo = fgetcsv($file_handle);
if($loansinfo[0] == $_POST['gameID'] && $referenceTimestamp >= strtotime($loansinfo[5]) && $referenceTimestamp <= strtotime($loansinfo[6]))
{
echo('<script type="text/javascript">alert("Game Already Booked");</script>');
exit;
}
}
}
It checks two things: 1. Whether the gameID matches. 它检查两件事:1. gameID是否匹配。 2. Whether reservation start is greater than or equal to $loansinfo[5] and also lesser than or equal to $loansinfo[6].
2.保留开始是否大于或等于$ loansinfo [5]并且还小于或等于$ loansinfo [6]。
Hopefully clearing up the debris will help. 希望清理碎片会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.