Foros del Web » Programando para Internet » Javascript » Frameworks JS »

Funciona bien pero es demasiado largo

Estas en el tema de Funciona bien pero es demasiado largo en el foro de Frameworks JS en Foros del Web. Hola foreros, gracias por leer mi nota, resulta que estuve haciendo una busqueda con Ajax y Php, pero en el php que llama la funcion ...
  #1 (permalink)  
Antiguo 22/01/2009, 13:51
 
Fecha de Ingreso: diciembre-2008
Ubicación: http://www.solucionesrios.tk/
Mensajes: 413
Antigüedad: 15 años, 4 meses
Puntos: 19
Funciona bien pero es demasiado largo

Hola foreros, gracias por leer mi nota, resulta que estuve haciendo una busqueda con Ajax y Php, pero en el php que llama la funcion ajax tuve que realizar demasiados if, no se si conocen alguna manera de resumirlos, pues creo que son bastantes, a continuación el código:

Formulario Php llamado

reporte_usuarios.php

Código PHP:
<html>
<
head>
<
style type="text/css">
<!--
body,td,th {
    
color#FFFFFF;
}
body {
    
background-color#3cc35d;
    
background-imageurl(Fondo.jpg);
    
background-repeatrepeat-x;
}
div.page

writing-modetb-rl;
height10%;
margin800%;
}
-->
</
style>
<
title>Sistema de Inventario</title>
</
head>
<
body>
<
script language="javascript" type="text/javascript">

function 
formValidator(){
// Make quick references to our fields
var cedula document.getElementById("cedula");
var 
nombre document.getElementById("nombre");
var 
cargo document.getElementById("cargo");

// Check each input in the order that it appears in the form!
if(isNumeric(cedula"Por favor sólo ingrese números para la cédula")){
return 
true;
}
return 
false;
}

function 
notEmpty(elemhelperMsg)
{
    if(
elem.value.length == 0)
    {
        
alert(helperMsg);
        
elem.focus(); // Devuelvo al usuario al input
        
return false;
    }
    return 
true;
}



function 
isNumeric(elemhelperMsg)
{
    var 
numericExpression = /^[0-9]+$/;
    if(
elem.value.match(numericExpression))
    {
        return 
true;
    }
    else
    {
        
alert(helperMsg);
        
elem.focus();
        return 
false;
    }
}

function 
isNotNumeric(elemhelperMsg)
{
    var 
numericExpression = /^[0-9]+$/;
    if(
elem.value.match(numericExpression))
    {
        
alert(helperMsg);
        
elem.focus();
        return 
false;
    }
    else
    {
        return 
true;    
    }
}


function 
isAlphabet(elemhelperMsg)
{
    var 
alphaExp = /^[a-zA-Z]+$/;
    if(
elem.value.match(alphaExp))
    {
        return 
true;
    }
    else
    {
        
alert(helperMsg);
        
elem.focus();
        return 
false;
    }
}


<!-- 
//Browser Support Code
function ajaxFunction(){
    var 
ajaxRequest;  // The variable that makes Ajax possible!
    
    
try{
        
// Opera 8.0+, Firefox, Safari
        
ajaxRequest = new XMLHttpRequest();
    } catch (
e){
        
// Internet Explorer Browsers
        
try{
            
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (
e) {
            try{
                
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (
e){
                
// Something went wrong
                
alert("Your browser broke!");
                return 
false;
            }
        }
    }
    
// Create a function that will receive data sent from the server
    
ajaxRequest.onreadystatechange = function(){
        if(
ajaxRequest.readyState == 4){
            var 
ajaxDisplay document.getElementById('ajaxDiv');
            
ajaxDisplay.innerHTML ajaxRequest.responseText;
        }
    }
    var 
cedula document.getElementById('cedula').value;
    var 
nombre document.getElementById('nombre').value;
    var 
cargo document.getElementById('cargo').value;
    var 
queryString "?cedula=" cedula "&nombre=" nombre "&cargo=" cargo;
    
ajaxRequest.open("GET""reportar_usuario.php" queryStringtrue);
    
ajaxRequest.send(null); 
}

//-->
</script>



<form name='myForm'>
  <p>Cédula:  
    <input name="cedula" type='text' id='cedula' />
  </p>
  <p>
    Nombre: 
    <input name="nombre" type='text' id='nombre' />
    </p>
  <p>
    Cargo: 
    <input name="cargo" type='text' id='cargo' />  
    </p>
  <p>  </br>
    
  <input type='button' onclick='ajaxFunction()' value='Buscar' />
    </p>
</form>
<div id='ajaxDiv'>
  <p>Los resultados se mostraran aqu&iacute;</p>
</div>
</body>
</html> 
Y aca el php que llama el ajax llamado:

reportar_usuario.php

Código PHP:
<?php
include "conectarse.php";
    
// Retrieve data from Query String
$cedula $_GET['cedula'];
$nombre $_GET['nombre'];
$cargo $_GET['cargo'];
    
// Escape User Input to help prevent SQL Injection
$cedula mysql_real_escape_string($cedula);
$nombre mysql_real_escape_string($nombre);
$cargo mysql_real_escape_string($cargo);
    
//build query
    
$query "SELECT * FROM `responsable`" ;

if(!empty(
$cedula) and empty($nombre) and empty($cargo))
{
    
$query .= " WHERE `cedula` = '$cedula' ORDER BY `cedula`";
}
elseif(!empty(
$nombre) and empty($cedula) and empty($cargo))
{
    
$query .= " WHERE `nombre` LIKE '%$nombre%' ORDER BY `cedula`";
}
elseif(!empty(
$cargo) and empty($nombre) and empty($cedula))
{
    
$query .= " WHERE `cargo` LIKE '%$cargo%' ORDER BY `cedula`";
}
elseif(!empty(
$cargo) and empty($nombre) and empty($cedula))
{
    
$query .= " WHERE `cargo` LIKE '%$cargo%' ORDER BY `cedula`";
}
elseif(!empty(
$cedula) and !empty($nombre) and empty($cargo))
{
    
$query .= " WHERE `cedula` = '$cedula' AND `nombre` LIKE '%$nombre%' ORDER BY `cedula`";
}
elseif(!empty(
$cedula) and !empty($cargo) and empty($nombre))
{
    
$query .= " WHERE `cedula` = '$cedula' AND `cargo` LIKE '%$cargo%' ORDER BY `cedula`";
}
elseif(!empty(
$nombre) and !empty($cargo) and empty($cedula))
{
    
$query .= " WHERE `nombre` LIKE '%$nombre%' AND `cargo` LIKE '%$cargo%' ORDER BY `cedula`";
}
elseif(!empty(
$nombre) and !empty($cargo) and !empty($cedula))
{
    
$query .= " WHERE `cedula` = '%$cedula%' AND `nombre` LIKE '%$cedula%' AND `cargo` LIKE '%$cargo%' ORDER BY `cedula`";
}

$qry_result mysql_query($query) or die(mysql_error());

    
//Build Result String
$display_string "<table width=100% border=1 cellspacing=1 cellpadding=1>";
$display_string .= "<tr bgcolor=#009900 align=center>";
$display_string .= "<th>C&Eacute;DULA</th>";
$display_string .= "<th>NOMBRE</th>";
$display_string .= "<th>CARGO</th>";
$display_string .= "</tr>";

    
// Insert a new row in the table for each person returned
while($row mysql_fetch_array($qry_result)){
    
$display_string .= "<tr>";
    
$display_string .= "<td>$row[cedula]</td>";
    
$display_string .= "<td>$row[nombre]</td>";
    
$display_string .= "<td>$row[cargo]</td>";
    
$display_string .= "</tr>";
    
    
}
$display_string .= "</table>";
echo 
$display_string;
?>
  #2 (permalink)  
Antiguo 22/01/2009, 14:10
Avatar de foreverOdd  
Fecha de Ingreso: noviembre-2007
Ubicación: Caracas
Mensajes: 489
Antigüedad: 16 años, 5 meses
Puntos: 14
Respuesta: Funciona bien pero es demasiado largo

Hola

¿Por que simplemente no juntas todo?

simplemente usas el OR en lugar del AND

WHERE `cedula` like '%$cedula%' OR `nombre` LIKE '%$nombre%' OR `cargo` LIKE '%$cargo%' ORDER BY `cedula`"
__________________
My path is lit by my own fire, I only go where I desire
  #3 (permalink)  
Antiguo 23/01/2009, 07:36
 
Fecha de Ingreso: diciembre-2008
Ubicación: http://www.solucionesrios.tk/
Mensajes: 413
Antigüedad: 15 años, 4 meses
Puntos: 19
Respuesta: Funciona bien pero es demasiado largo

Hola amigo, lo hice como me recomendaste con algunas modificaciones en caso de que sea cadena pero me da Parse Error, a ver si encontramos que error tiene esta linea de codigo MySQL dentro de Php

Código PHP:
$query "SELECT * FROM `responsable` WHERE `cedula` like '%$cedula%' OR `nombre` LIKE '%$nombre%' OR `cargo` LIKE '%$cargo%' OR WHERE MATCH (`nombre`) AGAINST ('$nombre') OR WHERE MATCH (`cargo`) AGAINST ('$cargo') ORDER BY `cedula`" 
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 17:37.