Simple Event calendar (Part Two)

Edit calendar entries

The reason for this follow up, is because I was asked how would you go about editing the entries for this calendar.

Well first here is the example:

Going off the original code; we just add a little bit of functionality by doing a few edits, first of is how do we get the record to pull up, with the saved info:

if(!isset($_GET['id']) && !isset($_GET['f'])){
echo '<h3>Events Listed</h3>';

In bold we added some conditions that will decide where we do this, the above will run a loop to bring out all the records from the db concerning that date, same as before but with a small addition, we are telling the server give me all the records, only if the id and the form is not shown.

The second part goes the exact opposite:

}elseif(isset($_GET['id']) && isset($_GET['f'])){

This last part will trigger this piece of code:

$sql="select calID,calName,calDesc, calDate from calTbl";
$sql.=" where calID = '" . $_GET['id'] . "'";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
//other message
$errMess = 'Edit Entry of: ' . $row['calName'];
//show form and edit ability
include 'calForm.php';

See in bold, we are calling one record instead of an array of info that we would loop through and display, this one will give us one record.

We are still calling the calForm.php, we need this to edit the record, there is no need to create a whole new form for this, we just make calForm.php SMARTER.

All we need now to do is edit calForm,php and make it create records and edit them, so in reality it is two forms in one, but we are only concerned with one half at a time.

You accomplish that like so:

<?php if(!isset($_GET['id'])){?>

All that reads is that if we don't show the id of the record, show me the form that we can create a record.

And you can guess what the other half of the statement reads:

<?php }else{?>

Give me the half that will allow me to edit the record.

Now this half is different in respects to the HTML , we have to add a hidden field to hold the id of the record we want to edit:

<input type="hidden" name="id" value="<?=$_GET['id'];?>" />

We are calling it in the URL so we can easily grab it and use it.

Also in the text fields we need to get the values and put them in so we can view what the record is holding:

<input type="text" name="calName" id="calName" value="<?php echo $row['calName'];?>" onKeyup="checkFilled();">

See in bold.

Now we have to post these and make the page smart enough to decide in which direction you want to go INSERT or UPDATE, no sweat.

Back on the main page cal-2.php, we created this snippet to handle that thinking for us:

if(isset($_GET['v'])){
if(isset($_POST['Submit'])){
switch($_GET['e']){
case 1:
$sql="insert into calTbl(calName,calDesc,calDate,calStamp) values('" . $_POST['calName'] ."','";
$sql.= $_POST['calDesc'] . "','" . $_POST['calDate'] . "',now())";
break;

case 2:
$sql="update calTbl set calName='" . $_POST['calName'] . "', calDesc='" . $_POST['calDesc'] . "'";
$sql.=" where calID = '" . $_POST['id'] . "'";
break;
}//end switch

mysql_query($sql);
//echo $sql;
//return;
}

The only difference between the earlier version of the cal and this one is this statement, the switch handles the request which is given to it, from the action on the calForm.php.

If you look at calForm.php at the action for the form, the end will contain either e=1 or e=2.

This will give the switch something to make a choice, 1 will INSERT, 2 will UPDATE.

Of course there are always more elegant ways to accomplish these things, but nothing beats practice on some things, in this writing you got the following.

  1. Extending condtional statements with the double ampersand operator
  2. switch
  3. UPDATE (MySQL)
  4. And how to reuse portions of forms etc, without creating additional files.

I hope that everyone can get something from this and make it better.

Alpho011


More by this Author


Comments 13 comments

psychicdog.net profile image

psychicdog.net 6 years ago

I spent a lot of time on Part 1, Alpho011 finding your scripts very elegant and informative. Thanks.


Alpho011 profile image

Alpho011 6 years ago from Marietta, Georgia Author

Thanks psychicdog.net


spoiledchild 6 years ago

thx a lot!


Alpho011 profile image

Alpho011 6 years ago from Marietta, Georgia Author

UR Welcome


Meadsy 6 years ago

Stunning, really really helpful. Is there anyway to display events by month or week?


Alpho011 profile image

Alpho011 6 years ago from Marietta, Georgia Author

Yeah you could, this would be from a query that would grab a time frame, and you would call the query the same as you would a day:

something like this:

SELECT week BETWEEN 10112010 AND 09122012


Meadsy 6 years ago

OK, so I just need select the first day of that week and set that as the from date then add 7 to it for the time frame. And do that for every week.

I've managed to display all events for the month by using LIKE so I can bypass the $day variable. That is the easy bit (thanks to the code supplied).

I'll let you know who I get on, but I wouldn't hold your breath, I'm quite green behind the ears with php.


Alpho011 profile image

Alpho011 6 years ago from Marietta, Georgia Author

@Meadsy , no worries if you hit a snag, post it, we will work thru it.


pneelam 6 years ago

it is really a nice code

great job

thanks a lot


pravin 6 years ago

can any1 sen me the source file download link?


riceman 5 years ago

I think it is a good calendar script. However I really need the source code - maybe a link to calForm.php.

Your account has expired so I cannot download your solution these days ; )


Kiran 5 years ago

waste scrip and worst script


braum 4 years ago

I tried to search for a working Calform source code...couldnt find one. Would be nice if you reposted it!

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working