PHP SOLUTIONS: DYNAMIC WEB DESIGN MADE EASY $mainImage and $caption. You ll see later why I ve assigned the values from the first record to separate variables. Amend the code in the table like this:
.
web and email hosting services if you need stable and cheap web hosting platform for your web applications.
Posted in MYSQL5 | No Comments »
Friday, April 25th, 2008
CREATING A DYNAMIC ONLINE GALLERY 4. The code for submitting the query and extracting the first record from the result depends on which method of connection you are using. For the original MySQL functions, use this: // submit the query $result = mysql_query($sql) or die(mysql_error()); // extract the first record as an array $row = mysql_fetch_assoc($result); For MySQL Improved, use this: // submit the query $result = $conn->query($sql) or die(mysqli_error()); // extract the first record as an array $row = $result->fetch_assoc(); For PDO, use this: // submit the query $result = $conn->query($sql); // get any error messages $error = $conn->errorInfo(); if (isset($error[2])) die($error[2]); // extract the first record as an array $row = $result->fetch(); The code for the original MySQL extension and MySQL Improved is exactly the same as you used in the previous chapter. The PDO code, however, introduces a new method, fetch(), which gets the next record from the result set. You can t use a foreach loop like in the previous chapter, because you need to get the first result on its own. 5. All three methods now have the first record from the result set stored as an array in $row. This means that $row[’image_id’] contains the primary key of the first record, $row[’filename’] contains its filename, and $row[’caption’] contains its caption. You need the filename, caption, and the dimensions of the large version so that you can display the images in the main body of the page. Add the following code: // get the name and caption for the main image $mainImage = $row[’filename’]; $caption = $row[’caption’]; // get the dimensions of the main image $imageSize = getimagesize(’images/’.$mainImage); The getimagesize() function was described in Chapters 4 and 8. 6. You can now use this information to display the thumbnail, main image, and its caption dynamically. The main image and thumbnail have the same name, but you eventually want to display all thumbnails by looping through the full result set, so the dynamic code that needs to go in the table cell needs to refer to the current record in other words, $row[’filename’] and $row[’caption’], rather than to 325 Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision J2ee Web Hosting services.
Posted in MYSQL5 | No Comments »
Friday, April 25th, 2008
PHP SOLUTIONS: DYNAMIC WEB DESIGN MADE EASY Figure 12-4. The stripped-down version of the static gallery ready for conversion 2. The gallery depends entirely on a successful connection to the database, so the first thing you need to do is include connection.inc.php. Add the following code just before the closing PHP tag above the DOCTYPE declaration in gallery.php (new code is highlighted in bold): Remember, connection.inc.php needs to be the correct version for the way you plan to connect to MySQL using the original MySQL extension, the MySQL Improved object-oriented interface, or PDO. The include command for the connection script is used as the condition for an if statement. The condition also uses the negative operator (an exclamation mark) and the error control operator (@). If the connection script is included successfully, the code inside the if statement is ignored; but if the file can t be found, a custom error message is displayed, and the rest of the script is abandoned. In a live application, you would probably redirect visitors to a custom error page. 3. Connect to the database by calling the dbConnect() function in the include file, and prepare the SQL query ready to submit it. The gallery needs only SELECT privileges for the database, so pass query as the argument to dbConnect() like this (the code for steps 3 to 5 goes immediately before the closing PHP tag): // create a connection to MySQL $conn = dbConnect(’query’); // prepare SQL to retrieve image details $sql = ‘SELECT * FROM images’; In case you need quality webspace to host and run your web applications, try our personal web hosting services.
Posted in MYSQL5 | No Comments »
Thursday, April 24th, 2008
CREATING A DYNAMIC ONLINE GALLERY elements that need to be converted to PHP code highlighted in bold (you can find the code in gallery01.php in the download files for this chapter):
Images of Japan
Displaying 1 to 6 of 8

