Prev | Current Page 230 | Next

Emilian Balanescu and Cristian Darie

"Beginning PHP and MySQL E-Commerce: From Novice to Professional, Second Edition"

php.
To improve the performance when a visitor browses back and forth through the subpages,
after we calculate the number of subpages for the first time, we??™re saving it to the visitor??™s session.
This way, the SQL query received as parameter won??™t need to be executed more than once
on a single visit to a catalog page.
This method is called from the other data tier methods (GetProductsInCategory(),
GetProductsOnDepartment(), GetProductsOnCatalog()), which we??™ll cover next.
Add HowManyPages() to the Catalog class:
/* Calculates how many pages of products could be filled by the
number of products returned by the $countSql query */
private static function HowManyPages($countSql, $countSqlParams)
{
// Create a hash for the sql query
$queryHashCode = md5($countSql . var_export($countSqlParams, true));
// Verify if we have the query results in cache
if (isset ($_SESSION['last_count_hash']) &&
isset ($_SESSION['how_many_pages']) &&
$_SESSION['last_count_hash'] === $queryHashCode)
{
// Retrieve the the cached value
$how_many_pages = $_SESSION['how_many_pages'];
}
else
{
// Execute the query
$items_count = DatabaseHandler::GetOne($countSql, $countSqlParams);
CHAPTER 5 ?–  CREATING THE PRODUCT CATALOG: PART 2 141
// Calculate the number of pages
$how_many_pages = ceil($items_count / PRODUCTS_PER_PAGE);
// Save the query and its count result in the session
$_SESSION['last_count_hash'] = $queryHashCode;
$_SESSION['how_many_pages'] = $how_many_pages;
}
// Return the number of pages
return $how_many_pages;
}
Let??™s analyze the function to see how it does its job.


Pages:
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
pożyczka hipoteczna parasole reklamowe reklama bukmacher wizualizacje