Set <option> in edit - CAKEPHP

Asked

Viewed 687 times

2

I’m working on a real estate project and in it I have the add, edit and delete, everything is normal. In real estate I have tables cidade and bairro, that when I register there are two <option> but when I go to edit the real estate, the city and the neighborhood, they don’t take the place of the option, that is, the customer has to search the city and the neighborhood again, wanted to set the city and the neighborhood in the edit.

These are the options:

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

This is my Immovelscontroller, the edit function:

public function editar($id = null) {

    if (!$id) return $this->redirect(array('action' => 'index'));
    $imovel = $this->Imovel->read(null, $id);
    if ($imovel['Imovel']['creator'] != $this->Session->read('Auth.User.Imobiliaria.id')) return $this->redirect(array('action' => 'index'));

    if (empty($this->request->data)) {


        $this->set('imovel', $imovel);  
        $id_cidade = $imovel['Imovel']['cidade'];

            include 'banco.php';
            $sql = mysql_query("SELECT * FROM cidades WHERE id = '$id_cidade' ");
            while($result = mysql_fetch_array($sql)){
                $id = $result['id'];
                $nomeCidade = $result['nome'];
            }
        $this->loadModel('Bairro');
        $bairrosRioClaro = $this->Bairro->find('list', array('fields' => array('id', 'bairro'), 'conditions' => array('cidade' => $nomeCidade)));
        //print_r($bairrosRioClaro);

        $this->set('bairrosRioClaro', $bairrosRioClaro);

        $especificacoes = array();
        $tipo = $imovel['Imovel']['tipo'];
        if ($tipo == 'residencial') {
            $especificacoes = array(
                1 => 'Casa',
                2 => 'Apartamento',
                3 => 'Condomínio',
                4 => 'Chácara',
                5 => 'Kitnet',
                6 => 'Temporada'
            );
        } elseif ($tipo == 'comercial') {
            $especificacoes = array(
                7 => 'Barracão', 
                8 => 'Salas comerciais', 
                9 => 'Ponto comercial', 
            );
        } elseif ($tipo == 'empreendimento') {
            $especificacoes = array(
                10 => 'Comercial', 
                11 => 'Residencial', 
            );
        } elseif ($tipo == 'terreno') {
            $especificacoes = array(
                12 => 'Comercal', 
                13 => 'Residencial', 
                14 => 'Comercial/Residencial', 
            );
        } elseif ($tipo == 'rural') {
            $especificacoes = array(
                15 => 'Fazenda', 
                16 => 'Sítio', 
                17 => 'Chácara', 
            );
        } else {
            $especificacoes = null;
        }
        $this->set('especificacoes', $especificacoes);
        $this->set('data', $imovel);

    } else {
        $data = $this->request->data;

        if ($data['Imovel']['opcoes']) $data['Imovel']['opcoes'] = implode(';', $data['Imovel']['opcoes']);
        $data['Imovel']['modifier'] = $this->Session->read('Auth.User.Imobiliaria.id');
        $data['Imovel']['especificacao'] = $data['Imovel']['imovel'];
        if ($data['Imovel']['bairro']) {
            $bairro = $this->Cep->find('first', array('conditions' => array('id' => $data['Imovel']['bairro'])));
            $data['Imovel']['bairroNome'] = $bairro['Cep']['bairro'];
        }
        if ($data['Imovel']['cidade']) {
            $cidade = $this->Cidade->find('first', array('conditions' => array('id' => $data['Imovel']['cidade'])));
            $data['Imovel']['cidadeNome'] = $cidade['Cidade']['nome'];
        }
        if ($this->Imovel->save($data)) return $this->redirect(array('action' => 'index'));
        $this->set('data', $data);

    }
}

He’s returning the real estate data:

array(
'Imovel' => array(
    'id' => '150',
    'codigo' => 'TESTEIMOVEL',
    'placa' => 'TESTEIMO',
    'tipo' => 'residencial',
    'cidade' => '9566',
    'cidadeNome' => 'Rio Claro',
    'bairro' => '578008',
    'bairroNome' => 'Jardim Donângela',
    'intencao' => '1',
    'especificacao' => '1',
    'endereco' => 'TESTEIMOVEL',
    'valor' => '1',
    'condominio' => 'TESTEIMOVEL',
    'salas' => '0',
    'dormitorios' => '0',
    'suites' => '0',
    'banheiros' => '0',
    'garagem' => '0',
    'complemento' => 'TESTEIMOVEL',
    'areaConstruida' => 'TESTEIMOVEL',
    'areaTotal' => 'TESTEIMOVEL',
    'descricao' => 'TESTEIMOVEL',
    'geolocalizacao' => 'Sob Consulta',
    'video' => null,
    'foto1' => '',
    'foto2' => '',
    'foto3' => '',
    'foto4' => '',
    'foto5' => '',
    'foto6' => '',
    'legendaFoto1' => '',
    'legendaFoto2' => '',
    'legendaFoto3' => '',
    'legendaFoto4' => '',
    'legendaFoto5' => '',
    'legendaFoto6' => '',
    'pageview' => '0',
    'opcoes' => '',
    'destaque' => '0',
    'ativo' => '1',
    'ativoPeloAdm' => '1',
    'created' => '2014-02-26 09:53:13',
    'creator' => '110',
    'modified' => '2014-02-26 09:53:13',
    'modifier' => '110'
)
)
  • A question, in your view all cities and neighborhoods are shown, however, the city that had already been chosen is not marked, this is the problem?

  • No, I wanted the city of real estate, I was already there in the countryside, without having the need to change the city and the neighborhood again, for example, I will change the real estate "a", but I will only change if she has garage, there I do not need to change the city and the neighborhood...got......

  • Now I got confused more, come on. You have a real estate agent who is already registered, when you will edit it the saved options are already selected?

  • Bairro is a relation table? Where does that come from $Cidades?

  • include 'database.php'; $sql = mysql_query("SELECT * FROM cities WHERE id = '$id_city' "); while($result = mysql_fetch_array($sql)){ $id = $result['id']; $nameCity = $result['name']; } First of all, this snippet should not even be written there. Anyway, basically it doesn’t make sense to write this if you’re using Cakephp, it takes care of CRUD itself.

  • At the end of the day I’m thinking there’s something wrong with Models

  • @Danielomine yes, I know, but do not know cakephp, only pure php, the modifications I was adapting to php

  • @Erloncharles as so selected? when I go edit, the city and the neighborhood are not set, I have to search the city and the neighborhood again

  • @Furlan, read this chapter a little Retrieving Your Data

  • @Furlan, do the following for me put the following code in your controller debug($imovel);, insert this immediately after $imovel = $this->Imovel->read(null, $id); after that add feedback to the question

  • @Erloncharles ready, take a look at the question, is returning the real estate data..

  • Wonder now I can give my answer

