CREATING A DYNAMIC ONLINE GALLERY display a forward link on the page that displays the last subset, because there s nothing to navigate to. Both issues are easily solved by using conditional statements. There s one final thing that you need to take care of. You must also include the value of the current page in the query string generated when you click a thumbnail. If you fail to do so, $curPage is automatically set back to 0, and the first set of thumbnails is displayed instead of the current subset. PHP Solution 12-5: Creating the navigation links Continue working with the same file. Alternatively, use gallery_mysql07.php, gallery_mysqli07.php, or gallery_pdo07.php. 1. I have placed the navigation links in an extra row at the bottom of the thumbnail table. Insert this code between the placeholder comment and the closing
| 0) { echo ‘ . < Prev‘; } // otherwise leave the cell empty else { echo ‘ ’; } ?> |
0) { for ($i = 0; $i < COLS-2; $i++) { echo '
|
‘; } } ?>
. Next >‘; } 337 You want to have a cheap webhost for your apache application, then check apache web hosting services.
Posted in MYSQL5 | No Comments »
May 5th, 2008
PHP SOLUTIONS: DYNAMIC WEB DESIGN MADE EASY Replace it with this:
Displaying
Let s take this line by line. The value of $startRow is zero-based, so you need to add 1 to get a more user-friendly number. So, $startRow+1 displays 1 on the first page and 7 on the second page. In the second line, $startRow+1 is compared with the total number of records. If it s less, that means the current page is displaying a range of records, so the third line displays the text to with a space on either side. You then need to work out the top number of the range, so a nested if… else conditional statement adds the value of the start row to the maximum number of records to be shown on a page. If the result is less than the total number of records, $startRow+SHOWMAX gives you the number of the last record on the page. However, if it s equal to or greater than the total, you display $totalPix instead. Finally, you come out of both conditional statements and display of followed by the total number of records. 7. Save the page and reload it in a browser. You still get only the first subset of thumbnails, but you should see the second number change dynamically whenever you alter the value of SHOWMAX. Check your code, if necessary, against gallery_mysql07.php, gallery_mysqli07.php, or gallery_pdo07.php. Navigating through subsets of records As I mentioned in step 3 of the preceding section, the value of the required page is passed to the PHP script through a query string. When the page first loads, there is no query string, so the value of $curPage is set to 0. Although a query string is generated when you click a thumbnail to display a different image, it includes only the filename of the main image, so the original subset of thumbnails remains unchanged. To display the next subset, you need to create a link that increases the value of $curPage by 1. It follows, therefore, that to return to the previous subset, you need another link that reduces the value of $curPage by 1. That s simple enough, but you also need to make sure that these links are displayed only when there is a valid subset to navigate to. For instance, there s no point in displaying a back link on the first page, because there isn t a previous subset. Similarly, you shouldn t Searching for affordable and reliable webhost to host and run your web applications? Go to our java web server services and you will be pleased.
Posted in MYSQL5 | No Comments »
May 4th, 2008
CREATING A DYNAMIC ONLINE GALLERY 4. You now have all the information that you need to calculate the start row, and to build the SQL query to retrieve a subset of records. Add the following code immediately after the code in the preceding step: // calculate the start row of the subset $startRow = $curPage * SHOWMAX; The original SQL query should now be on the next line. Amend it like this: // prepare SQL to retrieve subset of image details $sql = “SELECT * FROM images LIMIT $startRow,”.SHOWMAX; Notice that I ve used double quotes this time, because I want PHP to process $startRow. Unlike variables, constants aren t processed inside double-quoted strings. So SHOWMAX is added to the end of the SQL query with the concatenation operator (a period). The comma inside the clos- ing quotes is part of the SQL, separating the two arguments of the LIMIT clause. 5. Save the page and reload it into a browser. Instead of eight thumbnails, you should see just six, as shown here: 12 Change the value of SHOWMAX to see a different number of thumbnails. The text above the thumbnail grid doesn t update because it s still hard-coded, so let s fix that. 6. Locate the following line of code in the main body of the page:
Displaying 1 to 6 of 8
335 Please visit our professional web hosting services to find out about cheap and reliable webhost service that will surely answer all your demands.
Posted in MYSQL5 | No Comments »
May 3rd, 2008
PHP SOLUTIONS: DYNAMIC WEB DESIGN MADE EASY 2. You now need to run the new SQL query. The code goes immediately after the code in the preceding step, but differs according to the type of MySQL connection. If you re using the original MySQL extension, add this: // submit query and store result as $totalPix $total = mysql_query($getTotal); $row = mysql_fetch_row($total); $totalPix = $row[0]; This introduces a new function, mysql_fetch_row(), which gets a single record from a result set as an indexed array (one that refers to elements by numbers). The result of SELECT COUNT(*) contains just one field, so you access it as $row[0]. For MySQL Improved, use this: // submit query and store result as $totalPix $total = $conn->query($getTotal); $row = $total->fetch_row(); $totalPix = $row[0]; This uses the MySQLI equivalent of mysql_fetch_row() just described. The result set for the query has been saved as $total, so $total->fetch_row() gets the record as an indexed array. For PDO, use this: // submit query and store result as $totalPix $total = $conn->query($getTotal); $row = $total->fetchColumn(); $totalPix = $row[0]; $total->closeCursor(); This is the same as in the previous chapter, using fetchColumn() to get a single result, and closeCursor() to free the database connection for the next query. 3. Next, set the value of $curPage. The navigation links that you will create later pass the value of the required page through a query string, so you need to check whether curPage has been set in the $_GET array. If it has, use that value. Otherwise, set the current page to 0. Insert the following code immediately after the code in the previous step: // set the current page $curPage = isset($_GET[’curPage’]) ? $_GET[’curPage’] : 0; This uses the conditional operator (see Chapter 3). If you find the conditional operator hard to understand, use the following code instead. It has exactly the same meaning. if (isset($_GET[’curPage’])) { $curPage = $_GET[’curPage’]; } else { $curPage = 0; } From our experience, we are can tell you that you can find a reliable and cheap webhost service at Java Web Hosting services.
Posted in MYSQL5 | No Comments »
May 2nd, 2008
CREATING A DYNAMIC ONLINE GALLERY If $curPage is 0, $startRecord is also 0 (0 6), but when $curPage increases to 1, $startRecord changes to 6 (1 6), and so on. Since there are only eight records in the images table, you need a way of finding out the total number of records to prevent the navigation system from retrieving empty result sets. In the last chapter, you used $numRows to get this information. However, the technique that was used for the original MySQL extension and the MySQL Improved object- oriented interface won t work, because mysql_num_rows() and the num_rows property report the number of records in the current result set. Since you re limiting the number of records retrieved at any one time to a maximum of six, you need a different way to get the total. If you re using PDO, you already know the answer is this: SELECT COUNT(*) FROM images COUNT() is a SQL function that calculates the total number of results in a query. When used like this in combination with an asterisk, it gets the total number of records in the table. So, to build a navigation system, you need to run both SQL queries: one to find the total number of records, and the other to retrieve the required subset. MySQL is fast, so the result is almost instantaneous. I ll deal with the navigation links later. Let s begin by limiting the number of thumbnails on the first page. PHP Solution 12-4: Displaying a subset of records Continue working with the same file. Alternatively, use gallery_mysql06.php, gallery_mysqli06.php, or gallery_pdo06.php. 1. Define SHOWMAX and the SQL query to find the total number of records in the table. Amend the code toward the top of the page like this (new code is shown in bold): // define number of columns in table define(’COLS’, 2); // set maximum number of records per page define(’SHOWMAX’, 6); // create a connection to MySQL $conn = dbConnect(’query’); // prepare SQL to get total records $getTotal = ‘SELECT COUNT(*) FROM images’; Although COLS and SHOWMAX are defined as constants, it doesn t prevent you from offering visitors a choice of how many columns and items to display on a page. You could use variables as the sec- ond arguments to define(), and draw their values from user input. 333 Visit our web design programs services for an affordable and reliable webhost to suit all your needs.
Posted in MYSQL5 | No Comments »
May 2nd, 2008
PHP SOLUTIONS: DYNAMIC WEB DESIGN MADE EASY Paging through a long set of records The grid of eight thumbnails fits quite comfortably in the gallery, but what if you have 28 or 48? The answer is to limit the number of results displayed on each page, and build a navigation system that lets you page back and forth through the results. You ve seen this technique countless times when using a search engine; now you re going to learn how to build it yourself. The task can be broken down into the following two stages: 1. Selecting a subset of records to display 2. Creating the navigation links to page through the subsets Both stages are relatively easy to implement, although it involves applying a little conditional logic. Keep a cool head, and you ll breeze through it. Selecting a subset of records Limiting the number of results on a page is simple. Add the LIMIT keyword to the SQL query like this: SELECT * FROM images LIMIT startPosition, maximum The LIMIT keyword can be followed by one or two numbers. If you use just one number, it sets the maximum number of records to be retrieved. That s useful, but it s not what we need to build a paging system. For that, you need to use two numbers: the first indicates which record to start from, and the second stipulates the maximum number of records to be retrieved. MySQL counts records from 0, so to display the first six images, you need the following SQL: SELECT * FROM images LIMIT 0, 6 To show the next set, the SQL needs to change to this: SELECT * FROM images LIMIT 6, 6 There are only eight records in the images table, but the second number is only a maximum, so this retrieves records 7 and 8. To build the navigation system, you need a way of generating these numbers. The second number never changes, so let s define a constant called SHOWMAX. Generating the first number (call it $startRecord) is pretty easy, too. Start numbering the pages from 0, and multiply the second number by the current page number. So, if you call the current page $curPage, the formula looks like this: $startRecord = $curPage * SHOWMAX; And for the SQL, it becomes this: SELECT * FROM images LIMIT $startRecord, SHOWMAX 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 »
May 1st, 2008
CREATING A DYNAMIC ONLINE GALLERY 2. You need to initialize the cell counter outside the loop, so amend the beginning of the loop like this:
‘; } } while($row); // end of existing loop // new loop to fill in final row while ($pos%COLS) { echo ‘
| |
‘; $pos++; } ?>
A new loop is added at the end of the existing loop. If there are no more records, and $pos%COLS doesn t equal 0, it means you have an incomplete row at the bot tom of the table, so the second loop continues incrementing $pos while $pos%COLS produces a remainder (which is interpreted as true) and inserting an empty cell. Note that this second loop is not nested inside the first. It runs only after the first loop has ended. 4. Save the page and reload it in a browser. The single row of thumbnails across the top of the gallery should now be neatly lined up two by two, as shown to the right. Try changing the value of COLS and reload ing the page. See how easy it is to control 12 the number of cells in each row by chang ing just one number. You can check your code against gallery_mysql06.php, gallery_mysqli06.php, or gallery_pdo06.php. 331 Looking for affordable and reliable webhost to host and run your business application? Then look no more and go to servlet web hosting services.
Posted in MYSQL5 | No Comments »
April 30th, 2008
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.
Posted in MYSQL5 | No Comments »
|