HTML table pass Session variable for each record

I’m trying to build a car rental website and im pretty new to PHP so i cant figure this out. I’ve displayed the records in a table and also added a booking button to each record. Now,when a user clicks on the button, i need to transfer the values of that specific record to a booking page where the user can fill in some more details. So far, ivebeen trying it with sessions and it doesnt work.

display page

[php]<?php session_start(); ?>

Untitled Document <?php include("header.php"); $disp = "select * from selfdrive where City = '" . strtoupper($_SESSION['city']) . "' and Availability = 'YES' "; $result = mysqli_query($conn, $disp); echo $num = mysql_num_rows($result);

?>

<?php
$i=1;
$num = mysqli_num_rows($result);
while ($row = mysqli_fetch_array($result)) 
{		

?>
<form action="self-booking.php" method="get">
<?php } ?>
  Model Class Deposit Daily Rate
<?php echo $row['Model']?> <?php echo $row['Class']?> <?php echo $row['Deposit']?> <?php echo $row['Rate']?> <?php for($i=0;$i < $num;$i++){$_SESSION['row']['$i'] = $row['$i'];}?>
[/php]

and this is the booking page

[php]<?php
session_start();
include(“session.php”);
include(“header.php”);
?>

Booking <?php if(!isset($_SESSION['login_user'])) header("Location: login.php"); else{ ?>

Login

<?php $row = $_SESSION['row']['$i']; ?>
<div id="imageframe">
	<img src="<?php echo $row['Image']?>" width="600">
</div>
<div id="vehicle-details">
<?php	echo "Model: ".$row['Model']."<br><br>";
		echo "Class: ".$row['Class']."<br><br>";
		echo "Deposit: Rs.".$row['Deposit']."<br><br>";
		echo "Daily Rate: Rs.".$row['Rate']."<br><br>";
?>
</div>
<?php } ?> <?php include("footer.php"); ?> [/php]

Well, Aditya, basically any sales site, purchase site or car rental site would be handled in a similar manner.
This could become a long post if you are a newbie, but, let’s explain how most programmers would do it and
then, you can ask further questions…

When you create a MySQLi database table to be used for displaying and for selecting items from it, you do
need to figure out an indexing scheme that makes life easier for you. MySQLi has that built into it already.
Almost all programming samples will show this in just about any tutorial that you find online. It is simply to
use one table field as the index and to set it to be auto-incremented. Then, when you add an item to it, you
will have a UNIQUE id that can be used in your pages when you display the item or select it for use.

Therefore, for a table of cars to rent, you might have id, image, model, class, rate, whatever… Note that
I added in id… That would be set to auto-increment in the database table.

When you display it, you can add buttons that use the “id” as a value and it makes your work so much
easier. Let’s talk about your current code… First, you have a bad error in it. You loop thru the data that
is in your car table and for each entry, you create a new with the same name. That is wrong.
You need to move your line AND the titles

above the WHILE loop. Then, display the data from
the database query as a separate row for each car. Next, when you display a row of data for one car, you
need to have a simple way for the user to select one of them. Let’s say there is only three cars in the list:
pix1 - Chevy - RaceCar - etc - etc –
pix2 - Ford - RaceCar - etc - etc –
pix3 - Chevy - RaceCar - etc - etc –
How can the user pick one if you have a submit at the bottom of the list? It would be better to either
number each row and make them type in a number that matches a row or just place a button in each line
of the list that they could press on. You would just need to add another col to the display so that it would
allow them to select that row.
Select_Button1 - pix1 - Chevy - RaceCar - etc - etc –
The button would be assigned a value of the id number in the table with a name like “Select_Car” or any
name you want. It would be marked as type “submit” so that it would post the form. Then, you would add
code to the PHP section in the self-booking.php file to handle setting up the booking for the car that is then
marked in the posted $_POST[“Select_Car”] variable. Since each VALUE of the button would be unique for
each car, it is very easy to use that posted value to handle your booking. To place a button into your code,
you would use something like this:
[php]
while ($row = mysqli_fetch_array($result)) {
echo “”;
echo “<button type=‘submit’ name=‘Select_Car’ value=” . $row[“id”] . “>Select this car”;
echo “” . $row[“Image”] . “”;
echo “” . $row[“Model”] . “”;
echo “” . $row[“Class”] . “”;
echo “” . $row[“Deposit”] . “”;
echo “” . $row[“Rate”] . “”;
echo “”;
}
[/php]
As you can see, it adds in a button in the first col of the list of cars. If pressed the form is submitted and
you can pull this data in the booking code by using the $_POST[“Select_Car”] variable. You do not need
to pull the other info as you only need the index into the one car that the user picked. In your booking
code, you need to mark the car as reserved or something. Therefore, you will need to add in some sort of
“status” field in the database so that a second user does not pick the same car. I think that makes sense!

This is basically how most sites on the internet access and pass on values to other code. You need to use
a UNIQUE identifier for anything or any person on your site. For instance, you do the same for your users by
having a unique identifier for them. Then, you can store that ID in a $_SESSION variable and use it on every
page to keep them remembered. (Or, you can pass it in the posted values if you wish.)
Is this for a class project? Just curious… Hope this helps you!

Sponsor our Newsletter | Privacy Policy | Terms of Service