Wednesday, November 30, 2011

Re: How to show different text in a view depending on the has_many association

I'd do some parsing in the model before sending the data to the view. Not sure how your data is set up, but my instinct would be to not include the favourites in the find, then find them separately. Then either parse over the favourites array or use Set::extract, then set a new field in the Post key to false by default and set it to true where appropriate. Then in you view you have a true/false flag right where you need it without any logic.

Jeremy Burns

On 1 Dec 2011, at 00:04, andrewperk wrote:

Hello, I'm having a hard time displaying different text in a view
depending on if a user who is logged in has marked the post as a
favorite or not.

Users can have many posts, posts belong to users.
Users can have many favorites, favorites belong to users and posts.

In my index view I loop through all posts and display the post.

Inside that I loop, I then loop through all the favorites of that
post. Remember sometimes a post may not have any favorites so the
$post['Favorite] array might be empty. I want to display different
text if the post is a favorite of the logged in user. I do this by
comparing the current favorite's user_id to the logged in user's id.

Where I run into problems is when a post is marked as a favorite by 2
or more users. When this happens the Favorite array for the post
associations contains multiple users and thus I end up with both of my
different texts being displayed instead of just one or the other.

So if the post is a favorite I want to tell the user its already their
favorite.

If the post is not a favorite, I want to display a button so they can
add it as a favorite.

And if they are not logged in, they get a link describing to them what
favorites are.

Here's my view code:


*******************************

<?php foreach ($posts as $post): ?>
<?php
   // Check for logged in user
   if ($logged_in): ?>
      // Loop through the posts favorites
       <?php foreach($post['Favorite'] as $favorite): ?>
             <?php
                    // If favorite matches user id tell them its a
favorite
    if ($favorite['user_id'] == $current_user['id']): ?>
           ***** ITS YOUR FAVORITE!!!! *****
           <?php
           else: ?>
               *** BUTTON TO ADD TO FAVORITES ***
           <?php endif; ?>
      <?php endforeach; ?>
<?php
// Favorites array might be empty
if (empty($post['Favorite'])): ?>
  *** BUTTON TO ADD TO FAVORITES ***
<?php endif; ?>
<?php else: ?>
   // No logged in user. Display link to describe what favorites are
   *** LINK Favorites Description ***
<?php endif; ?>

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: