0

I think I have just been working too long and am tired. I have an application using the Zend Framework where I display a list of clubs from a database. I then want the user to be able to click the club and get the id of the club posted to another page to display more info.

Here's the clubs controller:

class ClubsController extends Zend_Controller_Action
{

    public function init()
    {

    }

    public function indexAction()
    {
        $this->view->assign('title', 'Clubs');
        $this->view->headTitle($this->view->title, 'PREPEND');
        $clubs = new Application_Model_DbTable_Clubs();
        $this->view->clubs = $clubs->fetchAll();
    }
}

the model:

class Application_Model_DbTable_Clubs extends Zend_Db_Table_Abstract
{

    protected $_name = 'clubs';

    public function getClub($id) {
        $id = (int) $id;
        $row = $this->fetchRow('id = ' . $id);
        if (!$row) {
            throw new Exception("Count not find row $id");
        }
        return $row->toArray();
    }
}

the view:

<table>
    <?php foreach($this->clubs as $clubs) : ?>
    <tr>
        <td><a href=''><?php echo $this->escape($clubs->club_name);?></a></td>
        <td><?php echo $this->escape($clubs->rating);?></td>
    </tr>
    <?php endforeach; ?>
</table>

I think I am just getting confused on how its done with the zend framework..

3 Answers 3

2

in your view do this

<?php foreach ($this->clubs as $clubs) : ?>
...
<a href="<?php echo $this->url(array(
                    'controller' => 'club-description',
                    'action' => 'index',
                    'club_id' => $clubs->id
                    ));?>">
...

That way you'll have the club_id param available in index action of your ClubDescription controller. You get it like this $this->getRequest()->getParam('club_id')

Sign up to request clarification or add additional context in comments.

6 Comments

Great! I have the clubs id in the url, but what exactly does $this->getRequest()->getParam('club_id') do? as I put it in the indexAction of the ClubDescriptionController and I get nothing. Do I have to create a function to get the clubs information through the id? What would this look like?
getParam('club_id') fetches the value of the GET-var club_id. Using this ID, you can fetch your club information using the find() method of your Zend_Db_Table object: $clubs = new Application_Model_DbTable_Clubs(); and then $row = $clubs->find($club_id)
Ok so I understand the url helper way of doing things and it works. But what I am having trouble understanding is how to get the data from the id in the ClubsDescritpionController. I have this: pastebin.com/V41uxEmv and then if i try and echo the club_name in the view for the controller it doesn't do anything..
in your view refer to $this->clubs not just $clubs. So it should look like this echo $this->escape($this->clubs->club_name);
It still doesn't work.. Also if i put the displayAction in my indexAction of the ClubDescriptionController it breaks the application. So something is wrong there also. ClubsDescriptionController pastebin.com/b3WcgEDD
|
1

An Example:

class ClubsController extends Zend_Controller_Action
{

    public function init()
    {

    }

    public function indexAction()
    {
        $this->view->assign('title', 'Clubs');
        $this->view->headTitle($this->view->title, 'PREPEND');
        $clubs = new Application_Model_DbTable_Clubs();
        $this->view->clubs = $clubs->fetchAll();
    }

   public function displayAction() 
   {
       //get id param from index.phtml (view)
       $id = $this->getRequest()->getParam('id');
       //get model and query by $id
       $clubs = new Application_Model_DbTable_Clubs();
       $club = $clubs->getClub($id);
       //assign data from model to view [EDIT](display.phtml)
       $this->view->club = $club;
       //[EDIT]for debugging and to check what is being returned, will output formatted text to display.phtml
       Zend_debug::dump($club, 'Club Data');

    }
}

[EDIT]display.phtml

<!-- This is where the variable passed in your action shows up, $this->view->club = $club in your action equates directly to $this->club in your display.phtml -->
<?php echo $this->club->dataColumn ?>

the view index.phtml

<table>
    <?php foreach($this->clubs as $clubs) : ?>
    <tr>
    <!-- need to pass a full url /controller/action/param/, escape() removed for clarity -->
    <!-- this method of passing a url is easy to understand -->
        <td><a href='/index/display/id/<?php echo $clubs->id; ?>'><?php echo $clubs->club_name;?></a></td>
        <td><?php echo $clubs->rating;?></td>
    </tr>
<?php endforeach; ?>

an example view using the url() helper

<table>
        <?php foreach($this->clubs as $clubs) : ?>
        <tr>
        <!-- need to pass a full url /controller/action/param/, escape() removed for clarity -->
        <!-- The url helper is more correct and less likely to break as the application changes -->
            <td><a href='<?php echo $this->url(array(
                'controller' => 'index',
                'action' => 'display',
                'id' => $clubs->id
             )); ?>'><?php echo $clubs->club_name;?></a></td>
            <td><?php echo $clubs->rating;?></td>
        </tr>
    <?php endforeach; ?>
</table>

[EDIT] With the way your current getClub() method in your model is built you may need to access the data using $club['data']. This can be corrected by removing the ->toArray() from the returned value.
If you haven't aleady done so you can activate error messages on screen by adding the following line to your .htaccess file SetEnv APPLICATION_ENV development. Using the info you have supplied, make sure display.phtml lives at application\views\scripts\club-description\display.phtml(I'm pretty sure this is correct, ZF handles some camel case names in a funny way)

5 Comments

Ok so I have the clubs page with the clubs listed and then when clicked I get the id on the club description controller page in the address bar. However when I try and get any information from the db it doesnt work... I dont know what I am doing wrong. Here is the clubDescriptionController: pastebin.com/1CPXhbZj If i try: <?php echo $clubs->club_name;?> It doesn't show anything.
This has confused me more.. The ClubsController in my application is to display the clubs with a link to take them to the ClubDescriptionController this is the Controller where I have passed the variable to get the information for that specific club.. I then have this in the index action for that controller -> pastebin.com/ERErZnUb and then this in the view -> echo $this->escape($this->clubs->club_name); Sorry to be a pain I have just started using the ZF and am at the final stages of getting my head round the MVC architecture, if you could make this clearer it would be great! Thnx
Also I added SetEnv APPLICATION_ENV development to my .htaccess file and I still get the same errors from my controller.. I have display errors set in my application.ini file.
ok, so sorry again! But I am one step further.. The Zend_debug::dump($clubs); dumps the array of the club with the id i clicked on to the page.. But if i delete this and then in the view add : <?php echo $this->clubs->club_name ?> I get nothing. <?php echo $club['club_name'] ?> doesn't work either..
in your paste bin code you never pass anything to the view. You need to pass something to the view. Using your example add the line $this->view->clubs = $row; this will make the clubs data available to the view script.
0

You can put the club ID into the URL that you link to as the href in the view - such as /controllername/club/12 and then fetch that information in the controller with:

$clubId = (int) $this->_getParam('club', false);

The 'false' would be a default value, if there was no parameter given. The (int) is a good practice to make sure you get a number back (or 0, if it was some other non-numeric string).

3 Comments

Ok so I have my clubs view: pastebin.com/LBa6YJwm I then have the controller: pastebin.com/k9PWqu2N But I the variable is not being passed..
I have to link like this pastebin.com/bqJQUhpE to even get to the clubDescritpionController, but in this way I don't understand how I would pass the variable?
Use the Url helper as displayed in @namesnik's answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.