Learning PHP and MySQL
Chapter 10 Code
Friday, August 18, 2006
Creating myapp/index.php
<?php require_once("smarty.php");
$smarty->assign('test', '123');
$smarty->display('index.tpl');
?>
Posted by krautgrrl on 08/18 at 11:53 AM
Chapter 10 Code • (110) Comments • (894) Trackbacks • Permalink
Chapter 10 Code • (110) Comments • (894) Trackbacks • Permalink
Example 10-01 A simple form example
Put this code in a file named, simple.php
<html>
<head>
<title>Building a Form</title>
</head>
<body>
<form action="<?php echo($_SERVER['PHP_SELF']); ?>"
method="get">
<label>
Search: <input type="text" name="search" />
</label>
<input type="submit" value="Go!" />
</form>
</body>
</html>
Posted by krautgrrl on 08/18 at 10:50 AM
Chapter 10 Code • (0) Comments • (23) Trackbacks • Permalink
Chapter 10 Code • (0) Comments • (23) Trackbacks • Permalink
Example 10-02 Modifying our simple search to process the results
<html>
<head>
<title>Building a Form</title>
</head>
<body>
<?php
$search = $_GET["search"];
$self=$_SERVER['PHP_SELF'];
if ($search != NULL )
{
echo "The search string is: <strong>$search</strong>.";
}
else
{
('
<form action="'.$_SERVER["PHP_SELF"].'" method="GET">
<label>Search: <input type="text" name="search" />
</label>
<input type="submit" value="Go!" />
</form>
');
}
?>
</body>
</html>
The above code generates:
<html>
<head>
<title>Building a Form</title>
</head>
<body>
<form action="/oreilly/ch10/simple.php" method="GET" />
<label> Search: <input type="text" name="search" id="search"> </label>
<input type="submit" value="Go!" />
</form>
</body>
</html>
Posted by krautgrrl on 08/18 at 10:52 AM
Chapter 10 Code • (1) Comments • (0) Trackbacks • Permalink
Chapter 10 Code • (1) Comments • (0) Trackbacks • Permalink
Example 10-03 Form default values
<html>
<head>
<title>Form Default Values</title>
</head>
<body>
<form action="<?php echo($_SERVER['PHP_SELF']); ?>" method="GET" />
<label>Min Price <input type="text" name="min_price" value="0" /></label><br />
<label>Max Price <input type="text" name="max_price" value="1000" /></label>
<br />
<input type="submit" value="Go!" />
</form>
</body>
</html>
Posted by krautgrrl on 08/18 at 10:54 AM
Chapter 10 Code • (0) Comments • (434) Trackbacks • Permalink
Chapter 10 Code • (0) Comments • (434) Trackbacks • Permalink
Example 10-04 Multiple book types
<form>
<select name="media" multiple=”multiple”>
<option></option>
<option>Hard Cover</option>
<option>Soft Cover</option>
<option>Reference</option>
<option>Audio Books</option>
</select>
</form>
Posted by krautgrrl on 08/18 at 11:01 AM
Chapter 10 Code • (1) Comments • (1) Trackbacks • Permalink
Chapter 10 Code • (1) Comments • (1) Trackbacks • Permalink
Example 10-05 A form with checkboxes using the same name to store multiple values
<html>
<head>
<title>Using Default Checkbox Values</title>
</head>
<body>
<?php
$food = $_GET["food"];
if (!empty($food)){
echo "The foods selected are: <strong>";
foreach($food as $foodstuff){
echo '<br />'.$foodstuff;
}
echo "</strong>.";
}
else {
echo ('
<form action="'.$_SERVER["PHP_SELF"].'" method="GET">
<fieldset>
<label> Italian
<input type="checkbox" name="food[]" value="Italian" />
</label>
<label> Mexican
<input type="checkbox" name="food[]" value="Mexican" />
</label>
<label> Chinese
<input type="checkbox" name="food[]" value="Chinese" checked="checked" />
</label>
</fieldset>
<input type="submit" value="Go!" />');
}
?>
</body>
</html>
Posted by krautgrrl on 08/18 at 11:02 AM
Chapter 10 Code • (0) Comments • (0) Trackbacks • Permalink
Chapter 10 Code • (0) Comments • (0) Trackbacks • Permalink
Example 10-06 Checking input from a radio button or a single select
<?php
$options = array('option 1', 'option 2', 'option 3');
// Coming from a checkbox or a multiple select statement
$valid = true;
if (is_array($_GET['input'])) {
$valid = true; foreach($_GET['input'] as $input) { if (!in_array($input, $options)) {
$valid = false;
}
}
if ($valid) {
// process input
}
}
?>
Posted by krautgrrl on 08/18 at 11:05 AM
Chapter 10 Code • (0) Comments • (101) Trackbacks • Permalink
Chapter 10 Code • (0) Comments • (101) Trackbacks • Permalink
Example 10-07 Checking input from a checkbox or a multiple select
<?php
$options = array('option 1', 'option 2', 'option 3');
//coming from a checkbox or a multiple select statement
$valid = true;
if (is_array($_GET['input'])) {
$valid = true;
foreach($_GET['input'] as $input) {
if (!in_array($input, $options)) {
$valid = false;
}
}
if ($valid) {
//process input
}
}
?>
Posted by krautgrrl on 08/18 at 11:06 AM
Chapter 10 Code • (0) Comments • (0) Trackbacks • Permalink
Chapter 10 Code • (0) Comments • (0) Trackbacks • Permalink
Example 10-08 PHP feet-to-meters converter
<head>
<title>Feet to meters conversion</title>
</head>
<body>
<?php
// Check to see if the form has been submitted
$feet = $_GET["feet"];
if ($_GET[feet] != NULL){
echo "<strong>$feet</strong> feet converts to <strong>";
echo $feet * 0.3048;
echo "</strong> meters.<br />";
}
?>
<form action="<?php echo($_SERVER['PHP_SELF']); ?>" method="GET">
<label>Feet:
<input type="text" name="feet" value="<?php echo $feet; ?>" />
</label>
<input type="submit" value="Convert!" />
</form>
</body>
</html>
Posted by krautgrrl on 08/18 at 11:38 AM
Chapter 10 Code • (0) Comments • (0) Trackbacks • Permalink
Chapter 10 Code • (0) Comments • (0) Trackbacks • Permalink
Example 10-09 Converting between time zones based on user input
<html>
<head>
<title>Time Zone Converter</title>
</head>
<body>
<?php
// An array holds the standard time zone strings
$time_zones = array("Asia/Hong_Kong",
"Africa/Cairo",
"Europe/Paris",
"Europe/Madrid",
"Europe/London",
"Asia/Tokyo",
"America/New_York",
"America/Los_Angeles",
"America/Chicago");
// Check to see if the form has been submitted
if ($_GET["start_time"] != NULL){
$start_time_input = $_GET["start_time"];
$start_tz = $_GET["start_tz"];
$end_tz = $_GET["end_tz"];
putenv("TZ=$start_tz");
$start_time = strtotime($start_time_input);
echo "<p><strong>";
echo date("h:i:sA",$start_time)."\n";
echo "</strong>";
putenv("TZ=$end_tz");
echo "in $start_tz becomes ";
echo "<strong> ";
echo date("h:i:sA",$start_time)."\n";
echo "</strong>";
echo " in $end_tz.</p><hr />";
}
?>
<form action="<?php echo($_SERVER['PHP_SELF']); ?>" method="GET">
<label>
Your Time:
<input type="text" name="start_time" value="<?php echo $start_time_input; ?>" />
</label> in
<select name="start_tz">
<?php
foreach ($time_zones as $tz) {
echo '<option';
if (strcmp($tz, $start_tz) == 0){
echo ' selected="selected"';
}
echo ">$tz</option>";
}
?>
</select>
<p>Convert to:
<select name="end_tz">
<?php
foreach ($time_zones as $tz) {
echo '<option';
if (strcmp($tz, $end_tz) == 0){
echo ' selected="selected"';
}
echo ">$tz</option>";
}
?>
</select>
<input type="submit" value="Convert!">
</form>
</body>
</html>
Posted by krautgrrl on 08/18 at 11:39 AM
Chapter 10 Code • (23) Comments • (0) Trackbacks • Permalink
Chapter 10 Code • (23) Comments • (0) Trackbacks • Permalink
Example 10-10 Combining form processing and database querying
?php
function query_db($qstring) {
include('db_login.php');
require_once('DB.php');
$connection = DB::connect("mysql://$db_username:$db_password@$db_host/$db_database");
if (DB::isError($connection)){
die ("Could not connect to the database: <br />". DB::errorMessage($connection));
}
$query = "SELECT * FROM `books` NATURAL JOIN `authors`
WHERE `books`.`title` like '%$qstring%'";
$result = $connection->query($query);
if (DB::isError($result)){
die("Could not query the database:<br />".
$query." ".DB::errorMessage($result));
}
echo ('<table border="1">');
echo "<tr><th>Title</th><th>Author</th><th>Pages</th></tr>";
while ($result_row = $result->fetchRow()) {
echo "<tr><td>";
echo $result_row[1] . '</td><td>';
echo $result_row[4] . '</td><td>';
echo $result_row[2] . '</td></tr>';
}
echo ("</table>");
$connection->disconnect();
}
?>
<html>
<head>
<title>Building a Form</title>
</head>
<body>
<?php
$search = $_GET["search"];
$self = $_SERVER['PHP_SELF'];
if ($search != NULL){
echo "The search string is: <strong>$search</strong>.";
query_db($search);
}
else {
echo ('
<form action="'.$self.'" method="get">
<label>Search:
<input type="text" name="search" id="search" />
</label>
<input type="submit" value="Go!" />
</form>
');
}
?>
</body>
</html>
Posted by krautgrrl on 08/18 at 11:46 AM
Chapter 10 Code • (0) Comments • (0) Trackbacks • Permalink
Chapter 10 Code • (0) Comments • (0) Trackbacks • Permalink
Example 10-11 The index.php file to create
?php
// use the absolute path for Smarty.class.php require($_SERVER["DOCUMENT_ROOT"].'/Smarty/Smarty.class.php');
$smarty = new Smarty();
$smarty->template_dir = $_SERVER["DOCUMENT_ROOT"].'/myapp/smarty/templates';
$smarty->compile_dir = $_SERVER["DOCUMENT_ROOT"].'/myapp/smarty/templates_c';
$smarty->cache_dir = $_SERVER["DOCUMENT_ROOT"].'/myapp/smarty/cache';
$smarty->config_dir = $_SERVER["DOCUMENT_ROOT"].'/myapp/smarty/configs';
?>
Posted by krautgrrl on 08/18 at 11:51 AM
Chapter 10 Code • (0) Comments • (0) Trackbacks • Permalink
Chapter 10 Code • (0) Comments • (0) Trackbacks • Permalink
Example 10-12 The sample index.tpl template to create
<html>
<head>
<title>Smarty</title>
</head>
<body>
It’s as easy as {$test}.
</body>
</html>
Posted by krautgrrl on 08/18 at 11:57 AM
Chapter 10 Code • (0) Comments • (2) Trackbacks • Permalink
Chapter 10 Code • (0) Comments • (2) Trackbacks • Permalink
Example 10-13 Using the template to display the table
<?php
function query_db($qstring){ require_once("smarty.php"); require_once("db_login.php"); require_once("DB.php");
$connection = DB::connect("mysql://$db_username:$db_password@$db_host/$db_database");
if (DB::isError($connection)){
die("Could not connect to the database: <br />". DB::errorMessage($connection));
}
$query = "SELECT * FROM `books` NATURAL JOIN `authors`
WHERE `books`.`title` like '%$qstring%'";
$result = $connection->query($query);
if (DB::isError($result)){
die ("Could not query the database: <br>". $query. " ".DB::errorMessage($result));
}
while ($result_row = $result->fetchRow(DB_FETCHMODE_ASSOC)) {
$test[] = $result_row;
}
$connection->disconnect();
$smarty->assign('users', $test);
$smarty->display('index2.tpl');
}
?>
<html>
<head>
<title>Building a Form</title>
</head>
<body>
<?php
$search = $_GET["search"];
$self = $_SERVER['PHP_SELF'];
if ($search != NULL){
echo "The search string is: <strong>$search</strong>.";
query_db($search);
}
else {
echo '
<form action="'.$self.'" method="GET">
<label> Search:
<input type="text" name="search" id="search" />
</label>
<input type="submit" value="Go!">
</form>';
}
?>
</body>
</html>
Posted by krautgrrl on 08/18 at 12:05 PM
Chapter 10 Code • (0) Comments • (0) Trackbacks • Permalink
Chapter 10 Code • (0) Comments • (0) Trackbacks • Permalink
Example 10-14 The new table template
<table border=1>
<tr><th>Title</th><th>Author</th><th>Pages</th></tr>
{section name=mysec loop=$users}
{strip}
<tr>
<td>{$users[mysec].title}</td>
<td>{$users[mysec].author}</td>
<td>{$users[mysec].pages}</td>
</tr>
{/strip}
{/section}
</table>
Example 10-14 outputs Figure 10-18.
Posted by krautgrrl on 08/18 at 12:07 PM
Chapter 10 Code • (0) Comments • (74) Trackbacks • Permalink
Chapter 10 Code • (0) Comments • (74) Trackbacks • Permalink
Statistics
This page has been viewed 375191 times
Page rendered in 0.2839 seconds
Total Entries: 224
Total Comments: 16
Total Trackbacks: 307338
Most Recent Entry: 09/27/2006 12:39 pm
Most Recent Comment on: 10/26/2007 10:00 am
Total Members: 2
Total Logged in members: 0
Total guests: 11
Total anonymous users: 0
Most Recent Visitor on: 02/10/2012 09:24 am
The most visitors ever was 1103 on 11/20/2007 12:50 pm
