Monday, August 30, 2010

paginate + filters

Hi, i have some filters in the index page of a listing, you can choose all of the filters or just one, if i choose all of them they work fine but if i only choose one it gives me a blank web page, here is the code:

/app/controllers/quotes_controller.php
function index() {
        $this->Quote->recursive = 0;
        $filters = array();
       
        if(!empty($this->data['Quote']['desde']) && !empty($this->data['Quote']['hasta'])){
               $filters[1] = "Quote.fevento BETWEEN '".$this->data['Quote']['desde']."' AND '".$this->data['Quote']['hasta']."'";
              
        }
       
        if(!empty($this->data['Quote']['cliente_id'])){
            $filters[2] = "Quote.cliente_id = ".$this->data['Quote']['cliente_id'];
           
            $this->set('cliente_id', $this->data['Quote']['cliente_id']);
            $this->set('autocliente', $this->data['Quote']['autocliente']);
        }
       
        if(empty($filters)) {
            $filters[3] = "WEEK(Quote.fevento) = WEEK(NOW())";
            $this->set('cliente_id', '');
            $this->set('autocliente', '');
        }
       
        $filters[4] = "Quote.status = 0";
       
        $this->set('quotes', $this->paginate(null, $filters));
    }

/app/views/quotes/index.ctp

<table cellpadding="0" cellspacing="0">
    <tr>
            <th><?php echo $this->Paginator->sort('user_id');?></th>
            <th><?php echo $this->Paginator->sort('cliente_id');?></th>
            <th><?php echo $this->Paginator->sort('fentrega');?></th>
            <th><?php echo $this->Paginator->sort('fevento');?></th>
            <th><?php echo $this->Paginator->sort('frecoleccion');?></th>
            <th><?php echo $this->Paginator->sort('solicitante');?></th>
            <th><?php echo 'Total'?></th>
            <th><?php echo $this->Paginator->sort('created');?></th>
            <th class="actions"><?php __('Actions');?></th>
    </tr>
    <?php
    $i = 0;
    foreach ($quotes as $quote):
        $class = null;
        if ($i++ % 2 == 0) {
            $class = ' class="altrow"';
        }
    ?>
   
    <?php
        $total = 0;
        $subtotal = $quote['Quote']['subtotal'];
        $descuento = $subtotal*$quote['Quote']['descuento']/100;
        $flete = $quote['Quote']['flete'];
        $iva = ($subtotal-$descuento+$flete)*$quote['Quote']['iva']/100;
        $total_final = $subtotal-$descuento+$flete+$iva;
    ?>
   
    <tr<?php echo $class;?>>
        <td>
            <?php echo $this->Html->link($quote['User']['username'], array('controller' => 'users', 'action' => 'view', $quote['User']['id'])); ?>
        </td>
        <td>
            <?php echo $this->Html->link($quote['Cliente']['name'], array('controller' => 'clientes', 'action' => 'view', $quote['Cliente']['id'])); ?>
        </td>
        <td><?php echo $quote['Quote']['fentrega']; ?>&nbsp;</td>
        <td><?php echo $quote['Quote']['fevento']; ?>&nbsp;</td>
        <td><?php echo $quote['Quote']['frecoleccion']; ?>&nbsp;</td>
        <td><?php echo $quote['Quote']['solicitante']; ?>&nbsp;</td>
        <td><?php echo '$'.number_format($total_final,2); ?>&nbsp;</td>
        <td><?php echo $this->Time->format('d-m-Y',$quote['Quote']['created']); ?>&nbsp;</td>
        <td class="actions">
            <?php echo $this->Html->link(__('View', true), array('action' => 'view', $quote['Quote']['id'])); ?>
            <?php echo $this->Html->link(__('Edit', true), array('action' => 'edit', $quote['Quote']['id'])); ?>
            <?php echo $this->Html->link(__('Delete', true), array('action' => 'delete', $quote['Quote']['id']), null, sprintf(__('Are you sure you want to delete # %s?', true), $quote['Quote']['id'])); ?>
        </td>
    </tr>
<?php endforeach; ?>
    </table>

Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.
 
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
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?hl=en

No comments: