help query mysql does not return anything

0

Good I have this query but it does not return anything to me, only it returns to me when all the data are complete, if only I put a city does not return nothing try to thunder the query but even so it does not return anything, as q does not enter any advice?

Model

$and1 = '';
        $and2 = '';
        $and3 = '';
        $and4 = '';
        $and5 = '';

        $month1 = date('m');
        $year1 = date('Y');
        $fecha3 = date('Y-m-d', mktime(0,0,0, $month1, 1, $year1));

      $month2 = date('m');
      $year2 = date('Y');
      $day2 = date("d", mktime(0,0,0, $month2+1, 0, $year2)); 
      $fecha4 = date('Y-m-d', mktime(0,0,0, $month2, $day2, $year2));


if($fecha == '' and $fecha2 == ''){$and3 = 'AND val.val_id_asessor = '.$asesor.'';}

elseif ($fecha2 == '' && $asesor == '') {$and1 = 'AND val.val_fecha_registro >= "'.$fecha.'"';}

elseif ($asesor == '') {$and2 = 'AND val.val_fecha_registro between "'.$fecha.'" and "'.$fecha2.'"';}

elseif ($asesor == '' && $fecha == '') {$and4 = 'AND val.val_fecha_registro >= "'.$fecha2.'"';}

elseif($fecha2 == ''){$and5 = 'AND val.val_fecha_registro >= "'.$fecha.'" AND val.val_id_asessor = "'.$asesor.'"';}

elseif($ciudad == 'Todos' && $asessor == '' && $fecha == '' && $fecha2 == ''){

                    $qry = 'SELECT val.idval_valuaciones AS id,
                   val.val_fecha_registro AS fecha_registro,
                   val.val_nom_contacto AS contacto_nombre,
                   val.val_modelo AS modelo,
                   val.val_aps AS aps,
                   val.val_id_asessor AS ase,
                   val.val_ciudad AS ciudad,
                   CONCAT( u.hus_nombre, " ", u.hus_apellido ) AS asessor
                   FROM valuaciones_seminuevos AS val
                   LEFT JOIN huser u ON u.hus_IDhuser = val.val_id_asessor
                   WHERE val.val_fecha_registro between "'.$fecha3.'" and "'.$fecha4.'"';

                return $this->db->query($qry)->result();

}elseif ($ciudad == 'Ti' || $ciudad == 'Me' || $ciudad == 'Af' && $asesor == '' && $fecha == '' && $fecha2 == '') {

                $qry = 'SELECT val.idval_valuaciones AS id,
                   val.val_fecha_registro AS fecha_registro,
                   val.val_nom_contacto AS contacto_nombre,
                   val.val_modelo AS modelo,
                   val.val_aps AS aps,
                   val.val_id_asessor AS ase,
                   val.val_ciudad AS ciudad,
                   CONCAT( u.hus_nombre,"", u.hus_apellido ) AS asessor
                   FROM valuaciones_seminuevos AS val
                   LEFT JOIN huser u ON u.hus_IDhuser = val.val_id_asessor
                   WHERE val.val_fecha_registro between "'.$fecha3.'" and "'.$fecha4.'"
                   AND val.val_ciudad = "'.$ciudad.'"';

                return $this->db->query($qry)->result();

}elseif($asesor == '' && $fecha == '' && $fecha2 == ''){

                    $qry = 'SELECT val.idval_valuaciones AS id,
                   val.val_fecha_registro AS fecha_registro,
                   val.val_nom_contacto AS contacto_nombre,
                   val.val_modelo AS modelo,
                   val.val_aps AS aps,
                   val.val_id_asessor AS ase,
                   val.val_ciudad AS ciudad,
                   CONCAT( u.hus_nombre," ", u.hus_apellido ) AS asessor
                   FROM valuaciones_seminuevos AS val
                   LEFT JOIN huser u ON u.hus_IDhuser = val.val_id_asessor
                WHERE val.val_ciudad = "'.$ciudad.'"
                AND val.val_id_asessor = "'.$asesor.'"
                AND val.val_fecha_registro between "'.$fecha.'"and "'.$fecha2.'"';

                return $this->db->query($qry)->result();
}else{

        $qry = 'SELECT val.idval_valuaciones AS id,
                   val.val_fecha_registro AS fecha_registro,
                   val.val_nom_contacto AS contacto_nombre,
                   val.val_modelo AS modelo,
                   val.val_aps AS aps,
                   val.val_id_asessor AS ase,
                   val.val_ciudad AS ciudad,
                   CONCAT( u.hus_nombre,"", u.hus_apellido ) AS asessor
                   FROM valuaciones_seminuevos AS val
                   LEFT JOIN huser u ON u.hus_IDhuser = val.val_id_asessor
                WHERE val.val_ciudad = "'.$ciudad.'"
                '.$and1.'
                '.$and2.'
                '.$and3.'
                '.$and4.'
                '.$and5.'';

        return $this->db->query($qry)->result();}

