php website programming question:
In this assignment the goal is to first write events into a SQLite3 database and then read these events from a SQLite3 database and display on the calendar the upcoming events.
Description
Your PHP code in the file update_database.php, should process the data a user fills in on the create_event.html page. Then your program calendar2.php should display the events in the correct time slots and for the correct person. The functionality of your programs should be similar to the example on the homework page. For example, consider that we set in the create_event.html the person to be Joe and the date to 5-20-2011 and time is set as 15:00. The user writes the event title as: “Soccer practice” and event message: “Bring the ball”. Then the calendar2.php should show for 5-20-2011 in the Joe column in the 3:00pm slot: “Soccer practice: Bring the ball”. You will also “upgrade” your calendar.php from last week to display at the bottom of the calendar three buttons labeled: “previous”, “now” and “next”. The button labeled “previous” should move the start hour displayed back by 12 hours. The button labeled “next” should move the start hour forward by 12 hours. Finally the button labeled now should display the calendar with the current date and time.
Directions
You will write a php file called update_database.php. This program will receive the form data when the user presses submit on create_event.php. It will then process this data and save the data into a SQLite3 database.
Your program calendar2.php is much like your calendar program from HW5, except now it will also read data from your SQLite3 database. For each slot (person and time) your code should call a function called get_events($person, $timestamp). This function will return a string containing all events that should be displayed in the time slot in question.
Directions for your database
Your database is called dbyourusername.
Your table should be called event_table
Your table should contain field time which is a timestamp it is going to be an integer with perhaps as many as 12 digits.
Your table should contain a field person which has character data. (Reserve some reasonable amouunt of characters for a name.
Your table should contain fields event_title and event_message. Both contain character data max of perhaps 300 characters.
Requirements
Your program should be in two files called calendar2.php and update_database.php those files should reside in your public directory along with CSS files called calendar.css and create_event.css.
When your program is finished, create a text file called calendar2.txt and copy and paste the contents of your calendar2.php file into calendar.txt. Similarly create a text file called update_database.txt. Put calendar.txt and update_database.txt in your public directory as well.
By default when your page is loaded it should display current day, date and time as shown in the example.
Your program must use a function called get_event to get the events for the calendar to display.
Your program must function as in the example given.
You must have the three buttons: previous, today, and next that function in the way described above.
Expected Output:
url: http://www.math.ucla.edu/~virtanen/40a.1.17su/assignments/hw6/create_event.html
url: http://www.math.ucla.edu/~virtanen/40a.1.17su/assignments/hw6/calendar2.php
Simple Event Joe O Joanna O Lil Cub Date should be in format M-D-Y. For example 11-24-2015. Time is 24 hour time in format H:mm. For example 15:00. Date Event title Create event Time Event message
Expert Answer
calendar2.php
———————————————–
#!/usr/local/bin/php
<?php print ‘<?xml version=”1.0″ encoding=”utf-8″ ?>’; ?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN”
“http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>
<html >
<head>
<title>Assignment 6</title>
<link rel=”stylesheet” type=”text/css” href=”calendar.css” />
</head>
<body>
<?php
date_default_timezone_set(‘America/Los_Angeles’);
$database = “dbcalendar2.db”;
try
{
$db = new SQLite3($database);
}
catch (Exception $exception)
{
echo ‘<p>There was an error connecting to the database!</p>’;
if ($db)
{
echo $exception->getMessage();
}
}
function get_hour_string($timestamp)
{
return date(“g.00a”, $timestamp);
}
function get_events($person, $timestamp)
{
global $db;
$table = “event_table”;
$field1 = “name”;
$field2 = “time_stamp”;
$field3 = “event_title”;
$field4 = “event_message”;
$future = $timestamp + 3600;
$result = $db->query(“SELECT $field3, $field4 FROM $table WHERE $field1=’$person’ AND $field2>=$timestamp AND $field2< $future”);
$my_array = array();
while($record = $result->fetchArray())
{
$insert = “$record[$field3]: $record[$field4]<br/>”;
array_push($my_array, $insert);
}
return implode(“”, $my_array);
}
$timestamp = (isset($_GET[‘time_stamp’]))?$_GET[‘time_stamp’]:time();
$hours_to_show = 12;
echo “<div class=’container’>”;
echo “<h1>My Family Schedule for ” . date(“D, F j, Y, g:i a”, $timestamp) . ” </h1>”;
echo “<table id=’event_table’>”;
echo ” <tr>
<th class=’hr_td_’>   </th> <th class=’table_header’>Joe</th><th class=’table_header’>Joanna</th><th class=’table_header’>Lil Cub</th>
</tr>”;
for($i = 0; $i <= $hours_to_show; $i++)
{
if ($i % 2 == 0)
{
echo “<tr class=’even_row’>”;
}
else
{
echo “<tr class=’odd_row’>”;
}
$td_time = $timestamp + $i*60*60;
$row_time = get_hour_string($td_time);
$finding_time = $td_time – ($td_time % 3600);
echo “<td class=’hr_td’>$row_time</td><td>” . get_events(‘Joe’, $finding_time) . “</td><td>” . get_events(‘Joanna’, $finding_time) . “</td><td>” . get_events(‘Lil Cub’, $finding_time) . “</td></tr>”;
}
echo “</table>”;
$increment = 12*60*60;
$future = $timestamp + $increment;
$before = $timestamp – $increment;
echo”<div>
<form id=’prev’ method=’get’ action=’calendar2.php’>
<p>
<input type=’hidden’ name=’time_stamp’ value=’$before’ />
<input type=’submit’ value=’Previous Twelve Hours’/>
</p>
</form>
<form id=’next’ method=’get’ action=’calendar2.php’>
<p>
<input type=’hidden’ name=’time_stamp’ value=’$future’ />
<input type=’submit’ value=’Next Twelve Hours’/>
</p>
</form>
<form id=’today’ method=’get’ action=’calendar2.php’>
<p>
<input type=’submit’ value=’Today’/>
</p>
</form>
</div>”;
echo “</div>”;
?>
</body>
</html>
———————————————————————
calendar.css
—————————–
*
{
margin:0;
padding:0;
}
body
{
background-color:#7BA3BE;
font-family:verdana;
font-size:12px;
}
h1
{
font:18pt verdana;
margin-left:10px;
text-decoration: underline;
font-style: italic;
color:black;
}
#event_table
{
border-collapse:collapse;
border-top:5px;
margin:5px 10px 10px;
}
#event_table .table_header
{
color:#000;
font-size:24px;
font-weight:400;
padding-left:5px;
text-align:left;
text-decoration:none;
width:220px;
font-family: Georgia;
}
#event_table .even_row
{
background-color:#e2a4ff;
height:40px;
}
#event_table .odd_row
{
background-color:#bd66e6;
height:40px;
}
#event_table .hr_td
{
text-align:right;
width:60px;
padding: 5px;
}
.container
{
background-color:#FFF;
border:5px solid #000;
font:13px Verdana, sans-serif;
margin:20px auto;
padding:0;
width:760px;
}
#next
{
float:right;
margin-bottom:10px;
margin-right:10px;
}
#prev
{
float:left;
margin-bottom:10px;
margin-left:10px;
}
#today
{
margin-bottom:10px;
margin-left:auto;
margin-right:auto;
width:100px;
}
input {
border-radius: 2px;
padding: 4px;
}
—————————————————————————
calendar2.txt
——————————————
#!/usr/local/bin/php
<?php print ‘<?xml version=”1.0″ encoding=”utf-8″ ?>’; ?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN”
“http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>
<html >
<head>
<title>Assignment 6</title>
<link rel=”stylesheet” type=”text/css” href=”calendar.css” />
</head>
<body>
<?php
date_default_timezone_set(‘America/Los_Angeles’);
$database = “dbcalendar2.db”;
try
{
$db = new SQLite3($database);
}
catch (Exception $exception)
{
echo ‘<p>There was an error connecting to the database!</p>’;
if ($db)
{
echo $exception->getMessage();
}
}
function get_hour_string($timestamp)
{
return date(“g.00a”, $timestamp);
}
function get_events($person, $timestamp)
{
global $db;
$table = “event_table”;
$field1 = “name”;
$field2 = “time_stamp”;
$field3 = “event_title”;
$field4 = “event_message”;
$future = $timestamp + 3600;
$result = $db->query(“SELECT $field3, $field4 FROM $table WHERE $field1=’$person’ AND $field2>=$timestamp AND $field2< $future”);
$my_array = array();
while($record = $result->fetchArray())
{
$insert = “$record[$field3]: $record[$field4]<br/>”;
array_push($my_array, $insert);
}
return implode(“”, $my_array);
}
$timestamp = (isset($_GET[‘time_stamp’]))?$_GET[‘time_stamp’]:time();
$hours_to_show = 12;
echo “<div class=’container’>”;
echo “<h1>My Family Schedule for ” . date(“D, F j, Y, g:i a”, $timestamp) . ” </h1>”;
echo “<table id=’event_table’>”;
echo ” <tr>
<th class=’hr_td_’>   </th> <th class=’table_header’>Joe</th><th class=’table_header’>Joanna</th><th class=’table_header’>Lil Cub</th>
</tr>”;
for($i = 0; $i <= $hours_to_show; $i++)
{
if ($i % 2 == 0)
{
echo “<tr class=’even_row’>”;
}
else
{
echo “<tr class=’odd_row’>”;
}
$td_time = $timestamp + $i*60*60;
$row_time = get_hour_string($td_time);
$finding_time = $td_time – ($td_time % 3600);
echo “<td class=’hr_td’>$row_time</td><td>” . get_events(‘Joe’, $finding_time) . “</td><td>” . get_events(‘Joanna’, $finding_time) . “</td><td>” . get_events(‘Lil Cub’, $finding_time) . “</td></tr>”;
}
echo “</table>”;
$increment = 12*60*60;
$future = $timestamp + $increment;
$before = $timestamp – $increment;
echo”<div>
<form id=’prev’ method=’get’ action=’calendar2.php’>
<p>
<input type=’hidden’ name=’time_stamp’ value=’$before’ />
<input type=’submit’ value=’Previous Twelve Hours’/>
</p>
</form>
<form id=’next’ method=’get’ action=’calendar2.php’>
<p>
<input type=’hidden’ name=’time_stamp’ value=’$future’ />
<input type=’submit’ value=’Next Twelve Hours’/>
</p>
</form>
<form id=’today’ method=’get’ action=’calendar2.php’>
<p>
<input type=’submit’ value=’Today’/>
</p>
</form>
</div>”;
echo “</div>”;
?>
</body>
</html>
——————————————————————
create_event.html
—————————————–
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN”
“http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>
<html >
<head>
<link rel=”stylesheet” type=”text/css” href=”create_event.css” />
<title>Create Event</title>
</head>
<body>
<div class=”stylized”>
<form class=”stylized” id=”form1″ action=”update_database.php” method=”post”>
<fieldset>
<legend>Simple Event</legend>
<label class=’people’>Joe</label> <input type=’radio’ name=’person’ value=’Joe’ checked=’checked’/>
<label class=’people’>Joanna</label> <input type=’radio’ name=’person’ value=’Joanna’ />
<label class=’people’>Lil Cub</label> <input type=’radio’ name=’person’ value=’Lil Cub’ />
<br/><br/>
<p>Date should be in format M-D-Y. For example 11-24-2015. Time is 24 hour time in format H:mm. For example 15:00.
<table>
<tr>
<td>
<label for=”date”>Date</label>
</td>
<td><input type=”text” name=”date” id=”date” value=””/>
</td>
<td>
<label for=”time”>Time</label></td><td><input type=”text” name=”time” id=”time”/>
</td>
</tr>
<tr>
<td><label for=”event_title”>Event title</label></td>
<td><input class=”stylized” type=”text” id=”event_title” name=”event_title” value=””/>
</td>
<td>
<label for=”event_message”>Event message</label>
</td>
<td><input class=”stylized” type=”text” id=”event_message” name=”event_message” value=””/>
</td>
</tr>
</table>
<input type=”submit” value=”Create event” />
</fieldset>
</form>
</div>
</body>
</html>
———————————————————————-
create_event.css
—————————————
body{
font-family:”Lucida Grande”, “Lucida Sans Unicode”, Verdana, Arial, Helvetica, sans-serif;
background: ffffff;
font-size:12px;
}
p, h1, form, button{border:0; margin:0; padding:0;}
td {padding-left:5px;}
form{
// background:#f1f1f1;
margin:0;
width:500px;
padding:14px;
}
label{
font-weight:normal;
font-size:14px;
}
fieldset {
border:solid 2px #959696;
}
legend{font-size: 20px; color: #0148a0; }
———————————————————————–
update_database.php
—————————————
#!/usr/local/bin/php -d display_errors=STDOUT
<?php
// begin this XHTML page
print(‘<?xml version=”1.0″ encoding=”utf-8″?>’);
print(“n”);
?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN”
“http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>
<html
>
<head>
<meta http-equiv=”content-type” content=”application/xhtml+xml; charset=utf-8″ />
<title>PHP Example: Accessing a SQLite 3 Database using PHP</title>
</head>
<body>
<div>
<?php
date_default_timezone_set(‘America/Los_Angeles’);
$database = “dbcalendar2.db”;
try
{
$db = new SQLite3($database);
}
catch (Exception $exception)
{
echo ‘<p>There was an error connecting to the database!</p>’;
if ($db)
{
echo $exception->getMessage();
}
}
// define tablename and field names for a SQLite3 query to create a table in a database
$table = “event_table”;
$field1 = “name”;
$field2 = “time_stamp”;
$field3 = “event_title”;
$field4 = “event_message”;
// Create the table
$sql= “CREATE TABLE IF NOT EXISTS $table (
$field1 varchar(100),
$field2 int(12),
$field3 varchar(100),
$field4 varchar(100)
)”;
$result = $db->query($sql);
// Write code here to extract the name, SID and GPA from the $_GET data.
$person = $_POST[‘person’];
$date = $_POST[‘date’];
$time = $_POST[‘time’];
$bigdate = explode(‘-‘, $date);
$smalldate = explode(‘:’, $time);
$final_date = mktime($smalldate[0],$smalldate[1],0,$bigdate[0],$bigdate[1],$bigdate[2]);
$event_title = $_POST[‘event_title’];
$event_message = $_POST[‘event_message’];
// Insert a new record to your database with name = $name, sid = $SID and gpa = $GPA
// Create the $sql string that will accomplish this.
$sql = “INSERT INTO $table ($field1, $field2, $field3, $field4) VALUES (‘$person’, $final_date, ‘$event_title’, ‘$event_message’)”;
$result = $db->query($sql);
echo “<h1>Database succesfully updated</h1>
<p><a href=’calendar2.php’>Click here to see the calendar</a></p>”;
$sql = “SELECT * FROM $table”;
$result = $db->query($sql);
print “<table border=’border’>n”;
print ” <tr>n”;
print ” <th>$field1</th>n”;
print ” <th>$field2</th>n”;
print ” <th>$field3</th>n”;
print ” <th>$field4</th>n”;
print ” </tr>n”;
// obtain the results from the SELECT query as an array holding a record
// one iteration per record for this select query
while($record = $result->fetchArray())
{
print ” <tr>n”;
print ” <td>$record[$field1]</td>n”;
print ” <td>$record[$field2]</td>n”;
print ” <td>$record[$field3]</td>n”;
print ” <td>$record[$field4]</td>n”;
print ” </tr>n”;
}
print “</table>n”;
?>
</div>
</body>
</html>
—————————————————————-
update_database.txt
————————————
#!/usr/local/bin/php -d display_errors=STDOUT
<?php
// begin this XHTML page
print(‘<?xml version=”1.0″ encoding=”utf-8″?>’);
print(“n”);
?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN”
“http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>
<html
>
<head>
<meta http-equiv=”content-type” content=”application/xhtml+xml; charset=utf-8″ />
<title>PHP Example: Accessing a SQLite 3 Database using PHP</title>
</head>
<body>
<div>
<?php
date_default_timezone_set(‘America/Los_Angeles’);
$database = “dbcalendar2.db”;
try
{
$db = new SQLite3($database);
}
catch (Exception $exception)
{
echo ‘<p>There was an error connecting to the database!</p>’;
if ($db)
{
echo $exception->getMessage();
}
}
// define tablename and field names for a SQLite3 query to create a table in a database
$table = “event_table”;
$field1 = “name”;
$field2 = “time_stamp”;
$field3 = “event_title”;
$field4 = “event_message”;
// Create the table
$sql= “CREATE TABLE IF NOT EXISTS $table (
$field1 varchar(100),
$field2 int(12),
$field3 varchar(100),
$field4 varchar(100)
)”;
$result = $db->query($sql);
// Write code here to extract the name, SID and GPA from the $_GET data.
$person = $_POST[‘person’];
$date = $_POST[‘date’];
$time = $_POST[‘time’];
$bigdate = explode(‘-‘, $date);
$smalldate = explode(‘:’, $time);
$final_date = mktime($smalldate[0],$smalldate[1],0,$bigdate[0],$bigdate[1],$bigdate[2]);
$event_title = $_POST[‘event_title’];
$event_message = $_POST[‘event_message’];
// Insert a new record to your database with name = $name, sid = $SID and gpa = $GPA
// Create the $sql string that will accomplish this.
$sql = “INSERT INTO $table ($field1, $field2, $field3, $field4) VALUES (‘$person’, $final_date, ‘$event_title’, ‘$event_message’)”;
$result = $db->query($sql);
echo “<h1>Database succesfully updated</h1>
<p><a href=’calendar2.php’>Click here to see the calendar</a></p>”;
$sql = “SELECT * FROM $table”;
$result = $db->query($sql);
print “<table border=’border’>n”;
print ” <tr>n”;
print ” <th>$field1</th>n”;
print ” <th>$field2</th>n”;
print ” <th>$field3</th>n”;
print ” <th>$field4</th>n”;
print ” </tr>n”;
// obtain the results from the SELECT query as an array holding a record
// one iteration per record for this select query
while($record = $result->fetchArray())
{
print ” <tr>n”;
print ” <td>$record[$field1]</td>n”;
print ” <td>$record[$field2]</td>n”;
print ” <td>$record[$field3]</td>n”;
print ” <td>$record[$field4]</td>n”;
print ” </tr>n”;
}
print “</table>n”;
?>
</div>
</body>
</html>