Home arrow PHP arrow Page 5 - Miles To Go Before I Sleep...

Adding, Editing, Deleting... - PHP

Ever wished you had an appointment calendar you could accessthrough your Web browser? Well, it's not too late. This week, learn how tobuild a simple PHP-based appointment calendar which allows you to view,add, edit and delete appointments and meetings. And if you're just gettinginto PHP, this article will show you how to apply the theory you've spentso much time learning to a real-world application.

  1. Miles To Go Before I Sleep...
  2. Building The Foundation
  3. Seven Days, Seven Nights
  4. Bringing In The Database
  5. Adding, Editing, Deleting...
  6. The Final Touch
By: Vikram Vaswani, (c) Melonfire
Rating: starstarstarstarstar / 6
February 05, 2001

print this article


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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: