Foros del Web » Programando para Internet » PHP » Zend »

Filtro/busqueda php

Estas en el tema de Filtro/busqueda php en el foro de Zend en Foros del Web. Hola para todos y gracias de antemano. Estoy intentando crear un buscador para mi tabla hecha con php/dreamweaver pero de momento no me funciona. Sencillamente ...
  #1 (permalink)  
Antiguo 16/02/2011, 18:43
 
Fecha de Ingreso: abril-2010
Mensajes: 52
Antigüedad: 14 años
Puntos: 0
Filtro/busqueda php

Hola para todos y gracias de antemano.

Estoy intentando crear un buscador para mi tabla hecha con php/dreamweaver pero de momento no me funciona. Sencillamente no responde, no hace nada.

He echo un form sencillo:

<form action="pagina.php" method="get" name="form1" id="form1">
<input name="busqueda" type="text" id="busqueda" value="<?php echo $_GET['busqueda']; ?>" />
<input name="btnBuscar" type="submit" id="btnBuscar" value="Buscar!" />
</form>

Luego el filtro:

if($_REQUEST["btnBuscar"]!="" && $_REQUEST["busqueda"]!="")
{

if(is_numeric($_REQUEST["busqueda"]))
$filtro="id=". $_REQUEST["busqueda"];
else
$filtro=" provincia like '%".$_REQUEST["busqueda"]."%' or precio like '%".$_REQUEST["busqueda"]."%'";
$mensaje=$filtro;

}else
{
$mensaje="<font color='red'>Escriba Algo</font>";
}

y luego los recordsets y detalles de la tabla

if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}

$currentPage = $_SERVER["PHP_SELF"];

if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}

mysql_select_db($database_cnxaventuras, $cnxaventuras);
$query_rsCategorias = "SELECT * FROM provincia ORDER BY provincia ASC";
$rsCategorias = mysql_query($query_rsCategorias, $cnxaventuras) or die(mysql_error());
$row_rsCategorias = mysql_fetch_assoc($rsCategorias);
$totalRows_rsCategorias = mysql_num_rows($rsCategorias);

mysql_select_db($database_cnxaventuras, $cnxaventuras);
$query_rsProductos = "SELECT * FROM producto ORDER BY precio ASC";
$rsProductos = mysql_query($query_rsProductos, $cnxaventuras) or die(mysql_error());
$row_rsProductos = mysql_fetch_assoc($rsProductos);
$totalRows_rsProductos = mysql_num_rows($rsProductos);

$maxRows_rsProductos = 3;
$pageNum_rsProductos = 0;
if (isset($_GET['pageNum_rsProductos'])) {
$pageNum_rsProductos = $_GET['pageNum_rsProductos'];
}
$startRow_rsProductos = $pageNum_rsProductos * $maxRows_rsProductos;

$colname_rsProductos = "-1";
if (isset($_POST['listaProvincia'])) {
$colname_rsProductos = $_POST['listaProvincia'];
}
mysql_select_db($database_cnxaventuras, $cnxaventuras);
$query_rsProductos = sprintf("SELECT * FROM producto WHERE familia = 'paintball' ORDER BY precio ASC", GetSQLValueString($colname_rsProductos, "text"));
$query_limit_rsProductos = sprintf("%s LIMIT %d, %d", $query_rsProductos, $startRow_rsProductos, $maxRows_rsProductos);
$rsProductos = mysql_query($query_limit_rsProductos, $cnxaventuras) or die(mysql_error());
$row_rsProductos = mysql_fetch_assoc($rsProductos);


if (isset($_GET['totalRows_rsProductos'])) {
$totalRows_rsProductos = $_GET['totalRows_rsProductos'];
} else {
$all_rsProductos = mysql_query($query_rsProductos);
$totalRows_rsProductos = mysql_num_rows($all_rsProductos);
}
$totalPages_rsProductos = ceil($totalRows_rsProductos/$maxRows_rsProductos)-1;

$queryString_rsProductos = "";
if (!empty($_SERVER['QUERY_STRING'])) {
$params = explode("&", $_SERVER['QUERY_STRING']);
$newParams = array();
foreach ($params as $param) {
if (stristr($param, "pageNum_rsProductos") == false &&
stristr($param, "totalRows_rsProductos") == false) {
array_push($newParams, $param);
}
}
if (count($newParams) != 0) {
$queryString_rsProductos = "&" . htmlentities(implode("&", $newParams));
}
}
$queryString_rsProductos = sprintf("&totalRows_rsProductos=%d%s", $totalRows_rsProductos, $queryString_rsProductos);
?>


Pero no hay manera.
Vuestra ayuda seria impagable.
  #2 (permalink)  
Antiguo 20/02/2011, 21:45
 
Fecha de Ingreso: febrero-2011
Ubicación: Capital Federal
Mensajes: 45
Antigüedad: 13 años, 2 meses
Puntos: 3
Respuesta: Filtro/busqueda php

A ver. En primer lugar, no deberías usar request. Es inseguro (http://devlog.info/2010/02/04/why-php-request-array-is-dangerous/)

deberías usar $_POST cuando estas usando un form con metodo post, y $_GET cuando lo usas en método get (en ese caso, las variables se mandan por url). Por mi parte, salvo para casos concretos, uso el form siempre con metodo post.

Después, esas funciones de abajo son bastante engorrosas. Vos lo que necesitás es buscar en campos indeterminados? Por qué usas una función tan rebuscada si lo que debe hacer es buscar a partir de cualquier criterio? Tal vez esté entendiendo mal...

Pero si entiendo bien, yo haría:

Código HTML:
Ver original
  1. <form action="pagina.php" method="get" name="form1" id="form1">
  2. <input name="busqueda" type="text" id="busqueda" value="<?php echo $_GET['busqueda']; ?>" />
  3. <input name="btnBuscar" type="submit" id="btnBuscar" value="Buscar!" />
  4. </form>

Código PHP:
Ver original
  1. if(isset($_POST["btnBuscar"]))
  2. {
  3.     if($_POST["busqueda"]!="") {
  4.           if(is_numeric($_POST["busqueda"])) {
  5.                   $sql="select * from producto where id=". $_POST["busqueda"];
  6.           }
  7.           else
  8.           {
  9.                   $sql="SELECT * FROM producto where provincia like '%".$_POST["busqueda"]."%' or precio like '%".$_POST["busqueda"]."%'";
  10.           }
  11.           $rs_obj=mysql_query($sql);
  12.           while($rs=mysql_fetch_array($sql)) {
  13.                  //ACA ARMAS LA TABLA
  14.           }
  15.      }
  16.      else
  17.     {
  18.            $mensaje="<font color='red'>Escriba Algo</font>";
  19.     }
  20. }

Esto no te sirve? Al menos, debería andar y simplificas el codigo. Saludos

Etiquetas: php
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 21:28.