Wednesday, November 30, 2011

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

Your code is quite messy...

Anyways:

<?php
foreach($posts as $post){//loop trough posts
if ($logged_in){//check if the user is logged in
if(!empty($post['Favorite'])){//check if favorites is
empty
$flag = 0;//initialize flag
foreach($post['Favorite'] as $favorite){ //loop trough
favorites
if ($favorite['user_id'] == $current_user['id'])//
if logged in user id is in favorites, increment flag
$flag += 1;
}
echo $favorite = $flag == 0 ? 'BUTTON TO ADD TO
FAVORITES' : 'ITS YOUR FAVORITE';//echo the corresponding link
} else {//Favorites is empty
/*****BUTTON TO ADD TO FAVORITES******/
}
} else {//User is not logged in
// No logged in user. Display link to describe what
favorites are
}
}
?>

Hope this helps...

--
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: