Load values in drop-down select with MySQL PHP and HTML

1

I have a table in my database called "client types", with 3 fields: user, type, discount.

Class code Clients :

<?php
/**
 * Clase TiposClientes.
 */
class TiposClientes {
    private $idusuario;
    private $tipo;
    private $descuento;

    /**
     * Constructor de la clase TiposClientes.
     * @param type $row
     */
    public function __construct($row){
        $this->idusuario = $row["idusuario"];
        $this->tipo = $row["tipo"];
        $this->descuento = $row["descuento"];
    }

    function getIdusuario() {
        return $this->idusuario;
    }

    function getTipo() {
        return $this->tipo;
    }

    function getDescuento() {
        return $this->descuento;
    }

    function setIdusuario($idusuario) {
        $this->idusuario = $idusuario;
    }

    function setTipo($tipo) {
        $this->tipo = $tipo;
    }

    function setDescuento($descuento) {
        $this->descuento = $descuento;
    }


    function mostrarTiposClientes(){
        echo $this->idusuario;
        echo $this->tipo;
        echo $this->descuento;
    }
}

In a .php file I make the queries I want in this case I want to extract all TYPES of client types .

static public function obtenerTiposClientes(){
        //Realizamos la consulta.
        $ejecucion = self::Conexion();
        $registro = $ejecucion->query("SELECT * FROM tiposclientes;");
        return $registro;
    }

And those guys I want to add them in a combobox (drop-down menu), why do not you load them on the select?

HTML + PHP code to load the "type" in the select:

<label>Tipos: <select name = "tipo">
                    <?php
                    $datos = BD::obtenerTiposClientes();
                    foreach($datos as $x){
                        echo "<option value='".$x['tipo']."'>".$x['tipo']."</option>";
                    }
                    ?> 
                </select>
                </label>
    
asked by omaza1990 02.11.2017 в 21:41
source

3 answers

0

In the end I solved it in a simpler way yet, the problem was that I went through the foreach without having values and could not get the columns I wanted. I explain myself with the key method:

static public function obtenerTiposClientes(){
   //Realizamos la consulta.
   $ejecucion = self::Conexion();
   $registro = $ejecucion->query("SELECT * FROM tiposclientes;");
   return $registro;
}

In this query I collect ALL the information from the table, and I save it in the $ records variable, then to the other to treat the values with the "foreach" I only select those with the "type" parameter.

include "Clases/BD.php";
$datos = BD::obtenerTiposClientes();
foreach($datos as $x){
   echo "<option value=\'".$x['tipo']."\'>".$x['tipo']."</option>";
}
    
answered by 06.11.2017 / 18:43
source
0

You have a serious error in the echo where you are printing the <option> , it should be like this:

echo "<option value='".$x['tipo']."'>".$x['tipo']."</option>";
    
answered by 02.11.2017 в 21:49
0

Apparently your problem is in the foreach as the error says. It may be because of the way you show options .

Try printing the options in the following way:

echo '<option value=\"'.$x[\"tipo\"].'\">'.$x[\"tipo\"].'</option>';

Or if it does not work for you then try it in this other way:

echo "<option value=\'".$x['tipo']."\'>".$x['tipo']."</option>";

I hope you serve, greetings!

    
answered by 03.11.2017 в 20:16