php - Automatically sorta table in a CakePHP web app
Get the solution ↓↓↓I have a table displayed in a page within my CakePHP web app set with paginator. The table always appears sorted ascending on 'id' (I really don't know why) and I would like it to be sorted also on 'id' but descending. Do you know if there is any simple function to make this thing happen? Please, find the code below.
<table>
<thead>
<tr>
<th scope="col"><?= $this->Paginator->sort('id') ?></th>
<th scope="col"><?= $this->Paginator->sort('contact_id') ?></th>
<th scope="col"><?= $this->Paginator->sort('sum_price') ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($orders as $order): ?>
<tr>
<td><?= $this->Number->format($order->id) ?></td>
<td><?= $this->Html->link($order->Contacts["name"] , ['controller' => 'Contacts', 'action' => 'view', $order->Contacts["id"]]) : '' ?></td>
<td><?= $this->Number->format($order->sum_price) ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
So far paginator is working fine, so I can click on the column headers to sort the data. Anyway, I would like it to be displayed properly as it loads for the first time.
Please, let me know if there is any other piece of code that might be important.
Thank you in advance!
EDIT
In case it helps, I have these two functions in my app controller:
public function all()
{
$orders = $this->Orders->find("all")->contain(["contacts"]);
$orders = $this->paginate($orders);
$this->set(compact('orders'));
$this->viewBuilder()->setLayout("admin");
}
public function view($id = null)
{
$this->loadModel('ContactAdresses');
$this->loadModel('Contacts');
$order = $this->Orders->get($id, ['contain' => ['Contacts']]);
$contactId = $order->contact->id;
$contact = $this->Contacts->get($contactId, ['contain' => ['ContactAdresses']]);
$this->set('order', $order);
$this->set('contact_adress', $contact->contact_adress);
$this->viewBuilder()->setLayout("admin");
}
Answer
Solution:
public function all()
{
$orders = $this->Orders->find("all", ['order' => ['id' => 'DESC']])->contain(["contacts"]);
$orders = $this->paginate($orders);
$this->set(compact('orders'));
$this->viewBuilder()->setLayout("admin");
}
OR
public function all()
{
$this->paginate = [
'contain' => ['contacts',],
'order' => ['id' => 'DESC'],
],
];
$query = $this->Orders->find('all');
$this->set('orders', $this->paginate($query));
$this->viewBuilder()->setLayout("admin");
}
Answer
Solution:
Here is an example of a functional code snippet that I wrote.
public function index() {
$this->paginate = [
'contain' => ['Ctrs', 'States', 'Stages'],
'order' => ['created' => 'DESC']
];
$movements = $this->paginate($this->Movements);
$this->set(compact('movements'));
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: closed without sending a request; it was probably just an unused speculative preconnection
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.