Water basin at Ryoanji temple, Kyoto
Converting the gallery elements to PHP Before you can display the contents of the gallery, you need to connect to the phpsolutions database and retrieve all the records stored in the images table. The procedure for doing so is the same as in the previous chapter, using the following simple SQL query: SELECT * FROM images You can then use the first record to display the first image and its associated caption and thumbnail. PHP Solution 12-1: Displaying the first image If you set up the Japan Journey website in Chapter 4, you can work directly with the original gallery.php. Alternatively, use gallery01.php from the download files for this chapter. You also need to copy title.inc.php, menu.inc.php, and footer.inc.php to the includes folder of the phpsolutions site. 1. Load gallery.php into a browser to make sure that it displays correctly. The maincontent part of the page should look like Figure 12-4, with one thumbnail image and a larger version of the same image. 323 If you are in need for cheap and reliable webhost to host your website, we recommend http web server services.
Posted in MYSQL5 | No Comments »
Wednesday, April 23rd, 2008
PHP SOLUTIONS: DYNAMIC WEB DESIGN MADE EASY Figure 12-2. Working out what needs to be done to convert a static gallery to a dynamic one Figure 12-3 shows the framework I created to hold the gallery together. The table of thumbnails and the main_image
are floated left and right respectively in a fixed- width wrapper
called gallery. I don t intend to go into the details of the CSS, but you may study that at your leisure. Figure 12-3. The underlying structure of the image gallery Once I had worked out what needed to be done, I stripped out the code for thumbnails 2 to 6, and for the navigation link (which is nested in the final row of the thumbs table). The following listing shows what was left in the maincontent
of gallery.php, with the We recommend high quality webhost to host and run your jsp application: christian web host services.
Posted in MYSQL5 | No Comments »
Wednesday, April 23rd, 2008
CREATING A DYNAMIC ONLINE GALLERY Why not store images in a database? The images table contains only filenames and captions, but not the images themselves. Even though I said in the last chapter that you can always add new columns or tables to a database when new requirements arise, I m not going to add anything to the images table. Instead, I intend to leave the images in their original location within the website for the simple reason that storing images in a database is usually more trouble than it s worth. The main problems are as follows: Images cannot be indexed or searched without storing textual information separately. Images are usually large, bloating the size of tables. Table fragmentation affects performance if images are deleted frequently. Retrieving images from a database involves passing the image to a separate script, slowing down display in a web page. Storing images in a database is messy, and involves more scripting. It s much more efficient to store images in an ordinary folder on your website and use the database for informa tion about the images. You need just two pieces of information in the database the file name and a caption that can also be used as alt text. You could also store the image s height and width, but it s not absolutely necessary. As you saw in Chapter 4, you can gen erate that information dynamically. Planning the gallery Unless you re good at visualizing how a page will look simply by reading its source code, I find that the best way to design a database-driven site is to start with a static page and fill it with placeholder text and images. I then create my CSS style rules to get the page look ing the way I want, and finally replace each placeholder element with PHP code. Each time I replace something, I check the page in a browser to make sure that everything is still holding together. Figure 12-2 shows the static mockup that I made of the gallery and points out the ele ments that need to be converted to dynamic code. The images are the same as those used for the random image generator in Chapter 4 and are all different sizes. I experimented by scaling the images to create the thumbnails, but decided that the result looked too untidy, so I made the thumbnails a standard size (80 54 pixels). Also, to make life easy, I gave each thumbnail the same name as the larger version and stored them in a separate folder called thumbs. As you saw in the previous chapter, displaying the contents of the entire images table was easy. You created a single table row, with the contents of each field in a separate table cell. By looping through the result set, each record displayed on a row of its own, simulating the column structure of the database table. This time, the two-column structure of the thumbnail grid no longer matches the database structure. This means that you need to count how many thumbnails have been inserted in each row before triggering the creation of the next row. 321 In case you need quality webspace to host and run your web applications, try our personal web hosting services.
Posted in MYSQL5 | No Comments »
|