PHP
  Home arrow PHP arrow Page 5 - Miles To Go Before I Sleep...
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Miles To Go Before I Sleep...
By: Vikram Vaswani, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 6
    2001-02-05

    Table of Contents:
  • Miles To Go Before I Sleep...
  • Building The Foundation
  • Seven Days, Seven Nights
  • Bringing In The Database
  • Adding, Editing, Deleting...
  • The Final Touch

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Miles To Go Before I Sleep... - Adding, Editing, Deleting...


    (Page 5 of 6 )

    The file "add.php" is actually split into two sections, one to display the initial form and the second to process form data. The $submit variable is used to identify which section to display at a given time.

    <? // form not yet submitted if (!$submit) { // format date $this_date = $currYear . "-" . sprintf("%02d", $currMonth) . "-" . sprintf("%02d", $currDay); ?> <html> <head> </head> <body> <table border="0" cellpadding="2" cellspacing="2"> <form action="<? echo $PHP_SELF; ?>" method="get"> <tr> <td colspan=2 align=center> <font face=Arial size=-1> <b><? echo date("D M d Y", mktime(0,0,0,$currMonth,$currDay,$currYear)); ?></b> </font> </td> </tr> <tr> <td> <font face=Arial size=-1> <i>Time</i> </font> </td> <td> <select name="hh"> <? // drop-downs for the date and time for ($x=0; $x<=23; $x++) { echo "<option value=\"" . sprintf("%02d", $x) . "\">" . sprintf("%02d", $x) . "</option>\n"; } ?> </select> <select name="mm"> <? for ($x=0; $x<=59; $x++) { echo "<option value=\"" . sprintf("%02d", $x) . "\">" . sprintf("%02d", $x) . "</option>\n"; } ?> </select> </td> </tr> <tr> <td> <font face=Arial size=-1> <i>Description</i> </font> </td> <td><input type="Text" name="comment" size="15"></td> </tr> <input type=hidden name=this_date value=<? echo $this_date; ?>> <input type=hidden name=currYear value=<? echo $currYear; ?>> <input type=hidden name=currMonth value=<? echo $currMonth; ?>> <input type=hidden name=currDay value=<? echo $currDay; ?>> <tr> <td colspan=2 align=center> <input type="Submit" name="submit" value="Add Entry"> </td> </tr> </form> </table> </body> </html> <? } else { // form data is processed here } ?>

    There's nothing very challenging here. If the $submit variable doesn't exist, a basic form is displayed with drop-down lists for the appointment time, and a text field for an appointment description. In order to simplify the form processing code, a number of hidden values are also passed with the visible data. PHP code is used to generate the drop-down lists, since it's faster to use a "for" loop than to create sixty <OPTION> tags (remember, you're dealing with an incredibly lazy programmer here!)

    Once the user submits the form, the script is called again, but this time, the second half of the code is executed.


    <? // form not yet submitted if (!$submit) { // form } else { include("config.php"); // format time $this_time = $hh . ":" . $mm . ":00"; // set up default description if ($comment == "") { $comment = "Not available"; } // open a connection to the database $connection = mysql_connect($server, $user, $pass); // formulate the SQL query - same as above $query = "INSERT into calendar VALUES (NULL, '$this_date', '$this_time', '$comment')"; // run the query on the database // assume the database is named "php101" $result = mysql_db_query($db,$query ,$connection); // close connection mysql_close($connection); header("Location: day.view.php?currYear=" . $currYear . "&currMonth=" . $currMonth . "&currDay=" . $currDay); } ?>
    Once the form is submitted, the data entered into it is formatted as per the data structures in the database, a query is generated, and the data is added to the database (note how "config.php" is include()d before connecting to the database to set up the necessary variables).

    In case the comment field is empty, the words "Not available" are used as a default comment string. Finally, the header() function is used to redirect the browser back to the "day view" page, which displays an updated appointment list.

    Here's what the result looks like.



    The files "edit.php" and "delete.php" are similar - the primary difference lies in the query strings.

    <? // from edit.php $query = "UPDATE calendar SET time='$this_time', comment='$comment' WHERE id='$id'"; // from delete.php $query = "DELETE FROM calendar WHERE id='$id'"; ?>

    The "edit.php" script also contains some extra code, used to pre-fill the form with the details of the appointment. If you take a look at the script, you'll see that, before the form is displayed, a query is generated to obtain the appointment time and description, and this information is then used to pre-fill the text box and pre-select the hour and minute from the drop-down lists.



    Here's the code to accomplish this:

    <? // form not submitted if (!$submit) { ?> <html> <head> </head> <body> <? // get data for this appointment to pre-fill form include("config.php"); // open a connection to the database $connection = mysql_connect($server, $user, $pass); // formulate the SQL query - same as above $query = "SELECT * from calendar where id='$id'"; // run the query on the database $result = mysql_db_query($db,$query ,$connection); while($row = mysql_fetch_array($result)) { $this_time = explode(":", $row["time"]); $this_date = $row["date"]; $comment = $row["comment"]; $hh = $this_time[0]; $mm = $this_time[1]; } // close connection mysql_close($connection); ?> <!-- table code - snipped out à <tr> <td><font face=Arial size=-1><i>Time</i></font></td> <td> <select name="hh"> <? // drop-down boxes for hour and time for ($x=0; $x<=23; $x++) { $output = "<option value=\"" . sprintf("%02d", $x) . "\""; // pre-select as per data if (sprintf("%02d", $x) == $hh) { $output .= " selected"; } $output .= ">" . sprintf("%02d", $x) . "</option>\n"; echo $output; } ?> </select> <select name="mm"> <? // and similar code to pre-select the minute ?> </select> <!-- more table code - snipped --> </body> </html> <? } else { // what happens when the form is submitted } ?>


    This article copyright Melonfire 2001. All rights reserved.

    More PHP Articles
    More By Vikram Vaswani, (c) Melonfire


     

       

    PHP ARTICLES

    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT