Archive for November, 2007

LIGHTENING YOUR WORKLOAD WITH (Tomcat web server) INCLUDES In other words,

Friday, November 30th, 2007

LIGHTENING YOUR WORKLOAD WITH INCLUDES In other words, if the variable $imageSize hasn t been set (defined), the PHP engine will ignore everything between the curly braces. It doesn t matter that most of the code between the braces is XHTML and CSS. If $imageSize hasn t been set, the PHP engine skips to the closing brace, and the intervening code isn t sent to the browser. Many inexperienced PHP coders wrongly believe that they need to use echo or print to create XHTML output inside a conditional statement. As long as the opening and closing braces match, you can use PHP to hide or display sections of XHTML like this. It s a lot neater and involves a lot less typing than using echo all the time. If $imageSize has been set, the style block is created, and $imageSize[0] is used to set the correct width for the paragraph that contains the caption. 7. Save random_image.php and index.php, and reload index.php into a browser. Click the Reload button until the image of the trainee geisha with the mobile phone appears. This time, it should look like Figure 4-10. If you view the source code in the browser, you will see that the style rule changes automatically for each image. The correct width and height attributes should also be inside the tag. Figure 4-10. The ugly gap is removed by creating a style rule directly related to the image size. 111
If you are in need for cheap and reliable webhost to host your website, we recommend http web server services.

Best web design - PHP SOLUTIONS: DYNAMIC WEB DESIGN MADE EASY The

Thursday, November 29th, 2007

PHP SOLUTIONS: DYNAMIC WEB DESIGN MADE EASY The if statement uses two functions, file_exists() and is_readable(), to make sure $selectedImage not only exists, but also that it s accessible (it may be corrupted or have the wrong permissions). These functions return Boolean values (true or false), so they can be used directly as part of the conditional statement. The single line inside the if statement uses the function getimagesize() to get the image s dimensions. The function returns an array containing four elements. By assigning the result to $imageSize, you can extract the following information: $imageSize[0]: The width of the image in pixels $imageSize[1]: The height of the image in pixels $imageSize[2]: A number indicating the type of file (see Chapter 8 for details) $imageSize[3]: A string containing the height and width for use in an tag The first and last items in this array are just what you need to solve the problem shown in Figure 4-9. 5. First of all, let s fix the code in step 2. Change it like this:

