0

This is my code for the method to fetch data from the database. I am passing those data as objects to use in html file.

     * Get news details
     * 
     * @return object
     */
    public static function getNews()
    {
        $sql = 'SELECT * FROM news 
                ORDER BY date DESC';

        $db = static::getDB();
        $stmt = $db->prepare($sql);

        $stmt->setFetchMode(PDO::FETCH_CLASS, get_called_class());

        $stmt->execute();

        $news = $stmt->fetch();

        return $news;
    }

Now, this is my rendering function for the index page.. getNews() is method of News class:

     * Show the index page
     *
     * @return void
     */
    public function indexAction()
    {
        $news = new News();
        $news = $news::getNews();

        View::renderTemplate('Home/index.html', [
            'news' => $news
        ]); 
    }

After news object is got.. I wanted to add posts according to descending timestamp. The problem I got is that $news is not taking data of whole table. It only shows data of latest object.

    <div class="row">

                {% for eachnews in news %}

                    <div class="col-4 mt-4">
                        <div class="card" style="width: 18rem;">
                            <img src="/uploads/news/{{ eachnews.fullfilename }}" class="card-img-top" alt="...">
                            <div class="card-body">
                                <h5 class="card-title">{{ eachnews.title }}</h5>
                                <p class="card-text">{{ eachnews.description }}</p>
                                <a href="news" class="btn btn-primary">Read More</a>
                            </div>
                        </div>
                    </div>

                {% endfor %}

            </div>

Every suggestion and answer are highly appreciated. Please ask me, if my problems are not clear.

Arcesilas
  • 1,388
  • 11
  • 24
Biplove Lamichhane
  • 3,995
  • 4
  • 14
  • 30

1 Answers1

1

You are using PDO::fetch(), which only returns one record. You need to use PDO::fetchAll() to fetch... all records.

    public static function getNews()
    {
        $sql = 'SELECT * FROM news 
                ORDER BY date DESC';

        $db = static::getDB();
        $stmt = $db->prepare($sql);

        $stmt->setFetchMode(PDO::FETCH_CLASS, get_called_class());

        $stmt->execute();

        $news = $stmt->fetchAll();

        return $news;
    }

See PDO::fetch() and PDO::fetchAll() documentation pages.

BTW, in this very specific case, you don't need to prepare the statement, since:

  • you don't use it more than once
  • you don't inject user data

Therefore, you may simply use PDO::query():

    public static function getNews()
    {
        $sql = 'SELECT * FROM news 
                ORDER BY date DESC';

        $db = static::getDB();

        return $db->query($sql, PDO::FETCH_CLASS, get_called_class());
    }

Never use this method if you need to inject user input (input that user may modify, like URL argument, form input, etc.). Also, prepared statements have better performances when you need to execute the query multiple times (with different parameters, for example).

Arcesilas
  • 1,388
  • 11
  • 24