Code continuing from previous parent thread....
//Returns a string of links to navigate the search results.
function searchNavigation($ageId,$priceId,$cat,$pieceId,$awardWinner,$newProduct,$numFound,$numToDisplay,$pagesToDisplay,$curPage){
$numPages = modCount($numFound,$numToDisplay); // determine, from the number of prods and prods/page, the number of pages of search results.
$numSets = modCount($numPages,$pagesToDisplay);//determine, from number of pages and pages/set, the number of sets of pages of search results.
$curSet = (int)($curPage/$pagesToDisplay); // the current set of pages being displayed.
if( $curPage % $pagesToDisplay == 0 ) { //A new set starts at 1, not 0.
$curSet = $curSet -1;
}
$nextSet = ""; //Image Link to first page of next set.
$prevSet = ""; //Image Link to first page of previous set.
$next = ""; //Image Link to next page of results.
$prev = ""; //Image Link to previous page of results.
$pages = ""; //String of text links to particular pages of results in the current set.
if( $curSet > 0 ) { //A previous set of pages exist
$page = ($pagesToDisplay * ($curSet-1)) + 1; //first page of previous set.
$image = "flecheg2.gif";
$prevSet = navImageLink($ageId,$priceId,$cat,$pieceId,$awardWinner,$newProduct,$image,$page);
}
if( $curSet < ($numSets-1) ) { //A next set of pages exist
$page = (($curSet + 1 ) * $pagesToDisplay) + 1; //first page of next set.
$image = "fleched2.gif";
$nextSet = navImageLink($ageId,$priceId,$cat,$pieceId,$awardWinner,$newProduct,$image,$page);
}
if( $curPage > 1 ) { //A previous page exists
$page = $curPage - 1;
$image = "flecheg1.gif";
$prev = navImageLink($ageId,$priceId,$cat,$pieceId,$awardWinner,$newProduct,$image,$page);
}
if( $curPage < $numPages ) { //A next page exists
$page = $curPage + 1;
$image = "fleched1.gif";
$next = navImageLink($ageId,$priceId,$cat,$pieceId,$awardWinner,$newProduct,$image,$page);
}
if ($numPages >= ($pagesToDisplay * ($curSet + 1)) ) { //Current set is at least full
$pagesInSet = $pagesToDisplay; //Display a full set of pages
} else { //Current set is not full
$pagesInSet = $numPages - ($curSet * $pagesToDisplay); // display remainder pages
}
$offset = $pagesToDisplay * $curSet;
echo "curPage = $curPage pagesInSet = $pagesInSet Category = $cat numPages = $numPages, numSets = $numSets curSet = $curSet setOffset = $setOffset ";
for( $i=1; $i<=$pagesInSet; $i++ ) { //loop throught the ages in the set.
$pages .= navLink($ageId,$priceId,$cat,$pieceId,$awardWinner,$newProduct,$curPage,($i+$offset));
}
//Return the navigation links & images.
return $prevSet . $prev . $pages . $next . $nextSet;
}
function navImageLink($ageId,$priceId,$cat,$pieceId,$awardWinner,$newProduct,$image,$page) {
global $PHP_SELF;
$link = "<a href=\"".$PHP_SELF."?cboAge=$ageId&dir=products&cboPrix=$priceId&cat=$cat&cboPiece=$pieceId&AW=$awardWinner&New=$newProduct&iPage=$page\" class=MyNavBar>";
return $link. "<img src=../../images/img_site/client/$image border=0 ></a>";
}
function navLink($ageId,$priceId,$cat,$pieceId,$awardWinner,$newProduct,$curPage,$page) {
global $PHP_SELF;
$link = "<a href=\"".$PHP_SELF."?cboAge=$ageId&dir=products&cboPrix=$priceId&cat=$cat&cboPiece=$pieceId&AW=$awardWinner&New=$newProduct&iPage=$page\" class=MyNavBar>";
if( $page == $curPage ) {
$page = "[ $page ]";
}
return $link . " ".$page . "</a> ";
}
//Returns the number of pages, or sets needed to display all the products or pages found.
function modCount($numFound,$numDisplay) {
$rem = ($numFound % $numDisplay) ? 1:0;
return ((int)($numFound / $numDisplay) + $rem );
}
//This function displays the products found in a 2X2 table.
function writeProducts(&$template,$prodRowTpl,$prodTpl,&$products) {
//Count the number of products found.
$count = is_array($products) ? count($products) : 0;
//Set the Product Row and Product HTML templates.
$template->set_file($prodRowTpl,$prodRowTpl);
$template->set_file($prodTpl,$prodTpl);
if( !$count ) { //If there are no products to display, display "no products message" and return.
$noProductsHTML = "<tr><td align=center height=100 width=130>No product found for this criteria.</td></tr>";
$template->set_var("PROD_ROWS",$noProductsHTML);
return;
} //Otherwise, there exists at least one product to display...
//Determine if there are an even number of products
if( $count % 2 == 0 ) {
$odd = false;
$c = floor($count/3);
} else {
$odd = true;
$c = floor($count/3);
}
$i = 0;
for( $i; $i<$c; $i++ ) { //write an even number of products
writeThreeProducts($products,$i3,$prodTpl,$template);
$template->parse("PROD_ROWS",$prodRowTpl,true);
}
if ($count%3==2) {
writeTwoProducts($products,$i3,$prodTpl,$template);
$template->parse("PROD_ROWS",$prodRowTpl,true);
}
elseif ($count%3==1) {
writeOneProduct($products,$i*3,$prodTpl,$template);
$template->parse("PROD_ROWS",$prodRowTpl,true);
}
}
//Write two products in the template
function writeTwoProducts(&$products,$i,$prodTpl,&$template) {
$p = $products[$i];
writeProduct($p,$template);
$template->parse("PRODUCT",$prodTpl);
$p = $products[++$i];
writeProduct($p,$template);
$template->parse("PRODUCT",$prodTpl,true);
}
function writeThreeProducts(&$products,$i,$prodTpl,&$template) {
$p = $products[$i];
writeProduct($p,$template);
$template->parse("PRODUCT",$prodTpl);
$p = $products[++$i];
writeProduct($p,$template);
$template->parse("PRODUCT",$prodTpl,true);
$p = $products[++$i];
writeProduct($p,$template);
$template->parse("PRODUCT",$prodTpl,true);
}
//Write only one product
function writeOneProduct(&$products,$i,$prodTpl,&$template) {
$p = $products[$i];
writeProduct($p,$template);
$template->parse("PRODUCT",$prodTpl);
}
//Writes the product to the template
function writeProduct(&$prod,&$template) {
//Get the individual data items of the product
list($prodId,$prodName,$prodAge,$prodPieces,$catProd,$prodPrice,$prodSKU,$prodThmImg,$prodPopUpImg,
$awardProd,$newProd,$ageId,$skillId,$piecesId,$priceId) = $prod;
//Fill the product HTML template with the product data.
$template->set_var( array (
"PROD_ID"=>$prodId,
"PROD_NAME"=>$prodName,
"PROD_AGE"=>$prodAge,
"PROD_PIECES"=>$prodPieces,
"PROD_CATEGORY"=>$catProd,
"PROD_PRICE"=>$prodPrice,
"PROD_SKU"=>$prodSKU,
"PROD_IMAGE"=>$prodThmImg,
"PROD_AWARD"=>$awardProd,
"PROD_NEW"=>$newProd,
"PROD_AGE_ID"=>$ageId,
"PROD_SKILL_ID"=>$skillId,
"PROD_PIECES_ID"=>$piecesId,
"PROD_PRICE_ID"=>$priceId ) );
writePopUpImg($prodId,$prodPopUpImg,$template); //write HTML to template for pop up image of template
writeNewText($newProd,$template);//write HTML to template to display "New" text for the product
}
//If a product has a popUp Image, writes a link-javascript function for a popup window with the image.
function writePopUpImg($prodId,$popUp="",&$template) {
$html = "";
if( $popUp != "" ) {
$html = "<a href=\"#\" onClick=\"MM_openBrWindow('showimage.php?iID=$prodId&img=1','pop','width=390,height=320')\">\n".
"<img src=\"../../images/img_site/client/loup.gif\" border=\"0\"></a>";
}
$template->set_var("PROD_IMG_POPUP",$html);
}
//If the product is new, writes in red "New" for new products.
function writeNewText($newProd="",&$template ) {
$html = "";
if( $newProd == "1" ) {
$html = "<font color=\"red\"> New!</font>";
}
$template->set_var("PROD_NEW_TEXT",$html);
}
//This function searches the product table for products matching the given criteria.
function searchProducts(&$db,$ageId,$priceId,$cat,$pieceId,$awardWinner,$newProduct,$pageIndex,$numToDisplay) {
//Query the database for matching products
$prodQuery = productQuery($ageId,$priceId,$cat,$pieceId,$awardWinner,$newProduct,$pageIndex);
$prodQuery->doquery2($db);
$i=0;
$products = array();
while( $p = $prodQuery->getrow() ) {//Loop through the rows in the product table which matched the criteria.
$prod = createProduct($db, $p ); //Creates a product from the given data.
$products[$i++] = $prod; //put product into array.
}
echo " search products: curPage = $curPage pagesInSet = $pagesInSet Category = $cat numPages = $numPages, numSets = $numSets curSet = $curSet setOffset = $setOffset ";
return $products; //return the products found.
}
//Gets product information from other tables and returns a Product.
function createProduct(&$db,$prodRow) {
//data elements in the $prodRow
list($prodId,$newProd,$awardProd,$prodName,$prodThmImg,$catProd,$prodPopUpImg,$prodSKU,$piecesId,$priceId,$ageId) = $prodRow;
//get the pieces
$pieceQuery = piecesQuery($piecesId);
$pieceQuery->doquery2($db);
list($prodPieces) = $pieceQuery->getrow();
//get the price
$priceQuery = priceQuery($priceId);
$priceQuery->doquery2($db);
list($prodPrice) = $priceQuery->getrow();
//get the age
$ageQuery = ageQuery($ageId);
$ageQuery->doquery2($db);
list($prodAge) = $ageQuery->getrow();
echo "create product: curPage = $curPage pagesInSet = $pagesInSet Category = $cat numPages = $numPages, numSets = $numSets curSet = $curSet setOffset = $setOffset ";
//return the Product
return array($prodId,$prodName,$prodAge,$prodPieces,$catProd,$prodPrice,$prodSKU,$prodThmImg,$prodPopUpImg,
$awardProd,$newProd,$ageId,$skillId,$piecesId,$priceId);
}
Due to size limit, code will continue again on next thread.....