This inserts the correct width and height attributes inside the tag. 6. Although this sets the dimensions for the image, you still need to control the width of the caption. You can t use PHP inside an external stylesheet, but there s nothing stopping you from creating a style block in the of index.php. Put this code just before the closing tag: This code consists of only nine short lines, but there s quite a lot going on in there. Let s start with the first three lines and the final one. If you strip away the PHP tags and replace the middle five lines with a comment, this is what you end up with: if (isset($imageSize)) { // do something if $imageSize has been set }
Looking for affordable and reliable webhost to host and run your business application? Then look no more and go to servlet web hosting services.

LIGHTENING YOUR WORKLOAD WITH (Domain and web hosting) INCLUDES and ‘caption’. The

Thursday, November 29th, 2007

LIGHTENING YOUR WORKLOAD WITH INCLUDES and ‘caption’. The definition of the multidimensional array forms a single state ment, so there are no semicolons until line 19. The closing parenthesis on that line matches the opening one on line 2. All the array elements in between are sepa rated by commas. The deep indenting isn t necessary, but it makes the code a lot easier to read. The variable used to select the image also needs to be changed, because $images[$i] no longer contains a string, but an array. To get the correct filename for the image, you need to use $images[$i][’file’]. The caption for the selected image is contained in $images[$i][’caption’] and stored in a shorter variable. 2. You now need to amend the code in index.php to display the caption like this:

3. Save index.php and random_image.php, and load index.php into a browser. Most images will look fine, but there s an ugly gap to the right of the image of the trainee geisha with a mobile phone, as shown in Figure 4-9. Fortunately, this is easily fixed. Figure 4-9. The long caption shifts the image too far left. 4. Add the following code to random_image.php just before the closing PHP tag: if (file_exists($selectedImage) && is_readable($selectedImage)) { $imageSize = getimagesize($selectedImage); } 109
We recommend high quality webhost to host and run your jsp application: christian web host services.

PHP SOLUTIONS: DYNAMIC WEB DESIGN MADE EASY elements: (Web site hosting)

Wednesday, November 28th, 2007

PHP SOLUTIONS: DYNAMIC WEB DESIGN MADE EASY elements: the filename and a caption. In graphical terms, it looks like this (for space reasons, only the first two items are displayed as arrays): In the original array, $images[1] is the picture of the two trainee geishas. In the multidimensional array, it still represents the same photo, but the filename is now stored in the subarray as $images[1][’file’] and the description as $images[1][’caption’]. Since the images are different sizes, you may be thinking it would be a good idea to store their width and height too. It s not necessary, because PHP can generate the details dynamically with a function called, appropriately enough, getimagesize(). This PHP solution builds on the previous one, so continue working with the same files. 1. Open random_image.php and change the code like this: ‘kinkakuji’, ‘caption’ => ‘The Golden Pavilion in Kyoto’), array(’file’ => ‘maiko’, ‘caption’ => ‘Maiko—trainee geishas in Kyoto’), array(’file’ => ‘maiko_phone’, ‘caption’ => ‘Every maiko should have one—a mobile, . of course’), array(’file’ => ‘monk’, ‘caption’ => ‘Monk begging for alms in Kyoto’), array(’file’ => ‘fountains’, ‘caption’ => ‘Fountains in central Tokyo’), array(’file’ => ‘ryoanji’, ‘caption’ => ‘Autumn leaves at Ryoanji temple, Kyoto’), array(’file’ => ‘menu’, ‘caption’ => ‘Menu outside restaurant in Pontocho, Kyoto’), array(’file’ => ‘basin’, ‘caption’ => ‘Water basin at Ryoanji temple, Kyoto’) ); $i = rand(0, count($images)-1); $selectedImage = “images/{$images[$i][’file’]}.jpg”; $caption = $images[$i][’caption’]; ?> Although the code looks complicated, it s an ordinary indexed array that contains eight items, each of which is an associative array containing definitions for ‘file’
We recommend you use shared web hosting services, because many users agree that it is cheap, reliable and customer-satisfying webhost.

LIGHTENING YOUR (Web hosting ecommerce) WORKLOAD WITH INCLUDES 5. Save both

Tuesday, November 27th, 2007

LIGHTENING YOUR WORKLOAD WITH INCLUDES 5. Save both random_image.php and index.php, and load index.php into a browser. The image should now be chosen at random. Click the Reload button in your browser, and you should see a variety of images, as shown in Figure 4-8. You can check your code for index.php against index04.php in the download files. The code for random_image.php is in random_image01.php. Figure 4-8. Storing image names in an indexed array makes it easy to display a random image. This is a simple and effective way of displaying a random image, but it would be much bet- ter if you could add a caption and set the width and height attributes for different sized images dynamically. PHP Solution 4-7: Adding a caption to the random image As I explained in Chapter 3, arrays can hold any type of data, including other arrays. To store more than one piece of information about an image, each image in the original $images array needs to be represented by a separate array. Each subarray has two 107
From our experience, we are can tell you that you can find a reliable and cheap webhost service at Java Web Hosting services.

PHP SOLUTIONS: DYNAMIC WEB DESIGN (Web site domain) MADE EASY number

Monday, November 26th, 2007

