PHP SOLUTIONS: DYNAMIC WEB (Web server version) DESIGN MADE EASY 8.

PHP SOLUTIONS: DYNAMIC WEB DESIGN MADE EASY 8. Save the page and reload it in your browser. This time, when you click a thumbnail, the main image and caption will change. Check your code, if necessary, against gallery_mysql05.php, gallery_mysqli05.php, or gallery_pdo05.php. Passing information through a query string like this is an important aspect of working with PHP and database results. Although form information is normally passed through the $_POST array, the $_GET array is frequently used to pass details of a record that you want to display, update, or delete. Like the $_POST array, the $_GET array automatically inserts backslashes if magic quotes are turned on in php.ini. Since only the filename is being passed through the query string, there s no need to use the nukeMagicQuotes() function from Chapter 3 because quotes are illegal in filenames. That s one reason I didn t pass the caption through the query string. Getting it directly from the database avoids the problem of handling backslashes. Creating a multicolumn table With only eight images, the single row of thumbnails across the top of the gallery doesn t look too bad. However, it s useful to be able to build a table dynamically with a loop that inserts a specific number of table cells in a row before moving to the next row. You do this by keeping count of how many cells have been inserted. When you get to the limit for the row, check whether any more rows are needed. If so, insert a closing tag for the current row and an opening tag for the next one. What makes it easy to implement is the modulo operator, %, which returns the remainder of a division. This is how it works. Let s say you want two cells in each row. When the first cell is inserted, the counter is set to 1. If you divide 1 by 2 with the modulo operator (1%2), the result is 1. When the next cell is inserted, the counter is increased to 2. The result of 2%2 is 0. The next cell produces this calculation: 3%2, which results in 1; but the fourth cell produces 4%2, which is again 0. So, every time that the calculation results in 0, you know or to be more exact, PHP knows you re at the end of a row. So how do you know if there are any more rows left? Each time you iterate through the loop, you extract the next record into an array called $row. By using is_array(), you can check whether $row contains the next result. If it does, you add the tags for the next row. If is_array($row) is false, you ve run out of records in the result set. Phew . . . let s try it. PHP Solution 12-3: Looping horizontally and vertically Continue working with the files from the preceding section. Alternatively, use gallery_mysql05.php, gallery_mysqli05.php, or gallery_pdo05.php. 1. You may decide at a later stage that you want to change the number of columns in the table, so it s a good idea to create a constant at the top of the script, where it s easy to find, rather than burying the figures deep in your code. Insert the following code just before the database connection: // define number of columns in table define(’COLS’, 2);
From our experience, we are can tell you that you can find a reliable and cheap webhost service at Java Web Hosting services.

Leave a Reply