**Date Model (These are the options)** class Model_Date extends \Orm\Model { protected static $_belongs_to = array('outing'); protected static $_has_many = array('responses'); protected static $_properties = array( 'id', 'outing_id', 'date', 'created_at', 'updated_at' );**Response Model (This is what the user adds to for their choice, 'date' is 1 or 0.**
class Model_Response extends \Orm\Model { protected static $_belongs_to = array('date', 'user'); protected static $_properties = array( 'id', 'date_id', 'user_id', 'response', 'created_at', 'updated_at' );** Event Model **
class Model_Outing extends \Orm\Model { protected static $_belongs_to = array('user'); protected static $_has_many = array('dates');**View File**
<table class="table table-striped"> <thead> <tr> <th width="15%">Invited</th> <?php foreach ($outing->dates as $d): ?> <th><?php echo date('D F j', strtotime($d->date)); ?></th> <?php endforeach; ?> </tr> </thead> <tbody> <?php foreach ($outing->invites as $i): ?> <tr> <td><?php echo $i->user->name; ?></td> <?php foreach ($dates as $d): ?> <td> <?php if ($i->user == $this->current_user): ?> <?php echo Form::open(array('action' => 'response', 'class' => 'date-response', 'method' => 'post')); ?> <?php echo Form::hidden('date_id', $d->id); ?> <?php echo Form::checkbox('response', 1); ?> <?php echo Form::close(); ?> <?php else: ?> RESPONSE OF 1 OR 0 here for the response for this date, by the user. <?php endif; ?> </td> <?php endforeach; ?> </tr> <?php endforeach; ?> </tbody> </table>
Harro Verton wrote on Thursday 18th of October 2012:Assuming $dates in your view is the result of a find(), you can include the related response in the result. Then $d->response will be an array with all responses for that date. You'll have to loop over that, and when the 'user_id' matches $i->id, you've got the response of the current user in the outer foreach. Save that result and break the loop. After the loop, if you don't have a reponse, set it to 0. So now you have the 0 or 1 for that date and that user.
<tbody> <?php foreach ($outing->invites as $i): ?> <tr> <td> <?php if ($i->user == $this->current_user): ?> You <?php else: ?> <?php echo $i->user->name; ?> <?php endif; ?> </td> <?php foreach ($dates as $d): ?> <td <?php $checked = false; $icon = '<i class="icon-remove"></i>' ?> <?php if ($i->user == $this->current_user): ?> <?php foreach ($d->responses as $r): ?> <?php if ($r->user_id === $i->user->id){$checked = true;} ?> <?php endforeach; ?> <?php echo Form::open(array('action' => 'response', 'class' => 'date-response', 'method' => 'post')); ?> <?php echo Form::hidden('date_id', $d->id); ?> <?php echo Form::checkbox('response', 1, $checked); ?> <?php echo Form::close(); ?> <?php else: ?> <?php foreach ($d->responses as $r): ?> <?php if ($r->user_id === $i->user->id): ?> <?php if ($r->response and $r->user_id == $i->user->id): ?> <?php $icon = '<i class="icon-ok"></i>'; ?> <?php echo $icon; ?> <?php endif; ?> <?php endif; ?> <?php endforeach; ?> <?php echo $icon; ?> <?php endif; ?> </td> <?php endforeach; ?> </tr> <?php endforeach; ?> </tbody>
Harro Verton wrote on Thursday 18th of October 2012:There's so much code and logic in here I would seriously think about using a Viewmodel. Would keep your view simple and clean, and use the viewmodel to convert the data to a unified structure you can easily work with in the view.
It looks like you're new here. If you want to get involved, click one of these buttons!