mallen Posted February 23, 2017 Share Posted February 23, 2017 function getSimilarProducts() { global $wpdb; $out =""; $Simprod = "SELECT DISTINCT (pr.prod_id), pr.prod_image FROM products as pr LEFT JOIN category_assoc as assoc ON assoc.prod_id = pr.prod_id WHERE pr.companyB = '1' AND assoc.cat_id = '28' AND pr.prod_active = '1' LIMIT 3"; $SIM = $wpdb->get_results($Simprod, ARRAY_A); echo "These are similar products you may like."; foreach($SIM as $sample) { $out .= "<img src='". $this->imgLink . "/".$sample['prod_image']. "' />"; echo $out; } } I have a simple query I tested in phpmyAdmin. Returns the limit results I need. But when I run my function I get duplicates, it returns 6 results all the same ID. Quote Link to comment https://forums.phpfreaks.com/topic/303283-returning-limited-results-in-foreach/ Share on other sites More sharing options...
Solution dalecosp Posted February 23, 2017 Solution Share Posted February 23, 2017 (edited) You're echoing "$out" inside the foreach loop ... and you're also concatenating to it with ".=" ... I think you probably want to put the echo statement AFTER the foreach loop? (That is, after the first bracket and before the bracket that closes your function.) Edited February 23, 2017 by dalecosp Quote Link to comment https://forums.phpfreaks.com/topic/303283-returning-limited-results-in-foreach/#findComment-1543311 Share on other sites More sharing options...
mallen Posted February 23, 2017 Author Share Posted February 23, 2017 Thanks I was looking at it so long i missed that. The concatenating (sp) was because my original code had more to out put. I just had the echo in the wrong place. 1 Quote Link to comment https://forums.phpfreaks.com/topic/303283-returning-limited-results-in-foreach/#findComment-1543313 Share on other sites More sharing options...
dalecosp Posted February 23, 2017 Share Posted February 23, 2017 You're welcome! Good luck with your project :-) Quote Link to comment https://forums.phpfreaks.com/topic/303283-returning-limited-results-in-foreach/#findComment-1543314 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.