template_dir = $SMARTY['orig']; $tpl->compile_dir = $SMARTY['comp']; // Connect to the database @mysql_connect($db_host, $db_user, $db_password) or die('not connected'); // Select the database $dbc = @mysql_select_db($db_name) or die ('no database selected'); // Switch state switch($_GET['state']) { // Add a new post case "addpost": // If the submit button has been pressed if(isset($_POST['submit'])) { // Check for title if(empty($_POST['title'])) { $error['title'] = 'Required field'; } else { $tpl->assign('title', $_POST['title']); } // Check for a post if(empty($_POST['post'])) { $error['post'] = 'Required field'; } else { $tpl->assign('post', $_POST['post']); } // If there are no errors, insert the blog post into the database if(sizeof($error) == 0) { // Add the post to the database $sql = "INSERT INTO blog ( post_id, title, post, post_date ) VALUES ( null, '{$_POST['title']}', '{$_POST['post']}', NOW() )"; $result = mysql_query($sql); // Redirect user header("Location: index.php?confirmation=addpost"); // Disconnect from database $dbh->disconnect(); exit(); } else { // Assign errors $tpl->assign('error', $error); } } // Display template $tpl->display('addpost.tpl'); break; // Display blog posts default: // Check for a confirmation if($_GET['confirmation'] == 'addpost') { $tpl->assign('confirmation', 'Your post has been added'); } // Select all of the bookmarks in the database (order from newest to oldest) $sql = "SELECT post_id, title, post, DATE_FORMAT(post_date, '%m/%d/%Y') AS formatted_date FROM blog ORDER BY post_date DESC"; $result = mysql_query($sql); while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { // Create a posts array $posts[] = array( 'post_id' => $row['post_id'], 'title' => $row['title'], 'post' => $row['post'], 'post_date' => $row['formatted_date'] ); } // Assign posts $tpl->assign('posts', $posts); // Display template $tpl->display('index.tpl'); break; } ?>