Jump to content

Search field help!


flashguy82

Recommended Posts

I have a search field linking to a different page for results, but if the user types nothing in the search field it comes up with every result in the database, this i don't want to happen. Could anyone tweak my code so if the search field is empty the results are also empty? or tell me how to achieve this? thank you.

 

<?php require_once('Connections/z3phones.php'); ?>
<?php
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"];

$maxRows_rsSearch = 5;
$pageNum_rsSearch = 0;
if (isset($_GET['pageNum_rsSearch'])) {
  $pageNum_rsSearch = $_GET['pageNum_rsSearch'];
}
$startRow_rsSearch = $pageNum_rsSearch * $maxRows_rsSearch;

mysql_select_db($database_z3phones, $z3phones);
$query_rsSearch = "SELECT ProductID, ProductName, image2, Description1 FROM z3phones WHERE Online = 1 AND ProductName LIKE '%$search%' OR Description1 LIKE '%$search%' OR Description2 LIKE '%$search%' ORDER BY z3phones.ProductName ASC";
$query_limit_rsSearch = sprintf("%s LIMIT %d, %d", $query_rsSearch, $startRow_rsSearch, $maxRows_rsSearch);
$rsSearch = mysql_query($query_limit_rsSearch, $z3phones) or die(mysql_error());
$row_rsSearch = mysql_fetch_assoc($rsSearch);

if (isset($_GET['totalRows_rsSearch'])) {
  $totalRows_rsSearch = $_GET['totalRows_rsSearch'];
} else {
  $all_rsSearch = mysql_query($query_rsSearch);
  $totalRows_rsSearch = mysql_num_rows($all_rsSearch);
}
$totalPages_rsSearch = ceil($totalRows_rsSearch/$maxRows_rsSearch)-1;

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

 

EDIT BY SA: Please use the [code][/code] tags!

Link to comment
https://forums.phpfreaks.com/topic/62170-search-field-help/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.