JordanSmith Posted March 3, 2012 Share Posted March 3, 2012 Hi Guys, I have a script that shows the user of my shop what they have looked at. I was wondering if someone can tell me how I can limit this so it only lists 10 and no more. Here is the code: if(RECENTVIEWED ==1) { if(isset($_COOKIE['jimbeam'])){ $productUrl = SITE_URL."ecom/index.php?action=ecom.pdetails&mode="; $productUrl = $this->libFunc->m_safeUrl($productUrl); foreach ($_COOKIE['jimbeam'] as $name => $value) { $this->obDb->query="SELECT vTitle FROM ".PRODUCTS." WHERE vSeoTitle = '".$value."'"; $rsProd = $this->obDb->fetchQuery(); $this->obTpl->set_var("TPL_VAR_PRODUCTTITLE",stripslashes($rsProd[0]->vTitle)); $this->obTpl->set_var("TPL_VAR_PRODUCTURL",$productUrl.$value); $this->obTpl->parse("recent_blk","TPL_RECENT_BLK",true); } $this->obTpl->parse("mainrecent_blk","TPL_MAINRECENT_BLK"); } } Thanks, Jordan Quote Link to comment https://forums.phpfreaks.com/topic/258187-recently-viewed-items/ Share on other sites More sharing options...
scootstah Posted March 3, 2012 Share Posted March 3, 2012 $this->obDb->query="SELECT vTitle FROM ".PRODUCTS." WHERE vSeoTitle = '".$value."' LIMIT 10"; Quote Link to comment https://forums.phpfreaks.com/topic/258187-recently-viewed-items/#findComment-1323474 Share on other sites More sharing options...
JordanSmith Posted March 3, 2012 Author Share Posted March 3, 2012 Hi, Thanks for getting back to me, I have updated the code but it doesnt seem to limit it, it goes past 10. Is there another way we can limit this please? Thanks, Jordan Quote Link to comment https://forums.phpfreaks.com/topic/258187-recently-viewed-items/#findComment-1323481 Share on other sites More sharing options...
scootstah Posted March 3, 2012 Share Posted March 3, 2012 Whoops, didn't notice it was in a foreach loop. Which by the way is very inefficient design. You should be able to use this code to run the same query outside of the foreach loop, so you have one query instead of 10. $cookie = array_slice($_COOKIE['jimbean'], 0, 10); $where = array(); foreach($cookie as $c) { $where[] = "'$c'"; } $where = implode(', ', $where); $this->obDb->query="SELECT vTitle FROM ".PRODUCTS." WHERE vSeoTitle IN($where) LIMIT 10"; Also, depending on how your database class works this may be susceptible to SQL injection. Quote Link to comment https://forums.phpfreaks.com/topic/258187-recently-viewed-items/#findComment-1323486 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.