PHP Date and Time
Using the sources provided you are to add functionality to search for all blogs written before a specific date. The framework for the assignment is set for you
user.html Contains the HTML to prompt for a date
blog_user.php
PHP code to parse the html form and call the function to search by date
blog_db_interface.php
PHP code to perform the search
You will need to
blog_user.php
Convert the date to a unix time stamp using strtotime() 10 points
Verify the strtotime return value. On error throw an exception 10 points
Convert the unix time stamp to a date and time string using date() 10 points
Verify the date functionality. On error throw an exception 10points
If all is OK then call the function to peform the search
blog_db_interface.php
Implement the SQL query to perform the search and display the results
Process errors from calling “prepare”, “execute” and “bind_results”
All errors should result in an exception thrown 10 points
Search result should display “Blogger’s name” “Blog Summary” 10 points
What to turn in (as a single ZIP)
Submit your modified source files: blog_user.php and blog_db_interface.php
blog_db_interface.php
« Go back
‘;
}
function __destruct(){
}
public function blogPostsSummary()
{
$db = @new mysqli(‘localhost’, ‘blogdb_user’, ‘blogdb_userPW’, ‘blogdbx’);
if (mysqli_connect_errno()) {
throw new MySQLI_Exception(” Unable to connect to MYSQL server “, mysqli_connect_errno());
}
// Create a join to pull information from the database
$query = “SELECT blogdb.users.Blogger_Name,”.
” blogdb.post.Post_Summary”.
” from blogdb.users”.
” inner join blogdb.post”.
” on blogdb.users.idUser = blogdb.post.User_idUser1″;
$stmt = $db->prepare($query);
$stmt->execute();
$stmt->bind_result($bname, $summary);
//echo “
Blogs Summary
“;
while($stmt->fetch()) {
echo $bname.” “.$summary.”
“;
}
$stmt->free_result();
$db->close();
}
public function allBloggers()
{
$db = @new mysqli(‘localhost’, ‘blogdb_user’, ‘blogdb_userPW’, ‘blogdb’);
if (mysqli_connect_errno()) {
throw new MySQLI_Exception();
}
// Create a simple query to pull all blogger names from the DB
return;
}
public function blogPostsByBlogger($bname)
{
$db = @new mysqli(‘localhost’, ‘blogdb_user’, ‘blogdb_userPW’, ‘blogdb’);
if (mysqli_connect_errno()) {
throw new MySQLI_Exception();
}
$query = “SELECT blogdb.users.Blogger_Name,”.
” blogdb.post.Post_Summary”.
” from blogdb.users”.
” inner join blogdb.post”.
” on blogdb.users.idUser = blogdb.post.User_idUser1″.
” and blogdb.users.Blogger_Name = ?”;
$stmt = $db->prepare($query);
$stmt->bind_param(‘s’, $bname);
$stmt->execute();
$stmt->bind_result($blogger_name, $bsummary);
// Pull the result into the PHP runtime. copies result data allowing memory to be freed in the
// MYSQL runtime environment
$stmt->store_result(); // Note: $STMT::num_rows is zero without first calling this API
$numBlogs = 0;
echo “
Blogs for : “.$bname.”
“;
while($stmt->fetch()) {
echo $bsummary.”
“;
$numBlogs++;
}
$stmt->free_result();
$db->close();
}
public function insertBlog($bloggerName, $blogSummary, $blogContent)
{
$db = @new mysqli(‘localhost’, ‘blogdb_user’, ‘blogdb_userPW’, ‘blogdb’);
if (mysqli_connect_errno()) {
throw new MySQLI_Exception();
}
// Create a query to get the user ID
// Now add a row to the post table; have to use the ID obtained from the previous query
}
public function searchByDate($dt_stamp)
{
$db = @new mysqli(‘localhost’, ‘blogdb_user’, ‘blogdb_userPW’, ‘blogdb’);
if (mysqli_connect_errno()) {
throw new MySQLI_Exception();
}
// Create a query to search for blogs that were created before the date passed in by $dt_stamp
}
};
?>
blog_exceptions.php
getCode().
“: “.
$this->getMessage().
“
“.
” in “.
$this->getFile().
” on line “.
$this->getLine().
“
“;
}
}
?>
blog_user.php
searchByDate($datetime);
}
else
{
// error handling
}
}
catch( Exception $ge)
{
echo $ge;
}
?>
user.html
Display Bloggers and Blogs
Search for Blogs
Search for Blogs created after this date: