How to view alphabetically - Cakephp

Asked

Viewed 845 times

0

I’m listing neighborhoods, when I select instead of taking the list value, the json_encode is returning to her position. ex:

-BARRIO I -WARD II

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

"0"-QUARTER I "1"-QUARTER II ...

Follow the code:

    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 so:

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

What’s wrong with the code?

E How to sort the neighborhoods in this line in Cakephp?

$bairros = $this->Bairro->find('list', 
    array('fields' => array('id','bairro'), 
          'conditions' => array('cidade' => $this->cidade[$_REQUEST['cidade']])
    )
);
  • Furlan, in its original function, try to rotate a die(var_dump($bairros)); just below its function and post the result.

  • 5

    You have taken the context of the whole question, and the answers have become meaningless. Please go back and add your doubt just below. Your question may be useful to other people.

3 answers

1

It wouldn’t be because you’re assigning value to the array without declaring index?

In this case, the interpreter automatically aggregates index 0 at 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 pattern

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

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

0

Possibly the error is mounting the neighborhood array.

Your problem is the Sort function. It reorders the array indexes, if you place the database ordering your problem is solved.

Cakephp allows using the following positions as the 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'
)

so that json stays that way:

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

so that json stays that way:

{
    '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

  • I put it in the code, and the neighborhoods are no longer listed...?

  • 1

    I made a correction in the code now you came to see?

  • Your problem was the Sort function, it reorders the array indexes, if you place the database ordering your problem is solved.

  • I put your code, even though it doesn’t show the neighborhoods, I took the Sort from my old code there. even so it won’t go...

  • puts a var_dump in the $neighborhoods variable and and adds a exit() right after to pick up the return, or if using xdebug Check the return and add to the question to get a better idea of what might be.

  • 1

    Also please add the code you are using to read the json. So it is easier to see what can be

  • I managed to solve, now I just need to put in alphabetical order...

Show 2 more comments

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

The ASC is not necessary in this case, as we well know in SQL, put so that you can see that there is this possibility, both ASC how much DESC.

  • Dude...I don’t get...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.