How to count, sort and store in variables, the data that I bring from the BD?

0

Good morning, I have a small query ... I have this table in Mysql

This is my query:

SELECT  at_ce.condicion, 
        COUNT(at_ce.id) AS cantidad_condicion, 
        gpr.grupo_programa, 
        COUNT(gpr.id_programa) AS cantidad_grupo_programa

What I need is to count all the "RESERVED", "CONFIRMED", "I DO NOT PASS CONSULTATIONS" and in group program "HEALTH CLUB" "INSTITUTIONAL / PRIVATE" "CIA INSURANCE / CONVENTIONS".

Something like that

$ Reserved = Quantity_reserved; $ Queries = number_consults; ...

I wanted to do it first with a FOR and then a switch something like that

$c = count($resultado);
    for($i = 0, $i < $c; $i++){
        switch($resultado[$i]->GRUPO2){
            case 'CONDICION':
            switch($resultado[$i]->GRUPO1){
                case('RESERVADO'): SUMA DE TODOS LOS RESERVADOS;
                break;
            };
            break;
            case 'GRUPO_PROGRAMA':
            switch($resultado[$i]->GRUPO1){
                case('CLUB DE LA SALUD'): SUMA DE TODOS LOS CLUB DE LA SALUD;
                break;
            };
            break;
        }
    }

The SQL query I do is the following:

SELECT at_ce.condicion, COUNT(at_ce.id) AS cantidad_condicion, gpr.grupo_programa, COUNT(gpr.id_programa) AS cantidad_grupo_programa 

But I know how to set its variables, someone could please guide me. Thank you very much in advance.

    
asked by Jean Paul 17.06.2016 в 15:33
source

2 answers

1

First of all I do not see any query in the code, I intuit that the query will be stored in $ result.

I would recommend you filter directly in the query that you make, for example:

$reservados = tu_funcion_de_consultar("SELECT * FROM {tabla} WHERE condicion = 'RESERVADO'");
$confirmados = tu_funcion_de_consultar("SELECT * FROM {tabla} WHERE condicion = 'CONFIRMADOS'");
$no_paso_consultas = tu_funcion_de_consultar("SELECT * FROM {tabla} WHERE condicion = 'NO PASO CONSULTAS'");

I put your_funcion_de_consultar because personally I prefer to build my own function to perform queries to the database instead of the functions of php.

Did it help you for something?

I'm still here to follow the thread.

If just what you do not want is to make several queries, you could try this, I put it full.

$consulta = ('SELECT  at_ce.condicion, 
              COUNT(at_ce.id) AS cantidad_condicion, gpr.grupo_programa, 
              COUNT(gpr.id_programa) AS cantidad_grupo_programa
              FROM at_cons_externa at_ce
              INNER JOIN gen_programa gpr ON at_ce.id_programa = gpr.id_programa
              GROUP BY at_ce.condicion, gpr.grupo_programa');

$devuelve = array();

#Realizamos la consulta y verificamos errores
if(!$respuesta = $db->query($consulta)){
    die('Hubo un error al consultar la base de datos: [' . $db->error . '] <br>' . $consulta);
}

while ($row = $respuesta->fetch_assoc()){
    if ($array){
        $devuelve[] = $row;
    } else {
        $devuelve = implode("",$row);
    }
}

foreach ($devuelve as $dato){
    if ($dato['condicion'] = "RESERVADOS"){ 
        array_push($reservados, $dato);
    } elseif ($dato['condicion'] = "CONFIRMADOS"){
        array_push($confirmados, $dato);
    }

}
    
answered by 17.06.2016 в 15:53
1

I found the solution ... well I'll leave it if someone needs it.

    $Confirmado = 0;
    $Reservado = 0;
    $No_Consulta = 0;
    $Observado = 0;

    $Institucional = 0;
    $Club_salud = 0;
    $Madre_ni = 0;
    $Cia_seg = 0;

    foreach ($query as $key) {
        for ($i=0; $i < count($key->anio) ; $i++) { 
            switch ($key->condicion) {
                case 'CONFIRMADO':
                    $Confirmado = $Confirmado + $key->cantidad_condicion;
                    break;
                case 'RESERVADO':
                    $Reservado = $Reservado + $key->cantidad_condicion;
                    break;
                case 'NO PASO CONSULTA':
                    $No_Consulta = $No_Consulta + $key->cantidad_condicion;
                    break;
                case 'OBSERVADO':
                    $Observado = $Observado + $key->cantidad_condicion;
                    break;
                default:
                    echo "";
                    break;
            }
            switch ($key->grupo_programa) {
                case 'INSTITUCIONAL/PRIVADOS':
                    $Institucional = $Institucional + $key->cantidad_grupo_programa;
                    break;
                case 'CLUB DE LA SALUD':
                    $Club_salud = $Club_salud + $key->cantidad_grupo_programa;
                    break;
                case 'MADRE NIÑO':
                    $Madre_ni = $Madre_ni + $key->cantidad_grupo_programa;
                    break;
                case 'CIA. SEGUROS/CONVENIOS':
                    $Cia_seg = $Cia_seg + $key->cantidad_grupo_programa;
                    break;
                default:
                    echo "";
                    break;
            }
        }
    }

    echo $Confirmado;

Thanks for your time.

    
answered by 17.06.2016 в 17:59