Show 7 more comments

2 answers

1

Your view code should be:

echo $this->Form->input('cidade', array('label' => 'Cidade', '', 'empty' => 'Selecione', 'type' => 'select', 'options' => $cidades, 'default' => $cidadeSelected));
echo $this->Form->input('bairro', array('label' => 'Bairro', 'empty' => 'Selecione o Bairro', 'type' => 'select', 'options' => $bairros, 'default' => $bairroSelected));

and exchange your controller code for this one:

public function editar($id = null) {

    if (!$id) return $this->redirect(array('action' => 'index'));
    $imovel = $this->Imovel->read(null, $id);
    if ($imovel['Imovel']['creator'] != $this->Session->read('Auth.User.Imobiliaria.id')) return $this->redirect(array('action' => 'index'));

    if (empty($this->request->data)) {

        $this->set('imovel', $imovel);
        $id_cidade = $imovel['Imovel']['cidade'];
        $id_bairro = $imovel['Imovel']['bairro'];

        $this->set('bairroSelected', $id_bairro);
        $this->set('cidadeSelected', $id_cidade);

        // Carrega todas as cidades
        $this->loadModel('Cidade');
        $cidades = $this->Cidade->find('list', array('fields' => array('id', 'nome')));
        $this->set('cidades', $cidades);

        // Carrega todos os bairros
        $this->loadModel('Bairro');
        $bairros = $this->Bairro->find('list', array('fields' => array('id', 'bairro'), 'conditions' => array('cidade' => $imovel['Imovel']['cidadeNome'])));

        $this->set('bairros', $bairros);

        $especificacoes = array();
        $tipo = $imovel['Imovel']['tipo'];
        if ($tipo == 'residencial') {
            $especificacoes = array(
                1 => 'Casa',
                2 => 'Apartamento',
                3 => 'Condomínio',
                4 => 'Chácara',
                5 => 'Kitnet',
                6 => 'Temporada'
            );
        } elseif ($tipo == 'comercial') {
            $especificacoes = array(
                7 => 'Barracão', 
                8 => 'Salas comerciais', 
                9 => 'Ponto comercial', 
            );
        } elseif ($tipo == 'empreendimento') {
            $especificacoes = array(
                10 => 'Comercial', 
                11 => 'Residencial', 
            );
        } elseif ($tipo == 'terreno') {
            $especificacoes = array(
                12 => 'Comercal', 
                13 => 'Residencial', 
                14 => 'Comercial/Residencial', 
            );
        } elseif ($tipo == 'rural') {
            $especificacoes = array(
                15 => 'Fazenda', 
                16 => 'Sítio', 
                17 => 'Chácara', 
            );
        } else {
            $especificacoes = null;
        }
        $this->set('especificacoes', $especificacoes);
        $this->set('data', $imovel);

    } else {
        $data = $this->request->data;

        if ($data['Imovel']['opcoes']) $data['Imovel']['opcoes'] = implode(';', $data['Imovel']['opcoes']);
        $data['Imovel']['modifier'] = $this->Session->read('Auth.User.Imobiliaria.id');
        $data['Imovel']['especificacao'] = $data['Imovel']['imovel'];
        if ($data['Imovel']['bairro']) {
            $bairro = $this->Cep->find('first', array('conditions' => array('id' => $data['Imovel']['bairro'])));
            $data['Imovel']['bairroNome'] = $bairro['Cep']['bairro'];
        }
        if ($data['Imovel']['cidade']) {
            $cidade = $this->Cidade->find('first', array('conditions' => array('id' => $data['Imovel']['cidade'])));
            $data['Imovel']['cidadeNome'] = $cidade['Cidade']['nome'];
        }
        if ($this->Imovel->save($data)) return $this->redirect(array('action' => 'index'));
        $this->set('data', $data);

    }
}

0

I don’t know how to use Cakephp, but my solution works, I’ve already made more than 6 real estate websites, and in all I used this function to select the city, neighborhood, state, etc that the property was registered.

In jQuery:

(function($){

var cidade = '<?php echo $cidade ?>';
var bairro = '<?php echo $estado ?>';

    $('select#cidade option').each(function(){
        if($(this).val() == cidade){
            $(this).attr('selected',true);
        }
    });

    $('select#bairro option').each(function(){
        if($(this).val() == bairro){
            $(this).attr('selected',true);
        }
    });

    })(jQuery);

The code will go through all the options you have in select, and whatever is equal to the city variable / neighborhood it will set as Selected.

Browser other questions tagged

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