Controller, loq only brand is

"Status" => false,
                                "Message" => "GET"

$ciudad = $_POST['ciudad'];
        $asesor = $_POST['asesor'];
        $fecha = $_POST['fecha'];
        $fecha2 = $_POST['fecha2'];


        $data = $this->valuaciones->busqueda_valuaciones($ciudad,$fecha,$fecha2,$asesor);
        if ($data) {

                        $this->output
                        ->set_content_type('application/json')
                        ->set_status_header(200)
                        ->set_output(json_encode(array(
                            "Status" => true,
                            "Message" => "GET",
                            "Data" =>  $data
                        )));
                    }else
                    { 
                         $this->output
                        ->set_content_type('application/json')
                        ->set_status_header(500)
                        ->set_output(json_encode(array(
                            "Status" => false,
                            "Message" => "GET"

                        )));
                    }
    
asked by Juan Jose 02.11.2018 в 19:23
source

1 answer

1

Your code has some errors, in a matter of if , you need to separate them by segments, since you are all joining in one, and for that reason they will never enter your queries:

$and1 = ''; 
$and2 = ''; 
$and3 = ''; 
$and4 = ''; 
$and5 = ''; 

$month1 = date('m'); 
$year1 = date('Y'); 
$fecha3 = date('Y-m-d', mktime(0,0,0, $month1, 1, $year1)); 

$month2 = date('m'); 
$year2 = date('Y'); 
$day2 = date("d", mktime(0,0,0, $month2+1, 0, $year2)); 
$fecha4 = date('Y-m-d', mktime(0,0,0, $month2, $day2, $year2)); 


if ($asesor == null) { 
$asesor = ''; 
} 
if ($ciudad == null) { 
$ciudad = ''; 
} 
if ($fecha == null) { 
$fecha = ''; 
} 
if ($fecha2 == null) { 
$fecha2 = ''; 
} 

if($fecha == '' && $fecha2 == ''){$and3 = 'AND val.val_id_asessor = '.$asesor.'';} 

elseif ($fecha2 == '' && $asesor == '') {$and1 = 'AND val.val_fecha_registro >= "'.$fecha.'"';} 

elseif ($asesor == '' && $fecha != '') {$and2 = 'AND val.val_fecha_registro between "'.$fecha.'" and "'.$fecha2.'"';} 

elseif ($asesor == '' && $fecha == '') {$and4 = 'AND val.val_fecha_registro >= "'.$fecha2.'"';} 

elseif($fecha2 == ''){$and5 = 'AND val.val_fecha_registro >= "'.$fecha.'" AND val.val_id_asessor = "'.$asesor.'"';} 


