Cakephp foreach - cakephp

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'];

Related

Cakephp $this->Form->postLink Not working

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
}

cakephp order by ID

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')
));

Display data from database with using COLUMN not row in CodeIgniter VIEW

I have problem in my view display in codeigniter
I have a problem with my database display in the 'view' in CodeIgniter, I've been able to do load my database and display data using tables, but the data is performed by using extends vertically downwards by using a row, whereas I want to display it horizontally by using column
[my view display in browser]
[data 1]
[data 2]
[data 3]
[etc...]
my view I wanted!
[data 1] [data 2] [data 3] [etc..]
this is my Controller Code:
function coba(){
// nyobain nge load td secara multiple
$data['query'] = $this->Latihan_model->coba()->result();
$this->load->view('rubbish/coba',$data);
}
this is my model Code:
function coba(){
$sql = ("
SELECT soal.ID_soal as soal
from soal
where soal.ID_bagian = 1;
"); return $this->db->query($sql);
}
this is my view code:
<table border=1>
<?php foreach($query as $post): ?>
<tr>
<td><? echo $post->soal; ?></td>
</tr>
<?php endforeach; ?>
I hope you're understand what I am asked
You can try this
<table border=1>
<tr>
<?php foreach($query as $post): ?>
<td><? echo $post->soal; ?></td>
<?php endforeach; ?>
</tr>
try this:
<?php foreach($query as $key => $value): ?>
<tr>
<?php foreach($key as $val): ?>
<td><?php echo $val; ?></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>

CakePHP form, printing out certain queries

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))));

CakePHP $this->Form->end('button text'); on same line with something

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();
?>

Resources