KevHopwood Posted September 11, 2013 Share Posted September 11, 2013 Sorry I'm not the best at PHP but I'm learning. I'm creating my website and am creating a page called more. Within this page I want to add a list of my top 5 promotions on my site. Each promotion is saved in my mobi database in mysql and I have added a count for each time someone views a particular promotion on my single.php page. So basically. What I need to do is display 5 promotions with the highest number in the "views" field in my mobi database. I currently have these codes: include_once('include/connection.php'); include_once('include/article.php'); and <?php foreach ($articles as $article) { ?> <a href="list.php?id=<?php echo $article['promo_title']; ?>"> <?php echo $article['promo_title']; ?> </a> in more.php and also class views { public function fetch_all(){ global $pdo; $query = $pdo->prepare("SELECT * FROM mobi ORDER BY views DESC LIMIT 0, 5"); $query->execute(); return $query->fetch(); } } in articles.php which only loads some weird text of "t t t t h h w w d d d d 3 3" what more will I need to add to get this to work? thank you. Quote Link to comment https://forums.phpfreaks.com/topic/282081-displaying-top-5-items/ Share on other sites More sharing options...
requinix Posted September 11, 2013 Share Posted September 11, 2013 Your fetch_all is only returning one row. It has to return an array of all the rows. Quote Link to comment https://forums.phpfreaks.com/topic/282081-displaying-top-5-items/#findComment-1449156 Share on other sites More sharing options...
KevHopwood Posted September 12, 2013 Author Share Posted September 12, 2013 So please can you show me the type of code I need to add in order to do this? Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/282081-displaying-top-5-items/#findComment-1449190 Share on other sites More sharing options...
Solution vinny42 Posted September 12, 2013 Solution Share Posted September 12, 2013 The easiest solution is probably to replace return $query->fetch(); with return $query->fetchAll(); See: http://php.net/manual/en/pdostatement.fetchall.php Quote Link to comment https://forums.phpfreaks.com/topic/282081-displaying-top-5-items/#findComment-1449193 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.