PHP SOLUTIONS: DYNAMIC WEB DESIGN MADE EASY number passed to rand(). It s much easier to get PHP to count them for you, and that s exactly what the count() function does: it counts the number of elements in an array. You need a number one less than the number of elements in the array, so the second argument passed to rand() becomes count($images)-1, and the result is stored in $i. If you re new to PHP, you may find it hard to understand expressions like $i = rand(0, count($images)-1). All that s happening is that you re passing an expression to rand(), rather than the actual number. If it makes it easier for you to follow the logic of the code, rewrite it like this: $numImages = count($images); // $numImages is 8 $max = $numImages 1; // $max is 7 $i = rand(0, $max); // $i = rand(0, 7) The random number is used in the final line to build the correct pathname for the selected file. The variable $images[$i] is embedded in a double-quoted string with no whitespace separating it from surrounding characters, so it s enclosed in curly braces. Arrays start at 0, so if the random number is 1, $selectedImage is images/maiko.jpg. 2. Open index.php and include random_image.php by inserting the command in the same code block as title.inc.php like this: Since random_image.php doesn t send any direct output to the browser, it s quite safe to put it above the DOCTYPE without forcing browsers into quirks mode. 3. Scroll down inside index.php and locate the code that displays the image in the maincontent

. It looks like this:
Water basin at Ryoanji temple

4. Instead of using images/basin.jpg as a fixed image, replace it with $selectedImage. All the images have different dimensions, so delete the width and height attributes, and use a generic alt attribute. The code in step 3 should now look like this:

ftp web hosting services.

LIGHTENING YOUR WORKLOAD (Cheap web hosting) WITH INCLUDES permitted in variable

Monday, November 26th, 2007

LIGHTENING YOUR WORKLOAD WITH INCLUDES permitted in variable names, this is one of the cases where you could omit the curly braces. However, their presence makes the code easier to read. 5. Save footer.inc.php and reload index.php in a browser. Experiment by changing the value of $startYear and alternating between uppercase and lowercase y in the date() function to see the different output, as shown in the following image. These values and the name of the copyright owner are the only things you need to change, and you have a fully automated copyright notice. The finished code for the footer include file is in footer.inc02.php. PHP Solution 4-6: Displaying a random image Displaying a random image is very easy. All you need is a list of available images, which you store in an indexed array (see Creating arrays in Chapter 3). Since indexed arrays are numbered from 0, you can select one of the images by generating a random number between 0 and one less than the length of the array. All accomplished in a few lines of code . . . Continue using the same files. Alternatively, use index03.php from the download files and rename it index.php. Since index03.php uses menu.inc.php, title.inc.php, and footer.inc.php, make sure all three files are in your includes folder. The images are already in the images folder. 1. Create a blank PHP page in the includes folder and name it random_image.php. Insert the following code (it s also in includes/random_image01.php in the download files): This is the complete script: an array of image names minus the .jpg filename extension (there s no need to repeat shared information they re all JPEG), a random number generator, and a string that builds the correct pathname for the selected file. To generate a random number within a range, you pass the minimum and maximum numbers as arguments to the function rand(). Since there are eight images in the array, you need a number between 0 and 7. The simple way to do this would be to use rand(0, 7). Simple, but inefficient . . . Every time you change the $images array, you need to count how many elements it contains and change the maximum 105
You need excellent and relaible webhost company to host your web applications? Then pay a visit to Inexpensive Web Hosting services.

PHP SOLUTIONS: DYNAMIC WEB DESIGN (Windows 2003 server web) MADE EASY Chapter

Sunday, November 25th, 2007

