I have a small CakePHP app in which I retrieve some client information in a table. In the main table where all my clients show up I would like to sort them by ID (highest ID on top)
My current code is as follows:
<?php foreach($clients as $client) : ?>
<tr>
<td><?php echo $client['Client']['id']?></td>
<td><?php echo $this->Html->link($client['Client']['client_name'], array('action' => 'view', $client['Client']['id']), array('class' => 'view')); ?></td>
<td><?php echo $client['Client']['client_contact']?></td>
<td><?php echo $client['Client']['client_phone']?></td>
<td><?php echo $client['Client']['client_email']?></td>
</tr>
<?php endforeach; ?>
How can I implement the related sort-order here? Some expert help would be greatly appreciated, thank you
you can use order in your find function.Like below code
$this->Client->find('all', array(
'order' => array('Client.id' => 'desc')
));
Related
I using cakephp framework 2. to develop a Employee daily attends, in my application index page i have have 3 options edit , apply holiday, and deleted holiday. Two links, edit and apply working correctly but for delete i am using post link. When there is one delete button as required it did't pass parameter to delete function, but creating copy of same link mean two delete button then the last one pass parameter, i did'd now there is any thing wrong in my code. Here is my View code
<?php
$i=1;
foreach($holidays as $holiday):?>
<tr >
<td><?php echo $i++; ?></td>
<td><?php echo $holiday['Holiday']['title'] ?></td>
<td><?php echo $holiday['Group']['title'] ?></td>
<td><?php echo $holiday['Shift']['title'] ?></td>
<td><?php echo $holiday['Holiday']['from_date'] ?></td>
<td><?php echo $holiday['Holiday']['to_date'] ?></td>
<td><?php echo $this->Html->link('<span class="glyphicon glyphicon- pencil"></span> Edit',array('action'=>'edit', $holiday['Holiday']['id']),array('class'=>'btn btn-warning','escape'=>false)); ?>
<?php echo $this->Html->link('<span class="glyphicon glyphicon-refresh"></span> Apply',array('action'=>'applyholiday', $holiday['Holiday']['id']),array('class'=>'btn btn-success','escape'=>false)); ?>
<?php
echo $this->Form->postLink(
'<span class="glyphicon glyphicon-trash"></span> Delete',
array('action' => 'delete', $holiday['Holiday']['id']),
array('confirm' => 'Are you sure?','escape'=>false,'class'=>'btn btn-danger')
);
?>
</td>
</tr>
<?php endforeach; ?>
<?php unset($holiday); ?>
Here is my HolidaysController code for delete function.
public function delete($id){
if ($this->request->is('get')) {
throw new MethodNotAllowedException();
}
debug($id);
exit;
if($this->Holiday->delete($id)){
$this->Flash->success(__('Record Deleted Success.'));
return $this->redirect($this->referer());
}
}
First : postLink Creates an HTML link, but access the URL using method POST , (see more information here)
means your method of request is post.
And Second:Your condition in delete function is:
if($this->request->is('get')){
//
}
Means your request is never going to inside your function.
Hence just try using:
if($this->request->is('post')){
//
}
instead.
You need to use post instead of get in your controller.
if(($this->request->is('post') || $this->request->is('put')){
//Code goes here
}
When I use foreach to show the my value in the base it show me a false value
in the controller:
public function index_hote() {
$this->Reservation->recursive = 1;
$this->loadModel("Bien");
$biens=$this->Bien->find('first',array('conditions'=>array("Bien.idBien =idBien")));
$reservations=$this->Reservation- >find('first',
array('conditions'=>array('Reservation.idBien'=> $biens['Bien'] ['idBien'])));
Debugger::dump($reservations['Reservation']);
$this->paginate('Reservation');
$this->set('reservations', $reservations['Reservation']);
}
In the view:
<?php foreach ($reservations as $reservation): ?>
<tr>
<?php echo h($reservation) ?>
<td><?php echo h($reservation['dateReserDu']); ?> </td>
<td><?php echo h($reservation['dateReserAu']); ?> </td>
<td><?php echo h($reservation['montantAPaye']); ?> </td>
<td><?php echo h($reservation['etatReservation']); ?> </td>
</tr>
<?php endforeach; ?>
When I debug it shows me the right value (the pic after doing Debugger::dump($reservations['Reservation']);):
but in the table it shows me a false value:
When I do <?php echo h($reservation) ?> it shows me the right value but when I add a attribute such as h($reservation['dateReserDu']); ?> it doesn't work PLZ why? And when can I do?
Why you are using loop here ? You have used find('first') to retrieve one data see
doc .
If you want to retrieve all data use find('all'). However if you want to echo your find 1st data just use below structure
$this->set('reservations', $reservations);
In view
echo $reservations['Reservation']['Your_field_name'];
hi all just wanting to know if you were printing stuff out in a foreach loop how would you code it so that only a specific set of data pertaining to that person. For example I want it to print when active in my relationships table is =false or 0
this is the current code I have in my view.
<?php foreach($Relationships as $relationship):?>
<tr>
<td align='center'><?php echo $relationship['Relationship']['partyone']; ?></td>
<td align='center'><?php echo $relationship['Relationship']['partytwo']; ?></td>
<td> </td>
<td><?php echo $this->Html->link('approve', array('action'=>'approve', $relationship['Relationship']['id'])); ;?>
<td><?php echo $this->Html->link('Decline', array('action'=>'decline', $relationship['Relationship']['id'])); ;?>
</td>
</tr>
<?php endforeach; ?>
The error was in my find condition in the controller, not the view. here is the line of code I had to write for my controller
$this->set('Relationships', $this->Relationship->find('all', array('conditions' => array('Relationship.partytwo' => $userid,'Relationship.active'=>false))));
I've to show the has one relation table data in the listing. I'm using CakePHP pagination and search functionality for the listing view. All the rest of the functionality is working fine, but I'm not able to give the sort functionality for the related table field.
I'm using
<th><?php echo $this->Paginator->sort('First Name','Enrollment.first_name');?></th>
<th><?php echo $this->Paginator->sort('Last Name','Enrollment.last_name');?></th>
<th><?php echo $this->Paginator->sort('Product','Product.name');?></th>
But sorting on product is not working.
<th><?php echo $this->Paginator->sort('First Name','Enrollment.first_name');?></th>
<th><?php echo $this->Paginator->sort('Last Name','Enrollment.last_name');?></th>
<th><?php echo $this->Paginator->sort('Product','Product.name');?></th>
.....
<td>
<?php echo $this->Html->link($YOURVAR['Enrollment']['first_name'], array('controller' => 'enrollments', 'action' => 'view', $YOURVAR['Enrollment']['id'])); ?>
</td>
<td>
<?php echo $this->Html->link($YOURVAR['Enrollment']['title'], array('controller' => 'enrollments', 'action' => 'view', $YOURVAR['Enrollment']['id'])); ?>
</td>
<td>
// Checkout this line that you change the line as following
<?php echo $this->Html->link($YOURVAR['Product']['name'], array('controller' => 'products', 'action' => 'view', $YOURVAR['Product']['id'])); ?>
</td>
I have a simple cakephp form and would like for the 'save translation' button to be to the LEFT of a link which cancels and returns the user to the index, but I can't seem to get it working. It always puts the cancel link below the button.
Code I have now:
<tr>
<td>
<?php echo $this->Form->end('Save Translation'); ?>
</td>
<td>
<?php echo $html->link('Cancel', array('action' => 'index')); ?>
</td>
</tr>
Code I've tried:
echo $this->Form->end('Save Translation',array('label'=>'Save Translation','div' => false));
Try this:
<?php
echo $this->Form->create();
echo $this->Form->input('name');
echo $this->Form->submit('Submit',
array('after' => $this->Html->link('Cancel', array('action' => 'index')))
);
echo $this->Form->end();
?>