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

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

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 foreach

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

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

How to fetch rows from associated hasMany model in Cake 3.0?

//PagesTable.php
class PagesTable extends Table
{
public function initialize(array $config)
{
$this->belongsTo('Books');
}
}
//BooksTable.php
class BooksTable extends Table
{
public function initialize(array $config)
{
$this->hasMany('Pages');
}
}
From the above two models, I want to retrieve pages that belong to a particular book in the BooksController.
After several hours of going through documentation, I somehow built this that is able to achieve what I want.
$pages = $this->Books->Pages->find()->where(['Pages.book_id' => $id]);
I have a feeling that I have not exploited cake here because it looks like I am building a custom query rather than just pulling the associated data. But as a beginner in cake 3.0 (with no experience in cake 2.x), this was the best I could do.
Am I going the right way ?
Isn't there a better way to find the pages of a book (rows of an associated hasMany model)?
there is a simpler way check this:
in the controller
$maestro = $this->Maestro->get($id, [
'contain' => ['DetalleMaestro']]);
$this->set('maestro', $maestro);
this search in the db all the maestro records and the associated records detallemaestro
in the view
<?php if (!empty($maestro->detalle)): ?>
<h5><?= __('Fuentes de Informacion Asociadas') ?></h5>
<table cellpadding="0" cellspacing="0">
<tr>
<th><?= __('Nombre 1') ?></th>
<th><?= __('Nombre 2') ?></th>
<th><?= __('Nombre 3') ?></th>
<th><?= __('Nombre 4') ?></th>
</tr>
<?php foreach ($maestro->detalle as $detalle): ?>
<tr>
<td><?= h($detalle->1) ?></td>
<td><?= h($detalle->2) ?></td>
<td><?= h($detalle->3) ?></td>
<td><?= h($detalle->4) ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php endif; ?>
this checks if Maestro has a association and display it

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

Resources