1
votes

After trying the tutorials I would like to modify the bookmarker tutorial to include the functionality of sorting data before displaying it in the view. In the tutorial you create a link to sort while clicking but I would like to output the data sorted by one of the columns directly. You can sort data in a number of ways. One way is to sort the data in the database every time you add a new entity, another is to leave the database alone and sort just before outputting to the view. When I try to sort in the view I just cannot get it to work.

<?php foreach ($users as $user): ?>
        <tr>
            <td><?= h($user->username) ?></td>
            <td><?= h($user->region) ?></td>
        </tr>

    <?php endforeach; ?>

let's say we want to sort by region. The data in $user is accessible and displayed as an output. My question is: how do you sort the date in $user by region? i've tried to use the ksort but no success so far.

$users=Set::sort($user,'region','DESC');

getting the error:

Error: Class 'Set' not found File C:\wamp\www\laravel\src\Template\Users\index.ctp Line: 14

Thanks for your help.

1
What version of CakePHP are you using? Please tag the question with the version in use.C:\wamp\www\laravel <- are you sure you're using CakePHP..?AD7six
I'm using version 3.0 and yes, it's cakephp. had some difficulties with the root directory in apache (set it to a different directory but was still pointing to c:\wamp\www\laravel for some reason I couldn't figure out)ThirtyKnots

1 Answers

0
votes

Import classes before using them

Classes need to be loaded to be available, otherwise php will look for them in the global namespace:

<?php
use Cake\Utility\Hash;

$stuff = Hash::sort($input);

Note that the Set class was deprecated in 2.x and replaced by the Hash class. Be sure to check the migration guide whenever using a new version.