PHP reservation system passing specific while loop results to next php page - php

Making a car rental reservation system for a school project, project is in html/php & SQL. So far the user picks location date/time and car category on page 1, user then searches available cars on page 2. on page 2 the cars are listed and user picks the car they want to rent, the cars are listed in while loop from
the database. there is a reserve button attached to each car so that the user can pick.
Heres my issue how do i pass over the cars details as variables(make, model, class, dailyrate) for what the user picks onto the next php page prompting page. im using $_sessions.
<?php
$sql= "SELECT * FROM vehicles WHERE class='$class'";
$result = mysqli_query($db,$sql);
$resultCheck = mysqli_num_rows($result);
if ($resultCheck > 0) {
while ($row = mysqli_fetch_assoc($result)) { //beginning of while loop
$_SESSION['make']=$row['make'];
$_SESSION['model']=$row['model'];
$_SESSION['class']=$row['class'];
$_SESSION['dailyrate']=$row['daily_rate'];
?>
<div class="card">
<div class="img">
<img src='../../images/defaultcar.png' width='100' height='100'>
</div>
<div>
Make: <?php echo $row['make']; ?><br>
Model: <?php echo $row['model']; ?><br>
<b>Class: <?php echo $row['class']; ?></b><br>
<div class="price"> <b>$ <?php echo $row['daily_rate']; ?></b>/Per day</div>
</div>
<button class='reserve-btn'>Reserve</button>
</div>;
<?php } //ending bracket of while loop
}
else {
echo "0 results";
}
?>
</div>

I will try to help you through google translate, the easiest way to do it is by adding the car ID in the value of the reserve button, then declare a name for the button and send that ID to the next page, getting there only from another SELECT * FROM CARS WHERE ID =? and get the information

Related

Inserting PHP database into HTML table

Hey I've recently been making a website and want to display the data from my database in a grid format opposed to it just listing down the page.
Here is my code right now:
<p>
<a href="pokemondetails.php?dex=<?php echo $row['dex'];?>">
<?php echo $row['name']; ?>
<br>
<img src="assets/<?php echo $row['dex']?>.png">
</a>
</p>
I was wondering how I would go about creating a for loop to allow the data from this database in conjunction with the image to span across the page with 7 columns and however many rows down until it reaches the end of the database.
Thanks!
<?php
$query = "Select * from tablename";
$bind = $conn->query($query);
if ($bind->num_rows > 0){
while ($row = $bind->fetch_assoc()){
?>
<p>
<a href="pokemondetails.php?dex=<?php echo $row['dex'];?>">
<?php echo $row['name']; ?>
<br>
<img src="assets/<?php echo $row['dex']?>.png">
</a>
</p>
<?php
}
}
?>
Try this, I just add while loop until End Of file (EOF table)

IF inside WHILE with database result not working

I have a simple ticket sale registration system with 6 types of tickets and 3 different "physical" sales locations. Each order is inserted as a row in the database and stores the amount of each type of ticket along with the sale location, total cost and a datetime timestamp.
I want to display the total amount of each ticket type, that was sold within a given time frame along with the total cost of those tickets. I also want to filter the results based on sale location.
This is my db query:
"SELECT SUM(ticketType1), SUM(ticketType2), SUM(ticketType3),
SUM(ticketType4), SUM(ticketType5), SUM(ticketType6),
SUM(cost), saleLocation
FROM `orders`
WHERE time BETWEEN '2018-07-10 07:00:01'
AND '2018-07-11 07:00:00'"
Then I display it in a HTML table row:
<?php
while ($row = $result->fetch_assoc()) {
if($row["saleLocation"] == 'location1') { ?>
<div class="cell">
<?php echo $row["SUM(ticketType1)"] ?>
</div>
<div class="cell">
<?php echo $row["SUM(ticketType2)"] ?>
</div>
<div class="cell">
<?php echo $row["SUM(ticketType3)"] ?>
</div>
<div class="cell">
<?php echo $row["SUM(ticketType4)"] ?>
</div>
<div class="cell">
<?php echo $row["SUM(ticketType5)"] ?>
</div>
<div class="cell">
<?php echo $row["SUM(ticketType6)"] ?>
</div>
<div class="cell">
<?php echo number_format($row["SUM(cost)"], 0, "", "."); ?>
</div>
<?php } //end if
} // end while
?>
It works, but I'm trying to use an IF statement inside the WHILE loop to filter the result based on location, but the IF statement doesn't appear to have any effect and instead it displays the results from all 3 locations.
I know I can easily modify the query to achieve this, but I would rather not do that in this specific case.
I'm guessing that the problem still lies within the query, though?
Some help would be greatly appreciated.

Two query at the same time

Hello first of all what i am doing in , i am coding a website for advertise .
Now what do i need is a help to display a lots of data from two tables of database .
What i have done so far u can check at My project you have to login use (Username : test , password : 123456a) to login , so there is everything is okay except an image image are the same on every ads and i do not find the way to make it right .
So i have a "posts" table with an information about ads and an "images" table with a path of an image this is how its looks like :
and this is my code :
<?php
$userid = $_SESSION["userid"];
$sql = "SELECT * FROM posts WHERE userid='$userid' ";
$res = mysqli_query($connect,$sql);
while ($row = mysqli_fetch_assoc($res)) {
?>
<div id="ads">
<div id="titlepic">
<?php echo $row["title"]; ?><br>
<img src="<?php echo $Photo[0]; ?>" height="100px;">
</div>
<div id="managead">
Edit<br style="margin-bottom: 5px;">
Delete<br style="margin-bottom: 5px;">
Renew
</div>
<div id="dates">
<b>Date Added:</b> <?php echo date('m/d/Y', $row["dateadded"]); ?><br>
<b>Renew Date:</b> <?php if($row["renewdate"] > 0){ echo date('m/d/Y', $row["renewdate"]); } ?><br>
<b>Location:</b> <?php echo $row["location"]; ?><br>
<b>Price:</b> <?php echo $row["price"]; ?><br>
</div>
</div>
<hr width="100%">
<?php
so the question is how to extract and images from other table at the same time or how tu run two query at the same time and get an information from them
your SQL statement needs a JOIN in order to include data from two tables in one query.
$sql = "
SELECT *
FROM posts p
JOIN images i
ON p.id = i.postid
WHERE p.userid='$userid'
";
this result set will be populated with all columns from both tables. now you can access path1 via:
<?php echo $row["path1"]; ?>
while this will work for all of your images, such as $row["path2"], $row["path3"], etc, keep in mind this is a bad design for a many-to-many relationship, so it should be normalized to include a linking table which would hold all of your images.

Looping through PHP database with columns that link to specific profiles

I have a website where I am getting information of college student profiles on a database and displaying it as a linked collection. When I'm looping through the database I want to give each row a specific link to the profile of the student. Right now I am linking it to "profilePage.html" with generic information but I want it to be correlated with the row the user chose on the last(college) page.How do I save/transfer that information to the page. I do not want multiple profile pages but one template that is filled with the user previous choice.
<?php
$result = mysql_query("SELECT * FROM student_info WHERE college='Boston College'", $db);
if (!$result) {
die("Database query failed: " . mysql_error());
}
while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
?>
<a href="profilePage.html" class="collection-item">
<div class="row summary">
<div class="col s4 center">
<img class = "profile-pic" src="img/defaultProfile.jpg">
</div>
<div class="col s8">
<div class="title"> <?php echo $row[student_Name]; ?> </div>
<div class="black-text condensed thin"><i class="tiny material-icons">today</i> Founder, CEO at Linkle since January 1st, 2015</div>
<div></div>
</div>
</div>
</a>
<?php } ?>
Key thing, my urls are mysite.com/college.php and have no id's to specify them.
Structure of the Database student_info:
Shows the structure of the database
First, do you have an URL Rewriting ? If not, your target page should be a PHP page, like profilePage.php.
Your link to this page have to include a parameter (Query String) which is, for example, the student's ID :
<a href="profilePage.php?id=<?php echo $row['id'] ?>" class="collection-item">
This type of URL will be generated: profilePage.php?id=36
In profilePHP.php, retrieve the parameter in the Query String :
<?php
$idStudent = mysql_real_escape_string($_GET['id']);
?>
mysql_real_escape_string() is really important, it prevents SQL injections.
After that, you could retrieve the student's informations with a SQL query.
<?php
$idStudent = mysql_real_escape_string($_GET['id']);
$sql = sprintf("SELECT * FROM student_info WHERE id = %s", $idStudent);
$query = mysql_query($sql);
$student = mysql_fetch_object($query);
// you should check if the ID exists, and if there is 1 result
?>
<p>Student name is <?php echo $student['student_Name'] ?></p>
A little advice: mysql_query() will disappear soon, you should take a look at PDO.

