簡體   English   中英

迭代數組中對象的問題

[英]Problems to iterate objects in array

我下面有這個方法,它負責為我帶來一系列對象(博客)。

public function getListaBlogsById($id) {
    $result = array();
    try {
        $statement = $this->getDb()->prepare('SELECT * FROM Blog WHERE categoria_id = :id');
        $statement->bindParam(':id', $id);
        $statement->execute();
        $result = $statement->fetchAll();
    } catch (Exception $ex) {
        $ex->getTrace();
    }

    return $result;
}

而且我知道該數組不為null。 我想做的就是以這種方式迭代數組中的對象:

<?php foreach ($blog_category as $item): ?>
    <h2>
        <a href="<?php echo Utils::createLink('detail-post', array('id' => $item->getId())); ?>"><?php echo $item->getTitle(); ?></a>
    </h2>
    <p class="lead">
        by <a href="<?php echo Utils::createLink('detail-post', array('id' => $item->getId())); ?>"><?php echo $item->getAuthor(); ?></a>
    </p>
    <p><i class="fa fa-clock-o"></i> Postado em 
        <?php $date = strtotime($item->getDate());
            $br_format = date("d/m/Y g:i A", $date);
            echo $br_format;
        ?></p>
    <hr>
    <a href="<?php echo Utils::createLink('detail-post', array('id' => $item->getId())); ?>">                
        <img class="img-responsive img-hover" src="../web/img/1850_james_ancestry.JPG" alt=""/>
    </a>
    <hr>
    <p><?php echo $item->getPre(); ?></p>
    <a class="btn btn-primary" href="<?php echo Utils::createLink('detail-post', array('id' => $item->getId())); ?>">Leia mais <i class="fa fa-angle-right"></i></a>

    <hr>
<?php endforeach; ?>  

但是頁面沒有任何內容。

任何光將不勝感激。

使用的對象將是從strClass()創建的,因此沒有任何與之關聯的get方法。

因此,請像這樣更改腳本,以便直接訪問這些對象的屬性,而不是期望它們具有關聯的->getxxx()方法。

<a href="<?php echo Utils::createLink('detail-post', array('id' => $item->id)); ?>">
<?php echo $item->title; ?>
</a>

屬性的實際名稱將取決於查詢表上的列名(和列名大小寫)。

您可能還需要在getListaBlogsById($id)函數中更改此行,以確保將數據作為對象而不是數組返回

$result = $statement->fetchAll();

$result = $statement->fetchAll(PDO::FETCH_CLASS);

感謝@RiggsFolly的幫助。 我解決了我的問題,做了一些修改作為休假:

public function getListaBlogsById($id) {
    try {
        $result = array();
        $blog = new Blog();
        $statement = $this->getDb()->prepare('SELECT * FROM Blog WHERE categoria_id = :id');
        $statement->bindParam(':id', $id);
        if ($statement->execute()) {
            while ($row = $statement->fetch()) {
                BlogMapper::map($blog, $row);
                $result[$blog->getId()] = $blog;
            }
        }         

    } catch (Exception $ex) {
        $ex->getTrace();
    }

    return $result;
}

public static function map(Blog $blog, array $properties) {

    if (array_key_exists('id', $properties)) {
        $blog->setId($properties['id']);
    }

    if (array_key_exists('blog_author', $properties)) {
        $blog->setAuthor($properties['blog_author']);
    }

    if (array_key_exists('blog_article_title', $properties)) {
        $blog->setTitle($properties['blog_article_title']);
    }

    if (array_key_exists('blog_date_creation', $properties)) {
        $blog->setDate($properties['blog_date_creation']);
    }

    if (array_key_exists('blog_article_pre', $properties)) {
        $blog->setPre($properties['blog_article_pre']);
    }

    if (array_key_exists('blog_article', $properties)) {
        $blog->setArticle($properties['blog_article']);
    }

    if (array_key_exists('categoria_id', $properties)) {
        $blog->setCategoria_id($properties['categoria_id']);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM