How to display in alphabetical order - Cakephp

0

I'm listing neighborhoods, when I select instead of getting the value from the list, json_encode is returning its position. ex:

-BAIRRO I -BIRTH II

If I select "Neighborhood II", it will return me: "1"

"0" -BAIRRO I "1" -BAIRRO II ...

Follow the code below:

    public function pegarBairros ($cidade = null) {
    $this->layout = 'json';
    $result = array();

    if (in_array($_REQUEST['cidade'], array_keys($this->cidade))) {
        $this->loadModel('Bairro');
        $bairros = $this->Bairro->find('list', array('fields' => array('id','bairro'), 'conditions' => array('cidade' => $this->cidade[$_REQUEST['cidade']]),'group' => 'bairro'));
        sort($bairros);     
        foreach ($bairros as $bairro) 
            if (!empty($bairro)){
                $result[] = $bairro;
                $arr = $result;
                json_encode($arr);
            }
    } else $result[] = 'error';
    $this->set('data', $arr);
}

For me to show, I use this:

echo $this->Form->input('bairro', array('label' => 'Bairro', 'empty' => 'Selecione o Bairro', 'options' => array() ));

What's wrong with the code?

E How to sort neighborhoods in this line in CakePHP?

$bairros = $this->Bairro->find('list', 
    array('fields' => array('id','bairro'), 
          'conditions' => array('cidade' => $this->cidade[$_REQUEST['cidade']])
    )
);
    
asked by anonymous 21.02.2014 / 13:14

3 answers

1

Would not it be because you're assigning value to the array without declaring index?

In this case, the interpreter automatically adds index 0 to the beginning of the array.

foreach ($bairros as $bairro) 
        if (!empty($bairro)){
            $result[] = $bairro;
            $arr = $result;
            json_encode($arr);
        }
} else $result[] = 'error';

Try this:

\Seu código...
foreach ($bairros as $indice => $bairro) {
        if (!empty($bairro)){
            $result[$indice] = $bairro;
        }
 }
 //após iteração(foreach) de todo array, usar encode.
 json_encode($result);
} else {
$result = false;

Using the default

foreach ($array as $key => $value)

It provides the index of the array itself that you got in find('list', $params) through the variable $key .

    
26.02.2014 / 18:42
0

Just add the parameter order :

$bairros = $this->Bairro->find('list', array('order' => array('Bairro.bairro ASC'), 'fields' => array('id','bairro'), 'conditions' => array('cidade' => $this->cidade[$_REQUEST['cidade']])));

ASC is not necessary in this case, as we know in SQL , I put it so that you can see that there is this possibility, both ASC and DESC .     

21.02.2014 / 16:20
0

Possibly the error is while assembling the neighborhood array.

Your problem is the sort function. It reorders the indexes of the array, if you put the database ordering your problem is solved.

CakePHP allows you to use the following positions as a find parameter.

array(
    'conditions' => array('Model.field' => $thisValue), //array of conditions
    'recursive' => 1, //int
    //array of field names
    'fields' => array('Model.field1', 'DISTINCT Model.field2'),
    //string or array defining order
    'order' => array('Model.created', 'Model.field3 DESC'),
    'group' => array('Model.field'), //fields to GROUP BY
    'limit' => n, //int
    'page' => n, //int
    'offset' => n, //int
    'callbacks' => true //other possible values are false, 'before', 'after'
)

For json to look like this:

{
    'bairros' : [
        'bairro1' : 1 // bairro => id,
        'bairro2' : 2,
        'bairro3' : 3
     ],
     success : true
}

PHP would look like this

public function pegarBairros ($cidade = null) {
    $this->layout = 'json';
    $result = array();

    if (in_array($_REQUEST['cidade'], array_keys($this->cidade))) {
        $this->loadModel('Bairro');
        $bairros = $this->Bairro->find( 'list', array(
             'fields' => array( 'bairro','id' ),
             'conditions' => array('cidade' => $this->cidade[$_REQUEST['cidade']]),
             'group' => 'bairro'),
             'order' => array('Bairro.bairro' => 'ASC')
        );
        $result['bairros'] = $bairros;
        $result['success'] = true;
    } else {
        $result['success'] = false;
    }
    $return = json_encode($result);
    $this->set('data', $return);
}

For json to look like this:

{
    'bairros' : [
        1 : 'bairro1' // id => bairro,
        2 : 'bairro2',
        3 : 'bairro3'
     ],
     success : true
}

PHP would look like this

public function pegarBairros ($cidade = null) {
    $this->layout = 'json';
    $result = array();

    if (in_array($_REQUEST['cidade'], array_keys($this->cidade))) {
        $this->loadModel('Bairro');
        $bairros = $this->Bairro->find( 'list', array(
             'fields' => array( 'id','bairro' ),
             'conditions' => array('cidade' => $this->cidade[$_REQUEST['cidade']]),
             'group' => 'bairro'),
             'order' => array('Bairro.bairro' => 'ASC')
        );    
        $result['bairros'] = $bairros;
        $result['success'] = true;
    } else {
        $result['success'] = false;
    }
    $return = json_encode($result);
    $this->set('data', $return);
}

Source CakePHP

    
21.02.2014 / 13:41