if($ciudad == 'Todos' && $asesor == '' && $fecha == '' && $fecha2 == ''){ 

$qry = 'SELECT val.idval_valuaciones AS id, 
val.val_fecha_registro AS fecha_registro, 
val.val_nom_contacto AS contacto_nombre, 
val.val_modelo AS modelo, 
val.val_aps AS aps, 
val.val_id_asessor AS ase, 
val.val_ciudad AS ciudad, 
val.val_servicio AS servicio, 
val.val_seminuevos AS seminuevo, 
val.val_ventas AS ventas, 
val.val_gte_ven AS gerente, 
val.val_contabilidad as conta, 
CONCAT( u.hus_nombre, " ", u.hus_apellido ) AS asessor 
FROM valuaciones_seminuevos AS val 
LEFT JOIN huser u ON u.hus_IDhuser = val.val_id_asessor 
WHERE val.val_fecha_registro between "'.$fecha3.'" and "'.$fecha4.'"'; 

return $this->db->query($qry)->result(); 

}elseif (($ciudad == 'Tijuana' || $ciudad == 'Mexicali' || $ciudad == 'Ensenada') && $asesor == '' && $fecha == '' && $fecha2 == '') { 

$qry = 'SELECT val.idval_valuaciones AS id, 
val.val_fecha_registro AS fecha_registro, 
val.val_nom_contacto AS contacto_nombre, 
val.val_modelo AS modelo, 
val.val_aps AS aps, 
val.val_id_asessor AS ase, 
val.val_ciudad AS ciudad, 
val.val_servicio AS servicio, 
val.val_seminuevos AS seminuevo, 
val.val_ventas AS ventas, 
val.val_gte_ven AS gerente, 
val.val_contabilidad as conta, 
CONCAT( u.hus_nombre,"", u.hus_apellido ) AS asessor 
FROM valuaciones_seminuevos AS val 
LEFT JOIN huser u ON u.hus_IDhuser = val.val_id_asessor 
WHERE val.val_fecha_registro between "'.$fecha3.'" and "'.$fecha4.'" 
AND val.val_ciudad = "'.$ciudad.'"'; 

return $this->db->query($qry)->result(); 

}elseif($ciudad == 'Todos' && $fecha == '' && $fecha2 == '') 
{ 
$qry = 'SELECT val.idval_valuaciones AS id, 
val.val_fecha_registro AS fecha_registro, 
val.val_nom_contacto AS contacto_nombre, 
val.val_modelo AS modelo, 
val.val_aps AS aps, 
val.val_id_asessor AS ase, 
val.val_ciudad AS ciudad, 
val.val_servicio AS servicio, 
val.val_seminuevos AS seminuevo, 
val.val_ventas AS ventas, 
val.val_gte_ven AS gerente, 
val.val_contabilidad as conta, 
CONCAT( u.hus_nombre,"", u.hus_apellido ) AS asessor 
FROM valuaciones_seminuevos AS val 
LEFT JOIN huser u ON u.hus_IDhuser = val.val_id_asessor 
WHERE val.val_id_asessor = "'.$asesor.'"'; 


return $this->db->query($qry)->result(); 

} else { 
$qry = 'SELECT val.idval_valuaciones AS id, 
val.val_fecha_registro AS fecha_registro, 
val.val_nom_contacto AS contacto_nombre, 
val.val_modelo AS modelo, 
val.val_aps AS aps, 
val.val_id_asessor AS ase, 
val.val_ciudad AS ciudad, 
val.val_servicio AS servicio, 
val.val_seasdminuevos AS seminuevo, 
val.val_ventas AS ventas, 
val.val_gte_ven AS gerente, 
val.val_contabilidad as conta, 
CONCAT( u.hus_nombre,"", u.hus_apellido ) AS asessor 
FROM valuaciones_seminuevos AS val 
LEFT JOIN huser u ON u.hus_IDhuser = val.val_id_asessor 
WHERE val.val_ciudad = "'.$ciudad.'" 
'.$and1.' 
'.$and2.' 
'.$and3.' 
'.$and4.' 
'.$and5.''; 

return $this->db->query($qry)->result(); 
}

As we talked about in the chat, this is the correct answer.

    
answered by 02.11.2018 / 23:19
source