PHP SOLUTIONS: DYNAMIC WEB DESIGN MADE EASY Chapter 14 explains dates in PHP and MySQL in detail, but let s take a quick look at what s happening here. The core part of the code is this line: echo date(’Y'); This displays the year using four digits. Make sure you use an uppercase Y. If you use a lowercase y instead, only the final two digits of the year will be displayed. The reason for the preceding line is because of changes to the way that PHP handles dates. Since PHP 5.1.0, PHP requires a valid time-zone setting. This should be set in php.ini, but if your hosting company forgets to do this, you may end up with ugly error messages in your page. Using ini_set() in a script like this is good insurance against this happening. It also allows you to override the hosting company setting, so this is particularly convenient if your host is in a different time zone from your own. I live in London, so the second argument for ini_set() is ‘Europe/London’. Check the time zone for where you live at www.php.net/manual/en/timezones.php. The date.timezone setting works only in PHP 5.1.0 and above. However, ini_set() silently ignores any settings it doesn t recognize, so you can use this setting safely on older versions of PHP. 3. Save footer.inc.php and load index.php into a browser. The copyright notice at the foot of the page should look the same as before unless, of course, you re reading this in 2007 or later, in which case the current year will be displayed. 4. Copyright notices normally cover a range of years, indicating when a site was first launched. To improve the copyright notice, you need to know two things: the start year and the current year. If both years are the same, you need to display only the current year; if they re different, you need to display both with a hyphen between them. It s a simple if… else situation. Change the code in footer.inc.php like this:

As in PHP Solution 4-4, I ve used curly braces around the variables in line 11 because they re in a double-quoted string that contains no whitespace. Since hyphens aren t
We recommend high quality webhost to host and run your jsp application: christian web host services.

LIGHTENING YOUR WORKLOAD WITH INCLUDES 15. Navigate back (Web server type)

Saturday, November 24th, 2007

LIGHTENING YOUR WORKLOAD WITH INCLUDES 15. Navigate back to contact.php, and you ll see that the page title is still derived cor rectly from the page name. 16. There s one final refinement you should make. The PHP code inside the If $title doesn t exist, the echo command will be ignored, leaving just the default site title, Japan Journey. You can check your code against an updated version of index.php in index03.php in the download files. Creating pages with changing content So far, we ve looked at using PHP to generate different output depending on the page s filename. The next two solutions generate content that changes independently: a copyright notice that updates the year automatically on January 1 and a random image generator. PHP Solution 4-5: Automatically updating a copyright notice Continue working with the files from the previous solution. Alternatively, use index02.php and includes/footer.inc01.php from the download files for this chapter. If using the download files, remove the numbers from the filenames when moving them into your working site. 1. Open footer.inc.php. It contains the following XHTML:

The advantage of using an include file is that you can update the copyright notice throughout the site by changing this one file. However, it would be much more efficient to increment the year automatically, doing away with the need for updates altogether. 2. The PHP date() function takes care of that very neatly. Change the code like this:

103
In case you need quality webspace to host and run your web applications, try our personal web hosting services.

PHP SOLUTIONS: DYNAMIC WEB DESIGN MADE EASY 13. (Msn web hosting)

Saturday, November 24th, 2007

PHP SOLUTIONS: DYNAMIC WEB DESIGN MADE EASY 13. What happens, though, if you have page names that don t make good titles? The home page of the Japan Journey site is called index.php. As the following screen- shot shows, applying the current solution to this page doesn t seem quite right. There are two solutions: either don t apply this technique to such pages or use a conditional statement (an if statement) to handle special cases. For instance, to display Home instead of Index, amend the code in title.inc.php like this: The first line of the conditional statement uses two equal signs to check the value of $title. The following line uses a single equal sign to assign the new value to $title. If the page is called anything other than index.php, the line inside the curly braces is ignored, and $title keeps its original value. PHP is case-sensitive, so this solution works only if index is all lowercase. To do a case-insensitive comparison, change the fourth line of the preceding code like this: if (strtolower($title) == ‘index’) { The function strtolower() converts a string to lowercase hence its name and is frequently used to make case-insensitive comparisons. The conversion to lowercase is not permanent, because strtolower($title) isn t assigned to a variable; it s only used to make the comparison. To make a change permanent, you need to assign the result back to a variable as in the final line, when ucwords($title) is assigned back to $title. To convert a string to uppercase, use strtoupper(). 14. Save title.inc.php and reload index.php into a browser. The page title now looks more natural, as shown in the following screenshot.
If you are in need for cheap and reliable webhost to host your website, we recommend http web server services.