Jump to content

How to calculate price of 1 or more hotel rooms in php using arrays


webdeveloper123
Go to solution Solved by Barand,

Recommended Posts

Hi Guys,

Been looking at this for a few days now, but not sure how to go about it. I wanted to book 1 or more hotel rooms (depending on user input) but I'm stuck as to how to do it. I have the following code: (only showing relevant parts)

<?php
$reservedrooms = [];

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

$reservedrooms = $_POST['reservedrooms'] ?? '';

$sqlbookquery = "SELECT r.room_id, rs.price FROM room AS r INNER JOIN roomsize AS rs ON r.size_id = rs.size_id;";
$statement1 = $pdo->query($sqlbookquery);
$bookquery  = $statement1->fetchAll();

}

?>

The reservedrooms array contains room_id from a checkbox on the search page. So If the user wanted to book 3 rooms, it will pick up the 3 matching ID's. I have tested this, it is working properly.

Then to book the rooms I was thinking to compare the 2 arrays, and return the matching ID's, along with their price, then multiply that by the number of nights (I already done this part, I get the correct result of nights). It is stored in a variable.

On the comparing arrays, I tried to use array_intersect but I got an error (probably because I was comparing 2 arrays that didn't match up)  E.g the reservedrooms array has only room_id in there and the bookquery has room_id and price. That is just a theory. Is it even like that?

There are several other array functions I was looking at but not sure which one to use. in_array, array_search, array_diff. 

Can someone please give me some help on how to go about this? 

Link to comment
Share on other sites

Your data probably looks something like this

$reservedrooms = [2, 4, 6];

$bookquery  = [ 0 => ['room_id' => 2, 
                      'price' => '125'
                      ],
                1 => ['room_id' => 3, 
                      'price' => '125'
                      ],
                2 => ['room_id' => 4, 
                      'price' => '125'
                      ],
                3 => ['room_id' => 5, 
                      'price' => '125'
                      ]
              ];

You can get an array of room ids from $bookquery in same format as $reserved rooms with

$queryrooms = array_column($bookquery, 'room_id')

 

Link to comment
Share on other sites

Hey Barand,

Sorry quick question. I am using the following code:

$bookquery = array_filter($bookquery, fn($v)=>in_array($v['room_id'], $reservedrooms));

$totalprice = array_sum(array_column($bookquery, 'price'));

Works great. But I was trying to find bugs in my app and I thought what If I tried to not select any rooms for booking from the search screen and just press "Book". It gives me this error:

Fatal error: Uncaught TypeError: in_array(): Argument #2 ($haystack) must be of type array, 
string given in /var/www/vhosts/:58 Stack trace: #0 /var/www/vhosts/): in_array() 
#1 [internal function]: {closure}() #2 /var/www/vhosts/: array_filter() 
#3 {main} thrown in /var/www/vhosts booking.php on line 58

Now I read the manual & I've used in_array before and the error is saying the second argument must be array (which in this case would be $reservedrooms) but it's receiving a string instead. So I'm assuming that the blank array is being interpreted as a string. I tried to fix is like this:

if (!$reservedrooms) {
	echo "You must select a room";
}

The echo line shows , but so does the original errror.

Here is my related code:

<?php


$formattedInterval = $difference->format('%d');


 
if (!$reservedrooms) {
	echo "You must select a room";
}

$bookquery = array_filter($bookquery, fn($v)=>in_array($v['room_id'], $reservedrooms));

$totalprice = array_sum(array_column($bookquery, 'price'));

$alltogether = $totalprice * $formattedInterval;

$alltogether  = number_format($alltogether);

?>

Thanks

Link to comment
Share on other sites

Hey Barand,

No it doesn't look like anything is being over written. But I do have a count() statement later on on $reservedrooms so that I can output how many rooms the user has booked. But I can't see how the 2 are linked. Here is my full code:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Booking</title>
</head>
<body>



<?php

include 'includes/db.php'; 
include 'includes/functions.php'; 


$reservedrooms = [];


$today = new DateTime();


if ($_SERVER['REQUEST_METHOD'] == 'POST') {

$reservedrooms = $_POST['reservedrooms'] ?? '';
$arrivalDate = $_POST['arrivaldate'];
$departureDate = $_POST['departuredate'];

$totaladults = $_POST['numofadults'] ?? '';
$totalchild = $_POST['numofchild'] ?? '';


$arrivalObj = new DateTime($arrivalDate);
$departureObj = new DateTime($departureDate);

$difference = $arrivalObj->diff($departureObj);

$formattedInterval = $difference->format('%d');

$sqlbookquery = "SELECT r.room_id, rs.price FROM room AS r INNER JOIN roomsize AS rs ON r.size_id = rs.size_id;";

$statement1 = $pdo->query($sqlbookquery);
$bookquery  = $statement1->fetchAll();

if (!$reservedrooms) {
} else {
		echo "You must select a room";
}

 
$bookquery = array_filter($bookquery, fn($v)=>in_array($v['room_id'], $reservedrooms));

$totalprice = array_sum(array_column($bookquery, 'price'));

$alltogether = $totalprice * $formattedInterval;

$alltogether  = number_format($alltogether);

if ($formattedInterval == 0) {
   echo "Price for the night is: &pound;$totalprice<br>";

} else {
	echo "Total Price: &pound;$alltogether<br>";

}

if ($arrivalObj == $departureObj) {
	echo "You will be charged for a minimum of 1 night<br>";
} else {
	echo $difference->format('%d nights<br>');
}


if ($totaladults == 1) {
	echo "$totaladults Adult<br>";
} else {
	echo "$totaladults Adults<br>";
}



if ($totalchild == 1) {
	echo "$totalchild Child<br>";
} elseif ($totalchild == 0) {
	echo '';
} else {
	echo "$totalchild Children<br>";
}

 
 $countrooms = count($reservedrooms);

 if ($countrooms == 1) {
 	echo "$countrooms Room";
 } else {
 	echo "$countrooms Rooms";
 }


}

?>



<p>Arrival date is:</p>
<?= htmlspecialchars($arrivalObj->format('D, jS M Y')) ?>
<p>Departure date is:</p>
<?= htmlspecialchars($departureObj->format('D, jS M Y')) ?>




</body>
</html>

Thanks

Link to comment
Share on other sites

Hey dodgeitorelse3,

I changed it to:

$reservedrooms = $_POST['reservedrooms'];

And:

$reservedrooms = $_POST['reservedrooms'] ?? 0;

On both of them, I still get the same error. And on the first one I get an additional error:

Warning: Undefined array key "reservedrooms" in /var/www/vhosts/ booking.php on line 25

Thanks

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.