Learning PHP and MySQL
Friday, August 18, 2006
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
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>
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>
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.
Chapter 10 Code • (0) Comments • (74) Trackbacks • Permalink
Example 11-01 Displaying a number in binary format
<?php
printf("The computer stores the number 42 internally as %b.",42);
?>
This code then produces the output shown in Figure 11-1.
Chapter 11 Code • (0) Comments • (0) Trackbacks • Permalink
Example 11-02 printf puts the numbers into the string
<?php
printf("The computer stores the numbers 42, and 256 internally as %b and %b.",
42,256);
?>
When called from a web browser, the code in Example 11-2 displays Figure 11-2.
Chapter 11 Code • (0) Comments • (0) Trackbacks • Permalink
Example 11-03 Displaying the same number in different formats
<?php
$value=42; printf("%d<br>",$value);
printf("%b<br>",$value);
printf("%c<br>",$value);
printf("%f<br>",$value);
printf("%o<br>",$value);
printf("%s<br>",$value);
printf("%x<br>",$value);
printf("%X<br>",$value);
?>
The last column of Table 11-1 was generated with the code in Example 11-3.
Example 11-3 gives us this column:
42
101010
*
42.000000
52
42
2a
2A
Chapter 11 Code • (3) Comments • (0) Trackbacks • Permalink
Example 11-04 Using left zero padding
<?php
printf("Zero padding can help alignment %05d.", 42);
?>
Padding with zeros gives us the result shown in Figure 11-3.
Chapter 11 Code • (0) Comments • (0) Trackbacks • Permalink
Example 11-05 Using left space padding
<?php
printf("Space padding can be tricky in HTML % 5d.", 42);
?>
Using the left space padding displays the screen shown in Figure 11-4.
Chapter 11 Code • (0) Comments • (0) Trackbacks • Permalink
Example 11-06 Adding the and tags so the spaces display
<?php
printf("<pre>Space padding can be tricky in HTML % 5d.</pre>", 42);
?>
In Figure 11-5, we correctly see the spaces.
Chapter 11 Code • (0) Comments • (0) Trackbacks • Permalink
Example 11-07 Left padding using the default of spaces
<?php
printf("<pre>Space padding can be tricky in HTML %5d.</pre>", 42);
?>
This code is equivalent to Example 11-5, and produces the same result, shown in Figure 11-6.
Chapter 11 Code • (0) Comments • (0) Trackbacks • Permalink
Example 11-08 Right padding with spaces
<?php
printf("<pre>Space padding can be tricky in HTML %-5d.</pre>", 42);
?>
The output from the negative number in the padding field displays Figure 11-7.
Chapter 11 Code • (0) Comments • (0) Trackbacks • Permalink
Example 11-09 Displaying a real number in money format
<?php
printf("Please pay $%.2f. ", 42.4242);
?>
Our code displays with the dollar sign and decimal correctly, as shown in Figure 11-8.
Chapter 11 Code • (0) Comments • (0) Trackbacks • Permalink
Example 11-10 Using sprintf with a variable
<?php
$total=sprintf("Please pay $%.2f. ", 42.4242);
echo $total;
?>
Figure 11-10 displays the result of this code.
Example 11-11 Calculating the length of a string
<?php
$password="secret1";
if (strlen($password) <= 5)
{
echo("Passwords must be a least 5 characters long.");
}
else {
echo ("Password accepted.");
}
?>
This password code above displays the screen in Figure 11-11.
Chapter 11 Code • (0) Comments • (0) Trackbacks • Permalink
Statistics
This page has been viewed 407272 times
Page rendered in 0.3334 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: 10
Total anonymous users: 0
Most Recent Visitor on: 05/19/2012 05:14 am
The most visitors ever was 1103 on 11/20/2007 12:50 pm
