homepage Welcome to WebmasterWorld Guest from 50.17.66.61
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Seats booking system
name for each seat
lipe009



 
Msg#: 4599606 posted 1:14 am on Aug 6, 2013 (gmt 0)

So, i made a seat booking system where there are checkboxes in every seat and the user select this boxes and the seats are considered reserved.
But now i want make the user be able to give each marked seat a name.

seats.php

<html>
<head>
<title>Tickets</title>
<style>
* {
font-size: 11px;
font-family: arial;
}
</style>
<script>

function reserveSeats() {

var selectedList = getSelectedList('Reserve Seats');

if (selectedList) {
if (confirm('Do you want to reserve
selected seat/s '
+ selectedList + '?')) {
document.forms[0].oldStatusCode.value=0;
document.forms[0].newStatusCode.value=1;
document.forms[0].action='bookseats.php';
document.forms[0].submit();
} else {
clearSelection();
}
}
}


function cancelSeats() {

var selectedList = getSelectedList('Cancel Reservation');

if (selectedList) {
if (confirm('Do you want to cancel reserved seat/s ' + selectedList + '?')) {
document.forms[0].oldStatusCode.value=1;
document.forms[0].newStatusCode.value=0;
document.forms[0].action='bookseats.php';
document.forms[0].submit();
} else {
clearSelection();
}
}
}


function confirmSeats() {

var selectedList = getSelectedList('Confirm Reservation');

if (selectedList) {
if (confirm('Do you want to confirm reserved seat/s ' + selectedList + '?')) {
document.forms[0].oldStatusCode.value=1;
document.forms[0].newStatusCode.value=2;
document.forms[0].action='bookseats.php';
document.forms[0].submit();
} else {
clearSelection();
}
}
}


function getSelectedList(actionSelected) {

// get selected list
var obj = document.forms[0].elements;
var selectedList = '';
for (var i = 0; i < obj.length; i++) {
if (obj[i].checked && obj[i].name == 'seats[]') {
selectedList += obj[i].value + ', ';
}
}

// no selection error
if (selectedList == '') {
alert('Please select a seat before clicking ' + actionSelected);
return false;
} else {
return selectedList;
}

}

function clearSelection() {
var obj = document.forms[0].elements;
for (var i = 0; i < obj.length; i++) {
if (obj[i].checked) {
obj[i].checked = false;
}
}
}


function refreshView() {
clearSelection();
document.forms[0].action='<?php echo $_SERVER['PHP_SELF']; ?>';
document.forms[0].submit();
}

</script>
</head>
<body>
<table>
<tr><td width="100%" align="center">
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

<input type="hidden" name="oldStatusCode" value=""/>
<input type="hidden" name="newStatusCode" value=""/>

