A Practical Example
Let’s take the program sqltest.php from Example 10-8 in Chapter 10 and rewrite it to use Smarty. This will be a two-part process: one part for the program code and one for the Smarty presentation layer. Example 12-3 is the revised program. Once you have typed it in, save it into the temp directory that you created earlier using the filename smartytest.php.
<?php // smartytest.php $path = $_SERVER['DOCUMENT_ROOT']; require "$path/Smarty/Smarty.class.php"; $smarty = new Smarty(); $smarty->template_dir = "$path/temp/smarty/templates"; $smarty->compile_dir = "$path/temp/smarty/templates_c"; $smarty->cache_dir = "$path/temp/smarty/cache"; $smarty->config_dir = "$path/temp/smarty/configs"; require_once("$path/temp/login.php"); $db_server = mysql_connect($db_hostname, $db_username, $db_password); if (!$db_server) die("Unable to connect to MySQL: " . mysql_error()); mysql_select_db($db_database) or die("Unable to select database: " . mysql_error()); if (isset($_POST['author']) && isset($_POST['title']) && isset($_POST['category']) && isset($_POST['year']) && isset($_POST['isbn'])) { $author = get_post('author'); $title = get_post('title'); $category = get_post('category'); $year = get_post('year'); $isbn = get_post('isbn'); if (isset($_POST['delete']) && $isbn != "") { $query = "DELETE FROM classics WHERE isbn='$isbn'"; if (!mysql_query($query)) { echo "DELETE failed: $query<br>" . mysql_error() ...
Get Learning PHP, MySQL, and JavaScript now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.