Posted - 02/28/2018 : 02:41:54
I setup up a few functions that I can use across the site. I have a few of these custom php functions simply stored in my includes.php and I call them when needed. A few people in other forums online cautioned against using global too often as it could unintentionally affect other parts of a larger program. My examples seem fairly simple and wondering what your thoughts are from the developers perspective on using this method - or if you have suggestions on a better way to accomplish the same thing. These all work for my purpose, so my question isn't about it working - its more about best practice. Im not a developer by any stretch and sometimes I wonder if I know just enough php to possibly mess something up :)...or if what is in my examples is harmless. This one for example, is a stripped down version of the mini-cart that just pulls the product ids for items that are in the cart. So that I can then run conditionals and have content appear based on if a pId is in the array: function cartprodids() { global $thesessionid; $sSQL = 'SELECT cartProdID FROM cart WHERE cartCompleted=0 AND ' . getsessionsql(); $result=ect_query($sSQL) or ect_error(); while($rs=ect_fetch_assoc($result)){ $array[]=$rs['cartProdID']; } ect_free_result($result); return $array; }
This one gets the number of items in the cart. Using this to populate the number of cart items next to the cart link... function cartquantity() { global $thesessionid; if(trim(@$_POST['sessionid'])!='') $thesessionid = trim(@$_POST['sessionid']); else $thesessionid = getsessionid(); $totquant=0; $sSQL = 'SELECT cartID,cartProdID,cartProdName,cartProdPrice,cartQuantity FROM cart WHERE cartCompleted=0 AND ' . getsessionsql(); $result=ect_query($sSQL) or ect_error(); while($rs=ect_fetch_assoc($result)){ $optPriceDiff=0; $mcpdtxt.='<div class="minicartcnt">' . $rs['cartQuantity'] . ' ' . $rs['cartProdName'] . '</div>'; $sSQL = 'SELECT SUM(coPriceDiff) AS sumDiff FROM cartoptions WHERE coCartID=' . $rs['cartID']; $result2=ect_query($sSQL) or ect_error(); $rs2=ect_fetch_assoc($result2); if(! is_null($rs2['sumDiff'])) $optPriceDiff=$rs2['sumDiff']; ect_free_result($result2); $subtot = (($rs['cartProdPrice']+$optPriceDiff)*(int)$rs['cartQuantity']); $totquant+=(int)$rs['cartQuantity']; $mcgndtot+=$subtot; } return $totquant; }
This one I am using in conjunction with a modification to the incproductbody2 page to add the prodid and category as css classes for styling purposes. To have a little more granular control over the CSS of each item on the products page... function addcss2products(){ global $rs; $productpID=$rs['pId']; $productcat=$rs['pSection']; return $productpID." cat-".$productcat; }
Pretty straigt forward. Returns the pSection... function productspSection(){ global $rs; return $rs['pSection']; }
|