Я пытаюсь создать систему бронирования в php и mysql с помощью datepicker. Когда пользователь выбирает дату, должна отображаться таблица с доступными периодами времени. Если временной интервал доступен, пользователь должен использовать радио input
и забронировать этот период времени. Если я использую "ORDER BY start ASC LIMIT 1"
. Это дает мне 8:22 exists
, и все правильно, но это не дает мне второго заявления, 8:30 exists
. Вместо этого на 8.30 он создает доступный временной интервал 8:30-8:38
. Если я использую "ORDER BY start ASC"
. Он умножает информацию, основанную на том, сколько записей у меня есть в базе данных. Я не вижу, что не так! пыльник мой код тузд таблицы:Что-то не так с mysqli_fetch_array в php?
CREATE TABLE IF NOT EXISTS `bookings` (
`id` int(100) NOT NULL AUTO_INCREMENT,
`date` date NOT NULL,
`start` enum('8:22-8:30','8:30-8:38','8:38-8:46','8:46-8:54','8:54-9:02','9:02-9:10','9:18-9:26','9:26-9:34','9:34-9:42','9:42-9:50','9:50-10:58','10:58-11:06','11:06-11:14','11:14-11:22','11:22-11:30','11:30-11:38','11:38-11:46','11:46-11:54','11:54-12:02','12:30-12:38','12:38-12:46','12:46-12:54','12:54-13:02','13:02-13:10','13:10-13:18','13:18-13:26','13:26-13:34','13:34-13:42','13:42-13:50','13:50-13:58','13:58-14:06','14:06-14:14','14:14-14:22','14:22-14:30','14:30-14:38','14:38-14:46','14:46-14:54','14:54-15:02','15:02-15:10','15:10-15:18','15:18-15:26','15:26-15:34') COLLATE utf8_unicode_ci NOT NULL,
`booked` enum('0','1') COLLATE utf8_unicode_ci NOT NULL,
`name` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`email` varchar(200) COLLATE utf8_unicode_ci NOT NULL,
`phone` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `start` (`start`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=5 ;
--
-- Dumping data for table `bookings`
--
INSERT INTO `bookings` (`id`, `date`, `start`, `booked`, `name`, `email`, `phone`) VALUES
(2, '2016-05-06', '8:30-8:38', '1', 'vcv', 'vxcv', 'xcvvc'),
(1, '2016-05-06', '8:22-8:30', '1', 'zxccxz', 'xczcc', 'zxczxc'),
(4, '2016-05-09', '9:02-9:10', '1', '', '', ''),
(3, '2016-05-09', '8:38-8:46', '1', '', '', '');
И код index.php является:
<?php
$conn = mysqli_connect("localhost", "", "", "");
$post_at = "";
$post_at_to_date = "";
$queryCondition = "";
if(!empty($_POST["search"]["post_at"])) {
$post_at = $_POST["search"]["post_at"];
list($fid,$fim,$fiy) = explode("-",$post_at);
$post_at_todate = date('Y-m-d');
if(!empty($_POST["search"]["post_at"])) {
$post_at_to_date = $_POST["search"]["post_at"];
list($tid,$tim,$tiy) = explode("-",$_POST["search"]["post_at"]);
$post_at_todate = "$tiy-$tim-$tid";
}
$queryCondition .= "WHERE date = '" . $post_at_todate . "' ";
}
$sql = "SELECT * from bookings " . $queryCondition . " ORDER BY start ASC LIMIT 1";
$result = mysqli_query($conn,$sql);
?>
<html>
<head>
<title>Recent Articles</title>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<style>
.table-content{border-top:#CCCCCC 4px solid; width:50%;}
.table-content th {padding:5px 20px; background: #F0F0F0;vertical-align:top;}
.table-content td {padding:5px 20px; border-bottom: #F0F0F0 1px solid;vertical-align:top;}
</style>
</head>
<body><input type="text" id="field_results" name="field_results" /> ...
<div class="demo-content">
<h2 class="title_with_link">Recent Articles</h2>
<form name="frmSearch" method="post" action="">
<p class="search_input">
<input type="text" placeholder="From Date" id="post_at" name="search[post_at]" value="<?php echo $post_at; ?>" class="input-control" />
<input type="submit" name="go" value="Search" >
</p>
<?php if(!empty($result)) { ?>
<table class="table-content">
<thead>
<tr>
<th width="30%"><span>Day</span></th>
<th width="50%"><span>Time period</span></th>
<th width="20%"><span>Book</span></th>
</tr>
</thead>
<tbody>
<?php
//while ($row=mysqli_fetch_row($result))
while($row = mysqli_fetch_array($result)) {
if(preg_match('/8:22-8:30/',$row['start'])==TRUE){
echo "8:22 exists";
}else {
echo "<tr>";
echo "<td>$post_at</td>";
echo "<td>8:22-8:30</td>";
echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '8:22-8:30' .'"></td>';
echo "</tr>";
}
if(preg_match('/8:30-8:38/',$row['start'])==TRUE){
echo "8:30 exists";
}else {
echo "<tr>";
echo "<td>$post_at</td>";
echo "<td>8:30-8:38</td>";
echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '8:30-8:38' .'"></td>';
echo "</tr>";
}
if(preg_match('/8:38-8:46/',$row['start'])){
echo "8:38 exists";
} else {
echo "<tr>";
echo "<td>$post_at</td>";
echo "<td>8:38-8:46</td>";
echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '8:38-8:46' .'"></td>';
echo "</tr>";
}
if(preg_match('/8:46-8:54/',$row['start'])){
echo "";
} else {
echo "<tr>";
echo "<td>$post_at</td>";
echo "<td>8:46-8:54</td>";
echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '8:46-8:54' .'"></td>';
echo "</tr>";
}
if(preg_match('/8:54-9:02/',$row['start'])){
echo "";
} else {
echo "<tr>";
echo "<td>$post_at</td>";
echo "<td>8:54-9:02</td>";
echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '8:54-9:02' .'"></td>';
echo "</tr>";
}
if(preg_match('/9:02-9:10/',$row['start'])){
echo "";
} else {
echo "<tr>";
echo "<td>$post_at</td>";
echo "<td>9:02-9:10</td>";
echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '9:02-9:10' .'"></td>';
echo "</tr>";
}
if(preg_match('/9:18-9:26/',$row['start'])){
echo "";
} else {
echo "<tr>";
echo "<td>$post_at</td>";
echo "<td>9:18-9:26</td>";
echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '9:18-9:26' .'"></td>';
echo "</tr>";
}
if(preg_match('/9:26-9:34/',$row['start'])){
echo "";
} else {
echo "<tr>";
echo "<td>$post_at</td>";
echo "<td>9:26-9:34</td>";
echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '9:26-9:34' .'"></td>';
echo "</tr>";
}
if(preg_match('/9:34-9:42/',$row['start'])){
echo "";
} else {
echo "<tr>";
echo "<td>$post_at</td>";
echo "<td>9:34-9:42</td>";
echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '9:34-9:42' .'"></td>';
echo "</tr>";
}
if(preg_match('/9:42-9:50/',$row['start'])){
echo "";
} else {
echo "<tr>";
echo "<td>$post_at</td>";
echo "<td>9:42-9:50</td>";
echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '9:42-9:50' .'"></td>';
echo "</tr>";
}
if(preg_match('/9:50-10:58/',$row['start'])){
echo "";
} else {
echo "<tr>";
echo "<td>$post_at</td>";
echo "<td>9:50-10:58</td>";
echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '9:50-10:58' .'"></td>';
echo "</tr>";
}
if(preg_match('/10:58-11:06/',$row['start'])){
echo "";
} else {
echo "<tr>";
echo "<td>$post_at</td>";
echo "<td>10:58-11:06</td>";
echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '10:58-11:06' .'"></td>';
echo "</tr>";
}
if(preg_match('/11:06-11:14/',$row['start'])){
echo "";
} else {
echo "<tr>";
echo "<td>$post_at</td>";
echo "<td>11:06-11:14</td>";
echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '11:06-11:14' .'"></td>';
echo "</tr>";
}
if(preg_match('/11:14-11:22/',$row['start'])){
echo "";
} else {
echo "<tr>";
echo "<td>$post_at</td>";
echo "<td>11:14-11:22</td>";
echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '11:14-11:22' .'"></td>';
echo "</tr>";
}
if(preg_match('/11:22-11:30/',$row['start'])){
echo "";
} else {
echo "<tr>";
echo "<td>$post_at</td>";
echo "<td>11:22-11:30</td>";
echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '11:22-11:30' .'"></td>';
echo "</tr>";
}
if(preg_match('/11:30-11:38/',$row['start'])){
echo "";
} else {
echo "<tr>";
echo "<td>$post_at</td>";
echo "<td>11:30-11:38</td>";
echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '11:30-11:38' .'"></td>';
echo "</tr>";
}
if(preg_match('/11:38-11:46/',$row['start'])){
echo "";
} else {
echo "<tr>";
echo "<td>$post_at</td>";
echo "<td>11:38-11:46</td>";
echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '11:38-11:46' .'"></td>';
echo "</tr>";
}
if(preg_match('/11:46-11:54/',$row['start'])){
echo "";
} else {
echo "<tr>";
echo "<td>$post_at</td>";
echo "<td>11:46-11:54</td>";
echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '11:46-11:54' .'"></td>';
echo "</tr>";
}
if(preg_match('/11:54-12:02/',$row['start'])){
echo "";
} else {
echo "<tr>";
echo "<td>$post_at</td>";
echo "<td>11:54-12:02</td>";
echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '11:54-12:02' .'"></td>';
echo "</tr>";
}
if(preg_match('/12:30-12:38/',$row['start'])){
echo "";
} else {
echo "<tr>";
echo "<td>$post_at</td>";
echo "<td>12:30-12:38</td>";
echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '12:30-12:38' .'"></td>';
echo "</tr>";
}
if(preg_match('/12:38-12:46/',$row['start'])){
echo "";
} else {
echo "<tr>";
echo "<td>$post_at</td>";
echo "<td>12:38-12:46</td>";
echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '12:38-12:46' .'"></td>';
echo "</tr>";
}
if(preg_match('/12:46-12:54/',$row['start'])){
echo "";
} else {
echo "<tr>";
echo "<td>$post_at</td>";
echo "<td>12:46-12:54</td>";
echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '12:46-12:54' .'"></td>';
echo "</tr>";
}
if(preg_match('/12:54-13:02/',$row['start'])){
echo "";
} else {
echo "<tr>";
echo "<td>$post_at</td>";
echo "<td>12:54-13:02</td>";
echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '12:54-13:02' .'"></td>';
echo "</tr>";
}
if(preg_match('/13:02-13:10/',$row['start'])){
echo "";
} else {
echo "<tr>";
echo "<td>$post_at</td>";
echo "<td>13:02-13:10</td>";
echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '13:02-13:10' .'"></td>';
echo "</tr>";
}
if(preg_match('/13:10-13:18/',$row['start'])){
echo "";
} else {
echo "<tr>";
echo "<td>$post_at</td>";
echo "<td>13:10-13:18</td>";
echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '13:10-13:18' .'"></td>';
echo "</tr>";
}
if(preg_match('/13:18-13:26/',$row['start'])){
echo "";
} else {
echo "<tr>";
echo "<td>$post_at</td>";
echo "<td>13:18-13:26</td>";
echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '13:18-13:26' .'"></td>';
echo "</tr>";
}
if(preg_match('/13:26-13:34/',$row['start'])){
echo "";
} else {
echo "<tr>";
echo "<td>$post_at</td>";
echo "<td>13:26-13:34</td>";
echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '13:26-13:34' .'"></td>';
echo "</tr>";
}
if(preg_match('/13:34-13:42/',$row['start'])){
echo "";
} else {
echo "<tr>";
echo "<td>$post_at</td>";
echo "<td>13:34-13:42</td>";
echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '13:34-13:42' .'"></td>';
echo "</tr>";
}
if(preg_match('/13:42-13:50/',$row['start'])){
echo "";
} else {
echo "<tr>";
echo "<td>$post_at</td>";
echo "<td>13:42-13:50</td>";
echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '13:42-13:50' .'"></td>';
echo "</tr>";
}
if(preg_match('/13:50-13:58/',$row['start'])){
echo "";
} else {
echo "<tr>";
echo "<td>$post_at</td>";
echo "<td>13:50-13:58</td>";
echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '13:50-13:58' .'"></td>';
echo "</tr>";
}
if(preg_match('/13:58-14:06/',$row['start'])){
echo "";
} else {
echo "<tr>";
echo "<td>$post_at</td>";
echo "<td>13:58-14:06</td>";
echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '13:58-14:06' .'"></td>';
echo "</tr>";
}
if(preg_match('/14:06-14:14/',$row['start'])){
echo "";
} else {
echo "<tr>";
echo "<td>$post_at</td>";
echo "<td>14:06-14:14</td>";
echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '14:06-14:14' .'"></td>';
echo "</tr>";
}
if(preg_match('/14:14-14:22/',$row['start'])){
echo "";
} else {
echo "<tr>";
echo "<td>$post_at</td>";
echo "<td>14:14-14:22</td>";
echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '14:14-14:22' .'"></td>';
echo "</tr>";
}
if(preg_match('/14:22-14:30/',$row['start'])){
echo "";
} else {
echo "<tr>";
echo "<td>$post_at</td>";
echo "<td>14:22-14:30</td>";
echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '14:22-14:30' .'"></td>';
echo "</tr>";
}
if(preg_match('/14:30-14:38/',$row['start'])){
echo "";
} else {
echo "<tr>";
echo "<td>$post_at</td>";
echo "<td>14:30-14:38</td>";
echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '14:30-14:38' .'"></td>';
echo "</tr>";
}
if(preg_match('/14:38-14:46/',$row['start'])){
echo "";
} else {
echo "<tr>";
echo "<td>$post_at</td>";
echo "<td>14:38-14:46</td>";
echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '14:38-14:46' .'"></td>';
echo "</tr>";
}
if(preg_match('/14:46-14:54/',$row['start'])){
echo "";
} else {
echo "<tr>";
echo "<td>$post_at</td>";
echo "<td>14:46-14:54</td>";
echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '14:46-14:54' .'"></td>';
echo "</tr>";
}
if(preg_match('/14:54-15:02/',$row['start'])){
echo "";
} else {
echo "<tr>";
echo "<td>$post_at</td>";
echo "<td>14:54-15:02</td>";
echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '14:54-15:02' .'"></td>';
echo "</tr>";
}
if(preg_match('/15:02-15:10/',$row['start'])){
echo "";
} else {
echo "<tr>";
echo "<td>$post_at</td>";
echo "<td>15:02-15:10</td>";
echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '14:54-15:02' .'"></td>';
echo "</tr>";
}
if(preg_match('/15:10-15:18/',$row['start'])){
echo "";
} else {
echo "<tr>";
echo "<td>$post_at</td>";
echo "<td>15:10-15:18</td>";
echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '15:10-15:18' .'"></td>';
echo "</tr>";
}
if(preg_match('/15:18-15:26/',$row['start'])){
echo "";
} else {
echo "<tr>";
echo "<td>$post_at</td>";
echo "<td>15:18-15:26</td>";
echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '15:18-15:26' .'"></td>';
echo "</tr>";
}
if(preg_match('/15:26-15:34/',$row['start'])){
echo "";
} else {
echo "<tr>";
echo "<td>$post_at</td>";
echo "<td>15:26-15:34</td>";
echo '<td><input class="text" type="radio" id="rez" name="rez" value="'. '15:26-15:34' .'"></td>';
echo "</tr>";
}
?>
<tr>
</tr>
<?php
}
?>
<tbody>
</table>
<?php } ?>
</form>
</div>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script>
$.datepicker.setDefaults({
showOn: "button",
buttonImage: "datepicker.png",
buttonText: "Date Picker",
buttonImageOnly: true,
dateFormat: 'dd-mm-yy'
});
$(function() {
$("#post_at").datepicker();
$("#post_at_to_date").datepicker();
});
</script>
<script>
$(document).ready(function() {
$(":radio").on('click', function() {
var fields = '';
$(":radio").each(function() {
if (this.checked) {
fields += $(this).val() + ' ';
}
});
$('#field_results').val($.trim(fields))
});
});
</script>
</body></html>
Структура кода выглядит прямо на меня. Можете ли вы сделать C9 из него и поделиться, если это возможно? –
'LIMIT 1' означает, что в результате вы записали только __1__. Что вы ожидаете? –
Хороший код спагетти! У вас сумасшедшие увольнения. Этот код должен быть 20 строк. Используйте переключатель, а не если хотите, добавьте html и echo только один раз. Где аякс на выбор даты? Используйте ajax и добавьте div. – cpugourou