Ok this is going to sound daft but how to i show 1 record with zend framework, i know how to show many in a foreach but not how to show many.

controller

    public function showAction()
    {
        $this->view->title = "Forum";
        $this->view->headTitle($this->view->title);

    /*
     * Calls the Database table for the Categories.
     */
    $category = new Forum_Model_DbTable_Categories();

    $this->view->category = $category->fetchAll();
}

view file so far but of course its giving me all the records

<p><a href="<?php echo $this->url(array('controller'=>'topics', 'action'=>'add'));?>">Add new Topic</a></p>
<?php foreach($this->category as $category) : ?>
<?php echo $this->escape($category->title);?>
<?php endforeach; ?>

    I don't know Zend but $category is calling the method 'fetchAll()'. Sounds like there should be a method to fetch one record but you'll need to check the manual for that.

    Alternatively you're using a 'foreach' command. You could directly reference the first record in the array and lose the foreach. This isn't elegant but it would be a quick fix.

    echo $this->escape($this->category[0]->title);

      Google'd "zend fetch row" and the top result led to this:

      Zend Framework Documentation wrote:

      Zend_Db_Table_Abstract provides methods find() and fetchAll(), which each return an object of type Zend_Db_Table_Rowset, and the method fetchRow(), which returns an object of type Zend_Db_Table_Row.

        Thanks for that, i should have left it till today but it was getting the best of me last night.

          Write a Reply...