Skip to content

Instantly share code, notes, and snippets.

@alrafiabdullah
Last active November 28, 2021 13:52
Show Gist options
  • Save alrafiabdullah/78719616103637ca94fad4a089d281df to your computer and use it in GitHub Desktop.
Save alrafiabdullah/78719616103637ca94fad4a089d281df to your computer and use it in GitHub Desktop.
Review page of CSE482 using PHP. Many useful code snippets are here.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Review</title>
<link rel="shortcut icon" href="assets/favicon.ico" type="image/x-icon" />
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css" />
<link
rel="stylesheet"
href="https://unpkg.com/swiper/swiper-bundle.min.css"
/>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css" integrity="sha384-B0vP5xmATw1+K9KRQjQERJvTumQW0nPEzvF6L/Z6nronJ3oUOFUFpCjEUQouq2+l" crossorigin="anonymous" />
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css"
/>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/js/bootstrap.min.js" integrity="sha384-+YQ4JLhjyBLPDQt//I+STsc9iw4uQqACwlvpslubQzn4u2UU2UFM80nGisd026JF" crossorigin="anonymous"></script>
<div class="bg">
<div class="form-container">
<header>
<div id="menu-bar" class="fas fa-bars"></div>
<a href="home.html" class="logo"><span>Hotel</span>Finder</a>
<nav class="navbar">
<a href="home.html">Home</a>
<a href="findhotel.html">Find Hotels</a>
<a href="package.html">packages</a>
<a href="service.html">services</a>
<a href="review.php">review</a>
<a href="contact.html">contact</a>
</nav>
<div class="icons"></div>
</header>
<div class="home" id="home">
<div id="successmessage" style="display: none;" align="center">
<h3 class="alert alert-success">Recommendation was succesfull!</h3>
</div>
<div id="failuremessage" style="display: none;" align="center">
<h3 class="alert alert-success">Recommendation was succesfull!<br>We will contact the relevant authority.</h3>
</div>
<div class="box-container">
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "hotel_finder";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//get all the hotels
$sql = "SELECT * FROM hotels";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
// get count of greater than 0 rating for each hotel
$rating_sql = "SELECT COUNT(*) FROM ratings WHERE hotel_id = '".$row["id"]."' AND rating > 0";
$rating_result = $conn->query($rating_sql);
$rating_value = $rating_result->fetch_assoc();
$rating_count = $rating_value["COUNT(*)"];
$discount = $row["hotel_price"]+round(rand(3000,5000), -2);
$discount_percent = round(($discount-$row["hotel_price"])/$row["hotel_price"]*100);
//get total count of ratings for each hotel
$total_rating_sql = "SELECT COUNT(*) FROM ratings WHERE hotel_id = '".$row["id"]."'";
$total_rating_result = $conn->query($total_rating_sql);
$total_rating_value = $total_rating_result->fetch_assoc();
$total_rating_count = $total_rating_value["COUNT(*)"];
echo'
<div class="box">
<img
src='.$row["hotel_image"].'
alt='.$row["hotel_name"].'
/>
<div class="content">
<h3>'.$row["hotel_name"].'</h3>
<p>'.$row["hotel_address"].', '.$row["hotel_city"].'</p>
<div class="price"><p>BDT <strong>'.$row["hotel_price"].'</strong> <span>BDT <strong>'.$discount.'</strong></span><small style="font-size: 1.5rem; color: red; font-weight: bold;"> '.$discount_percent.'% Off!</small></p>
</div>
<div class="stars">
<p>Recommended by <strong style="color: green">'.$rating_count.'</strong> out of <strong style="color: red">'.$total_rating_count.'</strong> travellers!</p>
</div>
<h3>Do you recommend this hotel?</h4>
<form action="review.php" style="
margin-bottom: 5px;
float: left;
border-radius: 0;
background: inherit;
width:0;"
method="post">
<input type="hidden" name="no'.$row["id"].'" value='.$row["id"].'>
<input type="submit" name="no" class="btn-danger p-3" style="font-size: 2rem;" value="No"/>
</form>
<form action="review.php" style="
margin-bottom: 5px;
float: left;
border-radius: 0;
background: inherit;
width:0px;"
method="post">
<input type="hidden" name="yes'.$row["id"].'" value='.$row["id"].'>
<input type="submit" name="yes" class="btn-success p-3" style="font-size: 2rem;" value="Yes" />
</form>
</div>
</div>';
if (!empty($_POST['yes'.$row['id'].''])){
// add a ratings entry for this hotel with value 1
$rating_sql = "INSERT INTO ratings (hotel_id, traveller_id, rating) VALUES ('".$row["id"]."', 1, 1)";
$conn->query($rating_sql);
echo '<script>const showMessage = () => {
document.getElementById("successmessage").style.display = "block";
setTimeout(() => {
document.getElementById("successmessage").style.display = "none";
}, 5000);
}
showMessage();
</script>';
echo "<meta http-equiv='refresh' content='0'>";
}
if (!empty($_POST['no'.$row['id'].''])){
// add a ratings entry for this hotel with value 0
$rating_sql = "INSERT INTO ratings (hotel_id, traveller_id, rating) VALUES ('".$row["id"]."', 1, 0)";
$conn->query($rating_sql);
echo '
<script>const showMessage = () => {
document.getElementById("failuremessage").style.display = "block";
setTimeout(() => {
document.getElementById("failuremessage").style.display = "none";
}, 5000);
}
showMessage();
</script>
';
echo "<meta http-equiv='refresh' content='0'>";
}
}
} else {
echo '
<div class="box">
<div class="content">
<h3>No Hotels Found</h3>
</div>
</div>
';
}
$conn -> close();
?>
</div>
</div>
<!-- <div>
<?php
// $servername = "localhost";
// $username = "root";
// $password = "";
// $dbname = "hotel_finder";
// Create connection
// $conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
// if ($conn->connect_error) {
// die("Connection failed: " . $conn->connect_error);
// }
// $sql = "CREATE TABLE travellers (
// id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
// firstname VARCHAR(255) NOT NULL,
// lastname VARCHAR(255) NOT NULL,
// username VARCHAR(255) NOT NULL UNIQUE,
// traveller_password VARCHAR(255) NOT NULL,
// city VARCHAR(255) NOT NULL
// )";
// if ($conn->query($sql) === TRUE) {
// echo "Table travellers created successfully";
// } else {
// echo "Error creating table: " . $conn->error;
// }
// create entry in travellers table
// $first_traveller_sql = "INSERT INTO travellers (firstname, lastname, username, traveller_password, city)
// VALUES ('Jane', 'Doe', 'second_username', md5('testpassword'), 'Rajshahi')";
// if ($conn->query($first_traveller_sql) === TRUE) {
// echo "New record created successfully";
// } else {
// echo "Error: " . $first_traveller_sql . "<br>" . $conn->error;
// }
// $hotel_sql = "CREATE TABLE hotels (
// id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
// hotel_name VARCHAR(255) NOT NULL,
// hotel_address VARCHAR(255) NOT NULL,
// hotel_city VARCHAR(255) NOT NULL,
// hotel_rating INT(6) NOT NULL DEFAULT 0,
// hotel_price INT(6) NOT NULL DEFAULT 0,
// hotel_image VARCHAR(255) NOT NULL
// )";
// if ($conn->query($hotel_sql) === TRUE) {
// echo "Table hotels created successfully";
// } else {
// echo "Error creating table: " . $conn->error;
// }
// create entry in hotels table
// $first_hotel_sql = "INSERT INTO hotels (hotel_name, hotel_address, hotel_city, hotel_rating, hotel_price, hotel_image)
// VALUES ('Le Méridien Dhaka', '79/A Commercial Area Airport Rd, Dhaka 1229', 'Dhaka', 0, 18000, 'https://lh3.googleusercontent.com/p/AF1QipMfGhnqy7el0bgos9YxtT5Ya1Zjtb1wsBh24uGI=w592-h404-n-k-rw-no-v1')";
// if ($conn->query($first_hotel_sql) === TRUE) {
// echo "New record created successfully";
// } else {
// echo "Error: " . $first_hotel_sql . "<br>" . $conn->error;
// }
// $rating_sql = "CREATE TABLE ratings (
// id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
// hotel_id INT(6) NOT NULL,
// traveller_id INT(6) NOT NULL,
// rating INT(6) NOT NULL
// )";
// if ($conn->query($rating_sql) === TRUE) {
// echo "Table hotel rating created successfully";
// } else {
// echo "Error creating table: " . $conn->error;
// }
// create entry in ratings table
// $first_rating_sql = "INSERT INTO ratings (hotel_id, traveller_id, rating)
// VALUES (1, 1, 5)";
// $conn -> close();
?>
</div> -->
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment