Building a Rating System with Zend

For programming and general questions on Zend Framework
Post Reply
jopojo
Posts: 3
Joined: Thu Mar 11, 2010 8:22 pm

Building a Rating System with Zend

Post by jopojo » Thu Mar 11, 2010 8:25 pm

I am a newbie to Zend and understand the basics, but I cannot get my head around creating a rating system for specific items in a database. I am not looking for code, just an MVC overview on implementing it. Any pointers/help is appreciated.

suggsjc
Posts: 5
Joined: Fri Feb 19, 2010 3:15 pm
Location: Nashville TN
Contact:

Re: Building a Rating System with Zend

Post by suggsjc » Thu Mar 11, 2010 10:17 pm

Can you give a little more detail on what you are looking for? Are you talking about how you would want to route the requests? Or are you wanting advice on things like directory structure what the models would look like, etc.

jopojo
Posts: 3
Joined: Thu Mar 11, 2010 8:22 pm

Re: Building a Rating System with Zend

Post by jopojo » Fri Mar 12, 2010 7:51 pm

My bad. I am not sure how to design a form and capture the results for a star rating system. So, if a user can vote (by selecting a star) on a book, how would that be rendered and then captured?

suggsjc
Posts: 5
Joined: Fri Feb 19, 2010 3:15 pm
Location: Nashville TN
Contact:

Re: Building a Rating System with Zend

Post by suggsjc » Fri Mar 12, 2010 8:52 pm

Ok, well I'll just make some assumptions and then you can fill in the gaps with more specific questions. Also, this is by no means exhaustive or even syntax checked, its just some ideas to think about.

First, we'll say that we create a route to handle the votes

$route = new Zend_Controller_Router_Route(
'item/:item_id/vote',
array(
'controller' => 'items',
'action' => 'vote'
)
);

$router->addRoute('vote', $route);

So now, when you submit to a link that matches that pattern it will be routed to the Items controller and the vote action.

You can either create the form manually or use Zend_Form. Zend_Form is a little quirky and somewhat intimidating when you first start out, but once you master it, it is very powerful and you'll be thankful for learning how to use it. That said, I'm just going to show you the example html for demonstration purposes.

So, if we wanted to submit a vote for an item with item_id = 4027 then the form would look something like this.
<form action="/item/4027/vote">
<select name="vote">
<option value="1">Hate it</option>
...
<option value="5">Love it</option>
</select>
<submit>
</form>

Then if your ItemController you'd have something like this.

public function voteAction()
{
$request = $this->getRequest();

$item_id = $request->getParam('item_id');
$vote = $request->getParam('vote');

// Store the vote in the database
functionToStoreVoteInDatabase($item_id,$vote);
}

Just as a disclaimer, I realize that I've completely skipped over tons of topics and used some horrible conventions (no Service Layers, no authentication or authorization, etc). So take this example with several grains of salt. Let me know if this helped or if it confused you more...

jopojo
Posts: 3
Joined: Thu Mar 11, 2010 8:22 pm

Re: Building a Rating System with Zend

Post by jopojo » Mon Mar 15, 2010 2:09 am

Thanks, that is helpful. I was trying to do this with Zend_Form but it seems easier to implement directly within the phtml.

suggsjc
Posts: 5
Joined: Fri Feb 19, 2010 3:15 pm
Location: Nashville TN
Contact:

Re: Building a Rating System with Zend

Post by suggsjc » Mon Mar 15, 2010 4:38 pm

jopojo wrote:Thanks, that is helpful. I was trying to do this with Zend_Form but it seems easier to implement directly within the phtml.
Don't give up on Zend_Form though. If you learn to master it, it can do a lot more than just displaying the form (validation, filtering, etc).

Check out zendcasts for a good tutorial or two and then take a look at this article on devzone for some of the basics. It takes a little bit to wrap your head around the concept of decorators but once you've got it, it isn't that hard to bend it to your will.

User avatar
clarkphp
Zend Global Services
Posts: 34
Joined: Sat Jun 30, 2012 9:59 pm
Location: United States
Contact:

Re: Building a Rating System with Zend

Post by clarkphp » Tue Sep 24, 2013 2:22 pm

For those who have moved to ZF2, you might take a look at the source code for one of the ZF2 rating systems found on Packagist, to leverage off an existing module, or learn from them to write your own.

Go to https://packagist.org/ and in the search box enter "voting" and also try "rating".

Best wishes,
Clark

Post Reply