<table width='100%' border='0'>
<tr><td align='center'>
<input type='button' value='Refresh View' onclick='refreshView();'/>
</td></tr>
</table>
</td></tr>
<tr><td width="100%" align="center">
<table width='100%' border='0'>
<tr><td align='center'>
<input type='button' value='Reserve Seats' onclick='reserveSeats()'/>
&nbsp;<input type='button' value='Confirm Reservation' onclick='confirmSeats()'/>
&nbsp;<input type='button' value='Cancel Reservation' onclick='cancelSeats()'/>
</td></tr>
</table>
</td></tr>
<tr><td width="100%" align="center">
<table width='100%' border='0'>
<tr><td align='center'>
<input type='button' value='Clear Selection' onclick='clearSelection()'/></td>
</tr>
</table>
</td></tr>
<tr><td width="100%" align="center">
<?php
$linkID = @ mysql_connect("localhost", "tickets", "tickets") or die("Could not connect to MySQL server");
@ mysql_select_db("tickets") or die("Could not select database");
/* Create and execute query. */
$query = "SELECT * from seats order by rowId, columnId desc";
$result = mysql_query($query);
$prevRowId = null;
$seatColor = null;
$tableRow = false;
//echo $result;
echo "<table width='100%' border='0' cellpadding='3' cellspacing='3'>";
while (list($rowId, $columnId, $status, $updatedby) = mysql_fetch_row($result))
{
if ($prevRowId != $rowId) {
if ($rowId != 'A') {
echo "</tr></table></td>";
echo "\n</tr>";
}
$prevRowId = $rowId;
echo "\n<tr><td align='center'><table border='1' cellpadding='8' cellspacing='8'><tr>";
} else {
$tableRow = false;
}
if ($status == 0) {
$seatColor = "lightgreen";
} else if ($status == 1 && $updatedby == 'user1') {
$seatColor = "FFCC99";
} else if ($status == 1 && $updatedby == 'user2') {
$seatColor = "FFCCFF";
} else if ($status == 2 && $updatedby == 'user1') {
$seatColor = "FF9999";
} else if ($status == 2 && $updatedby == 'user2') {
$seatColor = "CC66FF";
} else {
$seatColor = "red";
}

echo "\n<td bgcolor='$seatColor' align='center'>";
echo "$rowId$columnId";
if ($status == 0 || ($status == 1 && $updatedby == $_SERVER['PHP_AUTH_USER'])) {
echo "<input type='checkbox' name='seats[]' value='$rowId$columnId'></checkbox>";
}
echo "</td>";
if (($rowId == 'A' && $columnId == 7)
|| ($rowId == 'B' && $columnId == 9)
|| ($rowId == 'C' && $columnId == 9)
|| ($rowId == 'D' && $columnId == 10)
|| ($rowId == 'E' && $columnId == 8)
|| ($rowId == 'F' && $columnId == 5)
|| ($rowId == 'G' && $columnId == 13)
|| ($rowId == 'H' && $columnId == 14)
|| ($rowId == 'I' && $columnId == 14)
|| ($rowId == 'J' && $columnId == 12)
|| ($rowId == 'K' && $columnId == 14)
|| ($rowId == 'L' && $columnId == 13)
|| ($rowId == 'M' && $columnId == 9)) {
// This fragment is for adding a blank cell which represent the "center aisle"
echo "<td>&nbsp;</td>";
}
}

echo "</tr></table></td>";
echo "</tr>";
echo "</table>";

/* Close connection to database server. */
mysql_close();
?>
</td></tr>
<tr><td>&nbsp;</td></tr>
<tr><td width="100%" align="center">
<table border="1" cellspacing="8" cellpadding="8">
<tr>
<td bgcolor='lightgreen'>Available</td>
<td bgcolor='FFCC99'>Reserved user1</td>
<td bgcolor='FF9999'>Confirmed user1</td>
<td bgcolor='FFCCFF'>Reserved user2</td>
<td bgcolor='CC66FF'>Confirmed user2</td>
</tr>
</table>
</td></tr>
<tr><td>&nbsp;</td></tr>
<tr><td width="100%" align="center">
<a href="seatplan.jpg" target="new">View Layout</a>
</td></tr>
</table>
</form>
</body>
</html>


bookseats.php