Different Order numbers for same patient only allows me to get one from DB

So I have a table called Orders.
In this table I have
ordernum
name
Everytime I add a new order it creates a new order number the name only changes if I change it... So for instance..
Donald could have 20 different orders in that table..
Now I am trying to display all the order numbers for Donald
This is my current code.
<?php
$blue = $_SESSION['name']; // This is the customers name
$sqll = "SELECT * FROM orders WHERE name = :blah";
$qq=$con->prepare($sqll);
$qq->bindparam(":blah", $blue);
$qq->execute();
$u=$qq->fetch(PDO::FETCH_ASSOC);
?>
<div class="sectionContent">
<div class="sectionFull">
<div class="sectionOneHalf">
<fieldset>
<legend>Orders</legend>
<ol>
<li>
<label>Orders</label>
// Here I am trying to display all the different order numbers in teh table that are listed under his name
<div class="OrdersContainer">
<?php echo $u['ordernum'];?>
</div>
</li>
</ol>
<br>
</fieldset>
</div>
</div>
Problem is, When I try to list those orders now... I only get one that shows up.
I need to display them all for that specific persons name
The problem is you're fetching only one row from the result set. Loop through the result set to display all orders, like this:
while($u=$qq->fetch(PDO::FETCH_ASSOC)){
// display orders
}
Your code should be like this:
// your code
<li>
<label>Orders</label>
<div class="OrdersContainer">
<?php
while($u=$qq->fetch(PDO::FETCH_ASSOC)){
// display orders
echo $u['ordernum'] . "<br />";
}
?>
</div>
</li>
// your code
<?php
$blue = $_SESSION['name']; // This is the customers name
$sqll = "SELECT * FROM orders WHERE name = :blah";
$qq=$con->prepare($sqll);
$qq->bindparam(":blah", $blue);
$qq->execute();
?>
<div class="sectionContent">
<div class="sectionFull">
<div class="sectionOneHalf">
<fieldset>
<legend>Orders</legend>
<ol>
<li>
<label>Orders</label>
// Here I am trying to display all the different order numbers in teh table that are listed under his name
<div class="OrdersContainer">
<?php
while($u=$qq->fetch(PDO::FETCH_ASSOC))
{
echo $u['ordernum'];
}
?>
</div>
</li>
</ol>
<br>
</fieldset>
</div>
</div>
</div>
Change fetch to fetchall should solve your problem, you appear to be fetching only a single row so thats all you will get.
Instead of
$u=$qq->fetch(PDO::FETCH_ASSOC);
use
$u=$qq->fetchAll(PDO::FETCH_ASSOC);
You can try this:
$blue = $_SESSION['name']; // This is the customers name
$sqll = "SELECT * FROM orders WHERE name = :blah";
$qq=$con->prepare($sqll);
$qq->bindparam(":blah", $blue);
$qq->execute();
while ($row = $qq->fetch(PDO::FETCH_ASSOC)) {
// your html
echo $row['ordernum'] . "\n";
// your html
}
What Change?
Adding while loop

Categories