Help with an option selected

0

In these select I want to load the corresponding information for both, but I also want the select countries and cities, the selected country and city that the user has registered in the database, for the moment both functions work for me perfectly showing the countries and then the cities according to the country you select, but now I want that if you are already registered in the bd, come selected, in each function I receive the id_pais and id_ciudad in the variables $GLOBALS , which are the id of the country and city that that user has registered in the bd, I hope you can help me

public function SelectPaisescontroller(){
    $id_pais=$GLOBALS['id_pais'];
    $respuesta = Datos::SelectPaisesModel("pais");
    echo '<select  class="form-control" name="id_pais" id="id_pais" value="'.$id_pais.'">';
    echo '<option value="'.$id_pais.'">'.$respuesta['PaisNombre'].'</option>';
        foreach ($respuesta as $row => $item){
        echo '<option value="'.$item['id_pais'].'">'.utf8_encode($item['PaisNombre']).'</option>';
        }
        echo '</select>';

    }

public function SelectCiudadescontroller($datos=null){

    $datosController = $datos;
    $id_ciudad=$GLOBALS['id_ciudad'];
    echo '<select  class="form-control" name="id_ciudad" id="id_ciudad" value="'.$id_ciudad.'">';
    echo '<option value="0">Seleccione</option>';

    $respuesta = Datos::SelectCiudadesModel($datosController, "ciudad");

    foreach ($respuesta as $row => $item) {

        echo '<option value="'.$item['id_ciudad'].'">'.utf8_encode($item['CiudadNombre']).'</option>';

        }
    echo '</select>';
    }
    
asked by jorgnv 20.08.2017 в 05:30
source

1 answer

0

I do not recommend the use of a Global variable, and even less so in classes since they find abstraction

In any case you can create a conditional and decide if it is selected or not:

<?php

public function SelectPaisescontroller()
{
    $id_pais = $GLOBALS['id_pais'];
    $respuesta = Datos::SelectPaisesModel("pais");
    // select no tiene atributo value
    echo '<select  class="form-control" name="id_pais" id="id_pais">';
    //echo '<option value="'.$id_pais.'">'.$respuesta['PaisNombre'].'</option>';
    echo '<option value="0">Seleccione</option>';
        foreach ($respuesta as $row => $item)
        {
            if($item['id_pais'] == $id_pais){
                echo '<option value="'.$item['id_pais'].'" selected>'.utf8_encode($item['PaisNombre']).'</option>';             
            }
            else{
                echo '<option value="'.$item['id_pais'].'">'.utf8_encode($item['PaisNombre']).'</option>';
            }
        }
        echo '</select>';
}

public function SelectCiudadescontroller($datos=null)
{
    $datosController = $datos;
    $id_ciudad = $GLOBALS['id_ciudad'];
    // select no tiene atributo value
    echo '<select  class="form-control" name="id_ciudad" id="id_ciudad">';
    echo '<option value="0">Seleccione</option>';

    $respuesta = Datos::SelectCiudadesModel($datosController, "ciudad");

    foreach ($respuesta as $row => $item)
    {
        if($item['id_ciudad'] == $id_ciudad){
            echo '<option value="'.$item['id_ciudad'].'" selected>'.utf8_encode($item['CiudadNombre']).'</option>';
        }
        else{
            echo '<option value="'.$item['id_ciudad'].'">'.utf8_encode($item['CiudadNombre']).'</option>';
        }
    }
    echo '</select>';
}
    
answered by 21.08.2017 в 00:35