Sunday 29 July 2012

How to create paging and shorting in cake php


1. Controller/controllername.php
class ControllerNameController extends AppController {

var $name = 'ControllerName';
public $uses = array('TablesName');


function inbox() {
$this->paginate = array(
'conditions' => array('TablesName.receiver_delete'=>'0', 'AND'=>array('TablesName.user_to'=>$parameters ['id'])),'limit' => 10,'order' => array('TablesName.sent_date'=>'desc'
)
);
$data = $this->paginate('TablesName');
$this->set('messages', $data);
}


  1. views/elements/paging.ctp
    <?php
echo "<div align='left' class='l1'>".
$this->Paginator->counter(array('format' => 'Showing %page% to %pages% of %count% entries' )).
"</div><div align='right' class='r1'>".
$this->Paginator->first('First', null, null, array('class' => 'disabled'))."&nbsp;&nbsp;&nbsp;".
$this->Paginator->prev('« Prev', null, null, array('class' => 'disabled'))."&nbsp;&nbsp;".
$this->Paginator->numbers()."&nbsp;&nbsp;".
$this->Paginator->next('Next »', null, null, array('class' => 'disabled'))."&nbsp;&nbsp;".
$this->Paginator->last('Last', null, null, array('class' => 'disabled'))."".
"</div>";
?>

  1. views/paging vies file

<table width="100%" style="border: 1px solid #ccc;" border='0'>
<?php
if (!empty($messages)) {
echo "<tr>";
echo "<td colspan=\"5\" align=\"right\">". $this->element('paging')."</td>";
echo "</tr>";
echo "<tr style=\"background-color: #55699F;\">";
echo "<td><font color=white>S.No</font></td>";
echo "<td><font color=white>".$this->Paginator->sort('From', 'user_from')."</font></td>";
echo "<td><font color=white>".$this->Paginator->sort('Title', 'subject')."</font></td>";
echo "<td><font color=white>".$this->Paginator->sort('Date & Time', 'sent_date')."</font></td>";
echo "<td><font color=white>Action</font></td>";
echo "<tr>";
foreach ($messages as $message) {?>
<tr class="<?php echo ($message['Message']['status']=='Un-read')? 'unread':'read'?>">
<td><?php echo $message['Message']['id'] ?></td>
<td onclick="read('<?php echo base64_encode($message['Message']['id'])?>/inbox')" style="width: 200px"><?php echo $message['user_from']['email']?></td>
<td onclick="read('<?php echo base64_encode($message['Message']['id'])?>/inbox')" style="width: 500px"><?php echo $message['Message']['subject']?></td>
<td onclick="read('<?php echo base64_encode($message['Message']['id'])?>/inbox')" style="width: 130px"><?php echo $message['Message']['sent_date']?></td>
<td style="width: 40px"><a onclick="return confirm('Do you really want to delete this message?');" href="delete/<?php echo base64_encode($message['Message']['id']);?>/inbox">Delete</a></td>
</tr>
<?php
}
echo "<tr>";
echo "<td style=\"height:20px\"></td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan=\"5\" align=\"right\">". $this->element('paging')."</td>";
echo "</tr>";
} else {?>
<tr >
<td colspan="5">Ther are no messages in inbox.</td>
</tr>
<?php
}
?>
</table>

No comments:

Post a Comment