Solved

DreamWeaver CS3 database searching

Posted on 2009-07-06
9
174 Views
Last Modified: 2012-05-07
I have built a search form for my database that is functioning.  I have a simple form to use in the search.  I need to be able to search for multiple terms passed from one text field from the search form.  
For example:  search for   apples oranges
the result should be anything in the column that has both 'apples' and 'oranges', or whatever terms are typed in with a space between them.

I have had problems trying to get the results to behave when using a Date field in the form as well.
<?php require_once('../Connections/WCI.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_SearchResults = 15;
$pageNum_SearchResults = 0;
if (isset($_GET['pageNum_SearchResults'])) {
  $pageNum_SearchResults = $_GET['pageNum_SearchResults'];
}
$startRow_SearchResults = $pageNum_SearchResults * $maxRows_SearchResults;
 
$varcontact_SearchResults = "%";
if (isset($_GET['Contact'])) {
  $varcontact_SearchResults = $_GET['Contact'];
}
$varlong_SearchResults = "%";
if (isset($_GET['Long_Desc'])) {
  $varlong_SearchResults = $_GET['Long_Desc'];
}
mysql_select_db($database_WCI, $WCI);
$query_SearchResults = sprintf("SELECT `Date`, Hours, Contact, Long_Desc FROM Results WHERE Contact LIKE %s AND Long_Desc LIKE %s ORDER BY `Date` DESC", GetSQLValueString("%" . $varcontact_SearchResults . "%", "text"),GetSQLValueString("%" . $varlong_SearchResults . "%", "text"));
$query_limit_SearchResults = sprintf("%s LIMIT %d, %d", $query_SearchResults, $startRow_SearchResults, $maxRows_SearchResults);
$SearchResults = mysql_query($query_limit_SearchResults, $WCI) or die(mysql_error());
$row_SearchResults = mysql_fetch_assoc($SearchResults);
 
if (isset($_GET['totalRows_SearchResults'])) {
  $totalRows_SearchResults = $_GET['totalRows_SearchResults'];
} else {
  $all_SearchResults = mysql_query($query_SearchResults);
  $totalRows_SearchResults = mysql_num_rows($all_SearchResults);
}
$totalPages_SearchResults = ceil($totalRows_SearchResults/$maxRows_SearchResults)-1;
 
$queryString_SearchResults = "";
if (!empty($_SERVER['QUERY_STRING'])) {
  $params = explode("&", $_SERVER['QUERY_STRING']);
  $newParams = array();
  foreach ($params as $param) {
    if (stristr($param, "pageNum_SearchResults") == false && 
        stristr($param, "totalRows_SearchResults") == false) {
      array_push($newParams, $param);
    }
  }
  if (count($newParams) != 0) {
    $queryString_SearchResults = "&" . htmlentities(implode("&", $newParams));
  }
}
$queryString_SearchResults = sprintf("&totalRows_SearchResults=%d%s", $totalRows_SearchResults, $queryString_SearchResults);

Open in new window

0
Comment
Question by:prostang
  • 5
  • 4
9 Comments
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 24788707
>>  I need to be able to search for multiple terms passed from one text field from the search form.  

You can't easily do this with the built-in behaviors.  You either need to know how to write some PHP code that splits the input string into separate words and builds an OR clause for each one or you need to invest in an extension for DW that automates this.
0
 

Author Comment

by:prostang
ID: 24789030
I know how to write some PHP.  DreamWeaver makes it so much easier.
0
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 24789049
So which solution would like guidance on?  
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:prostang
ID: 24789684
I don't really care about the date issue.  I really would like to break down the search field.
0
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 24790208
Do you want me to recommend an extension or give you direction on the functions in PHP and you can attempt to code it yourself?
0
 

Author Comment

by:prostang
ID: 24791562
if you can recommend and extension that will work.  I am running out of time to tackle the coding at this point.  Sorry for the delay in my post.
0
 
LVL 70

Accepted Solution

by:
Jason C. Levine earned 500 total points
ID: 24791590
DataAssist from WebAssist has a wizard that will build a very advanced search routine.  Multiple form fields, multiple parameters are all possible:

http://www.webassist.com/software/dataassist/
0
 

Author Comment

by:prostang
ID: 24791598
thanks.  How much work is it to do it manually?
0
 
LVL 70

Assisted Solution

by:Jason C. Levine
Jason C. Levine earned 500 total points
ID: 24791622
Depends on you, really.

The way to go is to use the explode() function on the input string to get each word as an element of an array:

http://us2.php.net/manual/en/function.explode.php

You then step through the array and build the WHERE clause in the query with OR statements, one for each element in the array.  A similar example is here:

http://www.acuras.co.uk/articles/2-php-multi-word-mysql-search-algorithm-and-output
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I have been using r1soft Continuous Data Protection (http://www.r1soft.com/linux-cdp/) for many years now with the mySQL Addon and wanted to share a trick I have used several times. For those of us that don't have the luxury of using all transact…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question