2013-08-05 134 views
-2

所以,我犯了一個座位預訂系統哪裏有每個座位複選框,並且用戶選擇該箱和座椅被考慮保留。座位預訂系統

但現在我想使用戶能夠給每個座位標記的名稱。我怎樣才能做到這一點?

seats.php

<?php include("login.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 
/* 
* Created on Mar 17, 2007 
* Author: dayg 
*/ 

$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> 
<?php include("login.php"); ?> 
<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(); 
      echo "<h3>"; 
      echo "You have successfully updated $updatedSeats seat/s: "; 
      echo "["; 
      foreach($_POST['seats'] AS $seat) { 
       $rowId = substr($seat, 0, 1); 
       $columnId = substr($seat, 1); 
       echo $rowId . $columnId . ", "; 
      } 
      echo "]"; 
      echo "...</h3>"; 
     } 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> 

編輯:

我試過如下: 當用戶提交的複選框,這包括:

nam e.php

<?php 
//$mysql->commit(); 
session_start(); 
$_SESSION['rowId'] = $_POST['rowId']; 
$_SESSION['columnId'] = $_POST['columnId']; 
      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 . '</br><form method="post" name="input" action="pt2.php" > 
<input name="name" type="text"/></br>'; 
      } 
?> 

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

pt2.php:

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

$name = $_POST['name']; 
session_start(); 
$rowId = $_SESSION['rowId']; 
$columnId = $_SESSION['columnId']; 

$result = mysql_query("UPDATE seats SET updatedby='".$name."' WHERE rowId='".$rowId."' AND columnId='".$columnId."'") 
or die(mysql_error()); 

?> 

如果用戶選擇只有一個座位,該代碼就像一個魅力。但是,當有兩個或兩個以上的時候,$ columnId和$ rowId不會改變,因爲只有最後一個選中的座位的名稱纔會更改。

+0

你說的「名稱」的意思是,一個人的名字?首先,最後/第一和最後?如果是這樣,你將需要一些jQuery來實現這一點。 –

+4

爲什麼選擇jQuery?它不是一切的答案;) – Sir

+0

是的,一個人的名字。第一和最後。 – Jonhz

回答

0

我想你不應該使用jQuery的這部分。嘗試使用PHP的HTML表單(顯示旁邊的輸入旁邊有複選框的位置),然後將數據保存到mysql seatid,seat_userId,seat_name等等......然後只需將數據拉出來,然後用它做任何你想做的事情。

+0

是的,但最棘手的部分是發送到MySQL,因爲每個複選框都有自己的ID,所以輸入區域和複選框必須以某種方式連接。 – Jonhz

+0

只顯示輸入,只有他選擇的座位不是全部,並且您可以從複選框向輸入ID回顯相同的輸入。 – Bogdan

+0

這是一個選項。但實際上,只是出於UI的原因,我想找到一種方法讓用戶在選擇座位後插入名稱。那樣,在他選擇了座位後,下一頁將用輸入框列出每個標記的座位。 – Jonhz