Showing only the top 5 results

半世苍凉 提交于 2019-12-11 19:35:28

问题


Sorry, I'm rubbish at php but I am willing to learn.

I'm creating my website found here

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"

I think I need to ad an array of some sort but I'm not sure how to type it.

Please can someone advise me on how?

thank you.


回答1:


fetch() should be changed to fetchAll()

 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->fetchAll(); 

    }
 }

and foreach closing bracket should also be added

<?php foreach ($articles as $article) { ?>

<a href="list.php?id=<?php echo $article['promo_title']; ?>">
<?php echo $article['promo_title']; ?> </a>
 <?php } ?>

And also check the code where you are returning the result set and sending it to the article.php



来源:https://stackoverflow.com/questions/18757396/showing-only-the-top-5-results

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!