CREATING A DYNAMIC ONLINE GALLERY If $curPage is (Top web site)
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.