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
}
Related
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'];
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')
));
Trying to save the current event id '14' in the url '/localhost/topp/events/view/14' to a event_id field in a comments table. I am using the add function in the comments controller and an 'add comment' button on the events view.ctp page. Any ideas? I have been advised to use a hidden id field and $this->params['pass']['0'] but not sure how to go about this, any advice is welcome. Thanks in advance.
My current comments controller add action code:
public function add() {
//$this->loadModel('Event');
if ($this->request->is('post')) {
$this->Comment->create();
$this->request->data['Comment']['user_id'] = $this->Auth->user('id');
//Commented below is the logic Iam trying to achieve but is wrong
//$this->request->data['Comment']['event_id'] = $this->request->data['Event']['id'];
if ($this->Comment->save($this->request->data)) {
$this->Session->setFlash(__('The comment has been saved'));
$this->redirect(array('controller' => 'events', 'action' => 'myevents'));
} else {
$this->Session->setFlash(__('The comment could not be saved. Please, try again.'));
}
}
}
//////////////////////////////////////// My Events View code with add comment link//////////////////////////
<div class="events view">
<?php echo $this->element('maintitle'); ?>
<?php echo $this->element('profile_area'); ?>
<h2><?php echo __('Event'); ?></h2>
<dl>
<td><?php echo $this->Html->image('/files/event/photo/'.$event['Event']['id'].'/thumb_'.$event['Event']['photo'], array("alt" => "No Event Image")); ?> </td>
<td><?php echo $this->Html->image('/files/event/photo2/'.$event['Event']['id'].'/thumb_'.$event['Event']['photo2'], array("alt" => "No Event Image")); ?> </td>
<td><?php echo $this->Html->image('/files/event/photo3/'.$event['Event']['id'].'/thumb_'.$event['Event']['photo3'], array("alt" => "No Event Image")); ?> </td>
<td><?php echo $this->Html->image('/files/event/photo4/'.$event['Event']['id'].'/thumb_'.$event['Event']['photo4'], array("alt" => "No Event Image")); ?> </td>
<dt></dt>
<dd>
<br>
</dd>
<dt><?php echo __('Name'); ?></dt>
<dd>
<?php echo h($event['Event']['name']); ?>
</dd>
<dt><?php echo __('Date'); ?></dt>
<dd>
<?php echo h($event['Event']['date']); ?>
</dd>
</dl>
</div>
<div class="related">
<h3><?php echo __('Related Comments'); ?></h3>
<?php if (!empty($event['Comment'])): ?>
<?php echo ('Add a comment for this event') ?>
<?php
$i = 0;
foreach ($event['Comment'] as $comment): ?>
<tr>
<td><?php echo $comment['comment']; ?></td>
<td class="actions">
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
<div class="actions">
<ul>
<li><?php echo $this->Html->link(__('New Comment'), array('controller' => 'comments', 'action' => 'add')); ?> </li>
</ul>
</div>
</div>
Let me see if I got this right.
The form you posted is in the event view
The link "New Comment" redirects to another view "add", in the comments controller
This add view has a form to actually write a comment (I'm guessing this part,
you didn't put that code here)
You want the add function to have the event_id from where this came from.
Right?
You then need to pass the id of the event from event_view.ctp (I'm inventing names here) to comment_add.ctp.
So two ways to do it
First way: pass the id of the event in the link and recieve it in the action like this
//event_view.ctp
//I'm going to assume you know the event id here
$this->Html->link(__('New Comment'), array('controller' => 'comments', 'action' => 'add', $event_id)); ?>
That will create a link like comments/add/14, for example, where 14 is the event id. Now in the action, you receive the id
//pass it like parameter of the action
public function add($event_id = null) {
if ($this->request->is('post')) {
$this->Comment->create();
$this->request->data['Comment']['user_id'] = $this->Auth->user('id');
//check that the event_id isn't null and that the event actually exists first
$this->request->data['Comment']['event_id'] = $event_id;
/*etc*/
}
}
You don't need to pass the event_id to the comment_add view, if the user doesn't need to handle it, there's no need to add it to the form.
Second way: If you don't want to pass the event_id inside the url, you need to pass is by POST. To do that, the link you now have to add new comments needs to change to a form with the event_id hidden.
echo $this->Form->create('Comment', array('url'=>array('controller' => 'comments', 'action' => 'add')));
echo $this->Form->input('event_id', array('type'=>'hidden', 'value'=>$event_id);
echo $this->Form->end('New comment');
//instead of <?php echo $this->Html->link(__('New Comment'), array('controller' => 'comments', 'action' => 'add')); ?>
And in the action of the comments, check if the post you receive contains event_id, but not user_id, for example. I find this way a bit messy because every request you receive for that action is going to be a post, so it's be harder to differentiate when you want to display the form to be filled, and when you want to save it.
I like the first way better, and I recommend doing that. But I felt the need to point out there are other ways to do it. Depends what you want.
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 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();
?>