Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Getting a users choice in a multiple choice poll
  • I'm trying to create a poll system, and using ajax I have managed to get the vote to submit on checkbox click, see the image here http://f.cl.ly/items/432G3J1v1m173P3S1N0E/Screen Shot 2012-10-18 at 15.44.27.png However, I'm struggling to figure out the way to show the users vote choice by selecting the checkbox. The problem is the result of the query and how to display that best. "RESPONSE OF 1 OR 0 here for the response for this date, by the user." I have my relationships setup as follows
    **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>
    

    Any help is appreciated
  • 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.
  • 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.

    Thanks I gave something similar a shot earlier, I think I was onto it, but I kept, and still keep getting in the example below when there is a response, 2 ticks. Even though there is just one response in the table. Maybe I've been looking at this for too long, and if you can think of a better way of clearing up that loop, currently, it's a foreach...
    <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>
    

    Regards :)
  • 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.
  • 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.

    Haha yeah I think it needs cleaning up too, I'll do my best at creating a View Model :) Thanks again, you're amazing!

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion