topical media & game development

talk show tell print

basic-mysql-17-PHPApp-index.php / php



  <?
  
***************************************************************************** Author: Geoff Moes and Robert Sheldon Project: Begining MySQL - Chapter 17 Module: index.php Date: 12/10/04 Description: This is the main listing page for the DVDRentals transaction application. This page allows the user to list the current transactions and select individual transactions for editing and deletion. *****************************************************************************

  
  ?>
  
  <html>
  <head>
     <title>DVD - Listing</title>
     <link rel="stylesheet" href="dvdstyle.css" type="text/css">
     <script language="JavaScript" src="dvdrentals.js"></script>
  </head>
  
  <body>
  
  <form name="mainForm" method="post" action=<index.php>>
  <input type="hidden" name="command" value="view">
  <input type="hidden" name="transaction_id" value="">
  
  <p></p>
  
  <table cellSpacing="0" cellPadding="0" width="619" border="0">
  <tr>
     <td>
        <table height="20" cellSpacing="0" cellPadding="0" width="619" bgcolor="#bed8e1" border="0">
        <tr align=left>
           <td valign="bottom" width="400" class="title">
              DVD Transaction Listing
           </td>
  
           <td align="right" width="219" class="title">
              <input type="button" value="New Transaction" class="add" onclick="doAdd(this)">
           </td>
  
        </tr>
        </table>
        <br>
        <table cellSpacing="2" cellPadding="2" width="619" border="0">
        <tr>
           <td width="250" class="heading">Order Number</td>
           <td width="250" class="heading">Customer</td>
           <td width="250" class="heading">DVDName</td>
           <td width="185" class="heading">DateOut</td>
           <td width="185" class="heading">DateDue</td>
           <td width="185" class="heading">DateIn</td>
  
           <td width="99" class="heading">&nbsp;</td>
  
           <td width="99" class="heading">&nbsp;</td>
  
        </tr>
  
  <?
  // Connect to server or return an error
  link = mysql_connect("localhost", "mysqlapp", "pw1")
     or die("Could not connect: " . mysql_error());
  
  // Select database or return an error
  mysql_select_db("DVDRentals", link)
     or die("Unable to select database: . mysql_error()");
  
  // Initialize variables with parameters retrieved from the form
  command = null;
  transactionId = null;
  
  if(isset(_POST["command"]))
     command = _POST["command"];
  
  if(isset(_POST["transaction_id"]))
     transactionId = _POST["transaction_id"];
  
  // Processes the delete command
  if(transactionId != null)
  {
     if(strcmp("delete", command) == 0)
     {
        include <delete.php>;
     }
  }
  
  // Construct the SQL statement
  selectSql = "SELECT ".
               "Transactions.TransID, ".
               "Transactions.OrderID, ".
               "Transactions.DVDID, ".
               "Transactions.DateOut, ".
               "Transactions.DateDue, ".
               "Transactions.DateIn, ".
               "Customers.CustFN, ".
               "Customers.CustLN, ".
               "DVDs.DVDName ".
               "FROM Transactions, Orders, Customers, DVDs ".
               "WHERE Orders.OrderID = Transactions.OrderID ".
               "AND Customers.CustID = Orders.CustID ".
               "AND DVDs.DVDID = Transactions.DVDID ".
               "ORDER BY  Transactions.OrderID DESC, Customers.CustLN ASC, ".
               "Customers.CustFN ASC, ".
               "Transactions.DateDue DESC, DVDs.DVDName ASC;";
  
  // Execute the SQL query
  result = mysql_query(selectSql, link);
  
  if(!result)
     die("Invalid query: ".mysql_error()."<br>".selectSql);
  
  // Loop through the result set
  while(row = mysql_fetch_array(result))
  {
  // Retrieve the columns from the result set into local variables
     transId = row["TransID"];
     orderId = row["OrderID"];
     dvdId = row["DVDID"];
     dateOut = row["DateOut"];
     dateDue = row["DateDue"];
     dateIn = row["DateIn"];
     custFirstName = row["CustFN"];
     custLastName = row["CustLN"];
     dvdName = row["DVDName"];
  
     // Convert nulls to empty strings and format the data
     customerName = "";
     dateOutPrint = "";
     dateDuePrint = "";
     dateInPrint = "";
  
     if(custFirstName != null)
        customerName .= custFirstName." ";
  
     if(custLastName != null)
        customerName .= custLastName;
  
     if(dvdName == null)
        dvdName = "";
  
     dateFormat = "m-d-Y";
  
     if(dateOut != null)
        dateOutPrint = date(dateFormat, strtotime(dateOut));
  
     if(dateDue != null)
        dateDuePrint = date(dateFormat, strtotime(dateDue));
  
     if(strcmp(dateIn, "0000-00-00") != 0)
        dateInPrint = date(dateFormat, strtotime(dateIn));
  
  // Print each value in each row in the HTML table
  ?>
        <tr height="35" valign="top">
           <td class="item">
              <nobr>
              <?printf(orderId);?>
              </nobr>
           </td>
           <td class="item">
              <nobr>
              <?printf(customerName);?>
              </nobr>
           </td>
           <td class="item">
              <nobr>
              <?printf("dvdName");?>
              </nobr>
           </td>
           <td class="item">
              <nobr>
              <?printf(dateOutPrint);?>
              </nobr>
           </td>
           <td class="item">
              <nobr>
              <?printf(dateDuePrint);?>
              </nobr>
           </td>
           <td class="item">
              <nobr>
              <?printf(dateInPrint);?>
              </nobr>
           </td>
  
           <td class="item" valign="center" align="center">
              <input type="button" value="Edit" class="edit" onclick="doEdit(this, <?printf(transId)?>)">
           </td>
  
           <td class="item" valign="center" align="center">
              <input type="button" value="Delete" class="delete" onclick="doDelete(this, <?printf(transId)?>)">
           </td>
  
        </tr>
  <?
  }
  
  // close Close the database connection and the HTML elements
  
  mysql_close(link);
  ?>
        </table>
     </td>
  </tr>
  </table>
  </body>
  </html>
  
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.