In a dependent SELECT, with data of cities of a table how to leave a city in evidence?

Asked

Viewed 192 times

1

In a database, I have the States table and the Cities table. On the website, when I choose a state, the list of cities is populated according to it. In my code, it is to appear alphabetically, but I wanted the city of Ribeirão Preto to be at the top of the list of the state of SP. How do I put Ribeirão Preto at the top of the list and the rest below, considering that they are dependent selection input?

My html

<div class="col-sm-6">
        <select name="estado" id="estado" class="form-control form-b" required>
            <option value="" class="option_cad"> ESTADO</option>
                <?php foreach($estados as $estado) { ?>
                    <option value="<?php echo $estado->id; ?>" class="option_cad"><?php echo $estado->nome; ?></option>
                <?php } ?>
        </select>
   </div>
   <div class="col-sm-6">
        <select name="cidade" id="cidade" class="form-control form-b" required>
            <option value="" class="option_cad">* CIDADE</option>
        </select>
   </div>

My PHP

public function pedidos() {
    if($this->form_validation->run('cadastro/pedidos')) {


            $orcamento = array(
                'nome'              => $this->input->post('nome'),
                'email'             => $this->input->post('email'),
                'telefone'          => $this->input->post('telefone'),
                'celular'           => $this->input->post('celular'),
                'id_cidade'         => $this->input->post('cidade'),
                'id_subcategoria'   => $this->input->post('subcategoria'),
                'descricao'         => $this->input->post('descricao'),
                'data_orcamento'    => date('Y-m-d H:i:s')
            );


            $idOrcamento = $this->superModel->insert('orcamento', $orcamento);


            $this->db->select('cidade.nome AS cidade');
            $this->db->join('cidade', 'cidade.id = orcamento.id_cidade');
            $this->db->where('id_orcamento', $idOrcamento);
            $endereco = $this->db->get('orcamento')->row();

            if($endereco) {
                $data['cidade'] = $endereco->cidade;
            }

            $this->session->set_userdata($data);
            $this->enviaEmailSossegue($data);

    $this->layout->view('confirmacao-pedido', $this->data);
    } else {

        $clausulas = array(
        'order' => array(
            array(
                'campo' => 'ordem',
                'valor' => 'ASC'
            )
        )
    );

        $clausulas['order'] = array(
        array(
            'campo' => 'categoria'
        )
    );

    $this->data['listaCategorias'] = $this->superModel->select('categoria', $clausulas);


    $this->data['estados'] = $this->superModel->select('estado');
        $data = array(
            'select' => 'valor, complemento',
            'condicoes' => array(
                array(
                    'campo' => 'campo',
                    'valor' => 'cadastro_bloco',
                    'like' => 'after',
                    'escape' => true
                )
            ),
            'order' => array(
                array(
                    'campo' => 'campo'
                )
            )
        );

        $this->data['textoBlocos'] = $this->superModel->select('configuracoes', $data);

        insertTag('js', 'jquery.maskedinput.min.js', $this->data);
        $this->layout->view('pedidos', $this->data);
    }

}
  • 3

    Like you’re doing, post your code.

  • 3

    Post the code that displays cities.

  • 3

    Put us the PHP code and select

1 answer

3


A solution :

select cities do something like

select cod,nome from cidades
order by (case when nome = 'Ribeirão Preto' then 0 else 1 end).cidades.nome

Browser other questions tagged

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