<html>
<head>
<title>Book Seats</title>
<style>
* {
font-size: 14px;
font-family: arial;
}
</style>
</head>
<body>
<center>
<br/>
<br/>
<br/>
<?php
if (isset($_POST['seats']))
{
$user = $_SERVER['PHP_AUTH_USER'];

$newStatusCode = $_POST['newStatusCode'];
$oldStatusCode = $_POST['oldStatusCode'];

// open database connection
$linkID = @ mysql_connect("localhost", "tickets", "tickets") or die("Could not connect to MySQL server");
@ mysql_select_db("tickets") or die("Could not select database");

// prepare select statement
$selectQuery = "SELECT rowId, columnId from seats where (";
$count = 0;
foreach($_POST['seats'] AS $seat) {
if ($count > 0) {
$selectQuery .= " || ";
}
$selectQuery .= " ( rowId = '" . substr($seat, 0, 1) . "'";
$selectQuery .= " and columnId = " . substr($seat, 1) . " ) ";
$count++;
}
$selectQuery .= " ) and status = $oldStatusCode";
if ($oldStatusCode == 1) {
$selectQuery .= " and updatedby = '$user'";
}

//echo $selectQuery;

// execute select statement
$result = mysql_query($selectQuery);
//echo $result;

$selectedSeats = mysql_num_rows($result);
//echo "<br/>" . $selectedSeats;

if ($selectedSeats != $count) {
$problem = "<h3>There was a problem executing your request. No seat/s were updated.</h3>";
$problem .= "Possible problems are:";
$problem .= "<ul>";
$problem .= "<li>Another process was able to book the same seat while you were still browsing.</li>";
$problem .= "<li>You were trying to Confirm an unreserved Seat.</li>";
$problem .= "<li>You were trying to Cancel an unreserved Seat.</li>";
$problem .= "<li>You were trying to Reserve a reserved Seat.</li>";
$problem .= "<li>There was a problem connecting to the database.</li>";
$problem .= "</ul>";
$problem .= "<a href='seats.php'>View Seat Plan</a>";
die ($problem);
}

// prepare update statement
$newStatusCode = $_POST['newStatusCode'];
$oldStatusCode = $_POST['oldStatusCode'];

$updateQuery = "UPDATE seats set status=$newStatusCode, updatedby='$user' where ( ";
$count = 0;
foreach($_POST['seats'] AS $seat) {
if ($count > 0) {
$updateQuery .= " || ";
}
$updateQuery .= " ( rowId = '" . substr($seat, 0, 1) . "'";
$updateQuery .= " and columnId = " . substr($seat, 1) . " ) ";
$count++;
}
$updateQuery .= " ) and status = $oldStatusCode";
if ($oldStatusCode == 1) {
$updateQuery .= " and updatedby = '$user'";
}

// perform update
$result = mysql_query($updateQuery);
$updatedSeats = mysql_affected_rows();

if ($result && $updatedSeats == $count) {
//$mysql->commit();
include ('reservapt2.php');
} else {
//$mysql->rollback();
echo "<h3>There was a problem executing your request. No seat/s were updated.</h3>";
echo "Possible problems are:";
echo "<ul>";
echo "<li>Another process was able to book the same seat while you were still browsing.</li>";
echo "<li>You were trying to Confirm an unreserved Seat.</li>";
echo "<li>You were trying to Cancel an unreserved Seat.</li>";
echo "<li>You were trying to Reserve a reserved Seat.</li>";
echo "<li>There was a problem connecting to the database.</li>";
echo "</ul>";
}

echo "<a href='seats.php'>View Seat Plan</a>";

// Enable the autocommit feature
//$mysqldb->autocommit(TRUE);

// Recuperate the query resources
//$result->free();

mysql_close();
}
?>
</center>
</body>
</html>


I tried the following: When the user submit the checkboxes, this is included:

name.php

<img src="mapa.png" alt="mapa">
<form method="post" name="name" action="pt2.php" >

<?php
//$mysql->commit();
echo "<h3>";
echo "Please enter the name for each seat:<br><p>&nbsp</p>";
echo "";
foreach($_POST['seats'] AS $seat) {
$rowId = substr($seat, 0, 1);
$columnId = substr($seat, 1);
echo $rowId . $columnId . '<input type="hidden" name="seats[]" value="' . $seat . '"><input name="' . $seat . 'name'" type="text"/></br>';
}
;

?>

<input type="submit" name="submit" value="Submit Form"><br>
</form>



pt2.php:

<?php
// Connect to MySQL
mysql_connect("localhost", "root", "root") or die("Connection Failed");
mysql_select_db("tickets")or die("Connection Failed");


$namei = $_POST[$seat . 'name'];

foreach ($_POST['seats'] as $seat){
echo $seat;
$query = "INSERT INTO seatnames (seatname) VALUES ('$namei')";
mysql_query($query) or die(mysql_error());
}
?>





For some reason it insert a blank space in the table, instead of the name! Does someone know how to fix that?

 

jatar_k

WebmasterWorld Administrator jatar_k us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4599606 posted 9:02 pm on Aug 7, 2013 (gmt 0)

I only looked at the insert code, because that's way too much for me to read through and analyze.

you should test your data to find out why it's not working

what's in $seat? it's first mention is in this line
$namei = $_POST[$seat . 'name'];

which looks like it would end up pulling $_POST['name'], which I am guessing is unset, and putting that into $namei which is then blank

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved