Learning PHP and MySQL
Friday, August 18, 2006
Creating myapp/index.php
<?php require_once("smarty.php");
$smarty->assign('test', '123');
$smarty->display('index.tpl');
?>
Chapter 10 Code • (110) Comments • (894) Trackbacks • Permalink
Example 05-09 Using include_once to include a file
<?php
include_once('add.php');
include_once('add.php');
echo add(2, 2);
?>
Example 07-01 Creating the books and authors tables
CREATE TABLE books ( title_id INT NOT NULL AUTO_INCREMENT,
title VARCHAR (150),
pages INT,
PRIMARY KEY (title_id));
CREATE TABLE authors ( author_id INT NOT NULL AUTO_INCREMENT,
title_id INT NOT NULL,
author VARCHAR (125),
PRIMARY KEY (author_id));
Chapter 7 Code • (0) Comments • (27) Trackbacks • Permalink
Example 07-02 The SQL to create and populate a purchases table that links user_ids and title_ids
This code is for:
The SQL to create and populate a purchases table that links user_ids and title_ids to a purchase_id
CREATE TABLE `purchases` ( purchase_id int(11) NOT NULL auto_increment,
user_id varchar(10) NOT NULL,
title_id int(11) NOT NULL,
purchased timestamp NOT NULL default CURRENT_TIMESTAMP,
PRIMARY KEY (purchase_id));
INSERT INTO `purchases` VALUES (1, 'mdavis', 2, '2005-11-26 17:04:29');
INSERT INTO `purchases` VALUES (2, 'mdavis', 1, '2005-11-26 17:05:58');
Chapter 7 Code • (5) Comments • (391) 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>
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>
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>
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>
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>
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
}
}
?>
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
}
}
?>
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>
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>
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>
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';
?>
Chapter 10 Code • (0) Comments • (0) Trackbacks • Permalink
Statistics
This page has been viewed 375215 times
Page rendered in 0.3357 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: 8
Total anonymous users: 0
Most Recent Visitor on: 02/10/2012 09:45 am
The most visitors ever was 1103 on 11/20/2007 12:50 pm
