I'm trying to get multiple bookings from my database for a hotel project when i tried to get the bookings from my database i only recieved one booking it should display/select multiple bookings.
This is my php code: (formulier.php)
<?php
$dt1 = $_POST['date1'];
$dt2 = $_POST['date2'];
$query = "SELECT * FROM dt_tb WHERE dt BETWEEN '$dt1' AND '$dt2'";
$result = mysqli_query($connect,$query);
if (mysqli_num_rows($result)) {
while($row = $result->fetch_assoc()) { include_once 'source.php';
}
echo "<br>formulier.php";
echo "<br>check-in: $dt1 <br>check-out: $dt2";
}
else {
die;
}
?>
And this is my other php code: (source.php)
<?php
$dt1 = $_POST['date1'];
$dt2 = $_POST['date2'];
$query = "SELECT * FROM dt_tb WHERE dt BETWEEN '$dt1' AND '$dt2'";
$result = mysqli_query($connect,$query);
if (mysqli_num_rows($result)) {
while($row = $result->fetch_assoc()) {
}
echo "source.php";
echo "<br>check-in: $dt1 <br>check-out: $dt2";
}
else {
die;
}
?>
this is my database: (dt_tb)
https://gyazo.com/966efdf144a8cd1a74fa04a8127cd8f4
This is what i receive: (Website)
https://gyazo.com/1384bdafb5055f5777a40e88baccdbdd
I know my code is messed up badly and tried to solve it for quite some time.
I don't know why you need two files. You can join them to single loop.
<?php
$dt1 = !empty($_POST['date1']) ? $_POST['date1'] : null;
$dt2 = !empty($_POST['date2']) ? $_POST['date2'] : null;
if (!$dt1 || !$dt2) {
throw new Exception("No dates passed!");
}
$query = "SELECT * FROM dt_tb WHERE dt BETWEEN '$dt1' AND '$dt2'";
$result = mysqli_query($connect, $query);
if (mysqli_num_rows($result)) {
while($row = $result->fetch_assoc()) {
echo "check-in: {$row['dt']}<br/>";
echo "check-out: {$row['dt2']}<br/>";
echo "<br/>";
}
} else {
echo "No bookings found between {$dt1} and {$dt2}";
die();
}
?>
What was wrong:
require_once
is called only for first iteration, no further calls was made. It's used mostly when including classes or some configs, that must be included only once in whole logic.source.php
$dt1
to $row['dt']
and $dt2
to $row['dt2']
You repeated the query in both files! Why? As you typed, you don't need to include 2 lines! , you can add them directly:
$dt1 = $_POST['date1'];
$dt2 = $_POST['date2'];
$query = "SELECT * FROM dt_tb WHERE dt BETWEEN '$dt1' AND '$dt2'";
$result = mysqli_query($connect,$query);
if (mysqli_num_rows($result)) {
while($row = $result->fetch_assoc()) {
echo "source.php";
echo "<br>check-in: $dt1 <br>check-out: $dt2";
}
echo "<br>formulier.php";
echo "<br>check-in: $dt1 <br>check-out: $dt2";
}
else {
die;
}