Código:
$SueldoTotal = "SELECT SUM(E.Sueldo) FROM empleados E "; $BonoTaxiTotal = "SELECT SUM(REE.pago_evento) FROM rel_eventos_empleados REE "; $PagoExtraTotal = "SELECT SUM(PE.Cantidad) FROM pagos_extras PE "; $string = $vNombre."_".$vAPPA."_".$vAPMA."_".$vSUELDO."_".$vEXTRAS."_".$vTAXI."_".$vBONO; $cadena = explode("_",$string); $Nvals = array_filter($cadena); $countNvals = count($Nvals); //Siguiente codigo es el WHERE del Select de busqueda IF($countNvals>=1) {//1 $sql .= " WHERE "; $rela = "SELECT id_empleado FROM rel_eventos_empleados WHERE "; IF($Nvals[0]) {//2 $sql .= "E.nombre LIKE '%$Nvals[0]%' "; IF($countNvals>1 AND $Nvals[0]!=$Nvals[max(array_keys($Nvals))]) $sql .= " AND "; }//2 IF($Nvals[1]) {//3 $sql .= "E.Apellido_Paterno LIKE '%$Nvals[1]%' "; IF($countNvals>1 AND $Nvals[1]!=$Nvals[max(array_keys($Nvals))]) $sql .= " AND "; }//3 IF($Nvals[2]) {//4 $sql .= "E.Apellido_Materno LIKE '%$Nvals[2]%' "; IF($countNvals>1 AND $Nvals[2]!=$Nvals[max(array_keys($Nvals))]) $sql .= " AND "; }//4 IF($Nvals[3]) {//5 $SueldoTotal .= " WHERE E.sueldo = $Nvals[3]"; $BonoTaxiTotal .= ", empleados E WHERE E.id_empleado = REE.id_empleado AND E.sueldo = $Nvals[3]"; $PagoExtraTotal .= ", empleados E WHERE E.id_empleado = PE.id_empleado AND E.sueldo = $Nvals[3]"; $sql .= "E.Sueldo = $Nvals[3] "; IF($countNvals>1 AND $Nvals[3]!=$Nvals[max(array_keys($Nvals))]) $sql .= " AND "; }//5 IF($Nvals[4]) {//6 $IdPagoExtra = "SELECT id_empleado FROM pagos_extras WHERE cantidad = $Nvals[4]"; $resIdPagoExtra = mysql_query($IdPagoExtra); $rowIdPagoExtra = mysql_fetch_row($resIdPagoExtra); $SueldoTotal .= ", pagos_extras PE WHERE PE.cantidad = $Nvals[4]"; $BonoTaxiTotal .= ", pagos_extras PE WHERE PE.id_empleado = REE.id_empleado AND PE.cantidad = $Nvals[4]"; $PagoExtraTotal .= "WHERE PE.cantidad = $Nvals[4]"; $sql .= " E.id_empleado = $rowIdPagoExtra[0] "; if(!$resIdPagoExtra) {//7 //echo @mysql_errno($result); die('Invalid query(1): ' . mysql_error()); }//7 }//6 IF($Nvals[5] OR $Nvals[6]) {//8 IF(empty($Nvals[5])) $rela .= " pago_evento = $Nvals[6] "; ELSEIF(empty($Nvals[6])) $rela .= "pago_taxi = $Nvals[5] "; ELSE $rela .= "pago_taxi = $Nvals[5] AND pago_evento = $Nvals[6] "; IF(!empty($Nvals[5])) {//9 $SueldoTotal .= ", rel_eventos_empleados REE WHERE E.id_empleado = REE.id_empleado AND REE.pago_taxi = $Nvals[5]"; $BonoTaxiTotal .= " WHERE REE.pago_taxi = $Nvals[5]"; $PagoExtraTotal .= ", rel_eventos_empleados REE WHERE PE.id_empleado = REE.id_empleado AND REE.pago_taxi = $Nvals[5]"; }//9 IF(!empty($Nvals[6])) {//10 $SueldoTotal .= ", rel_eventos_empleados REE WHERE E.id_empleado = REE.id_empleado AND REE.pago_evento = $Nvals[6]"; $BonoTaxiTotal .= "WHERE REE.pago_evento = $Nvals[6]"; $PagoExtraTotal .= ", rel_eventos_empleados REE WHERE E.id_empleado = REE.id_empleado AND REE.pago_evento = $Nvals[6]"; }//10 }//8 $i=0; $BonTax = mysql_query($rela); while($rowBonTax = mysql_fetch_row($BonTax)) {//11 $sql .= "E.Id_empleado = $rowBonTax[0] "; $i++; if($i>0 and $i<mysql_num_rows($BonTax)) $sql .= " OR "; }//11 IF($Nvals>0 AND $Nvals <$Nvals[max(array_keys($Nvals))]) $sql .= " AND "; }//1//Termina WHERE del query principal de busqueda $result = mysql_query($sql); if(!$result) { echo "LA BUSQUEDA QUE ESTAS REALIZANDO NO ES FACTIBLE "; $sql = "SELECT E.id_empleado, E.Nombre, E.Apellido_Paterno, E.Apellido_Materno, E.Sueldo FROM empleados E "; $result = mysql_query($sql); $SueldoTotal = "SELECT SUM(E.Sueldo) FROM empleados E "; $BonoTaxiTotal = "SELECT SUM(REE.pago_evento) FROM eventos_empleados REE "; $PagoExtraTotal = "SELECT SUM(PE.Cantidad) FROM pagos_extras PE "; } $resPagoExtraTotal = mysql_query($PagoExtraTotal); $rowPagoExtraTotal = mysql_fetch_row($resPagoExtraTotal); $resBonoTaxiTotal = mysql_query($BonoTaxiTotal); $rowBonoTaxiTotal = mysql_fetch_row($resBonoTaxiTotal); $resSueldoTotal = mysql_query($SueldoTotal); $rowSueldoTotal = mysql_fetch_row($resSueldoTotal);