Solved

Simple PHP & MySQL search engine

Posted on 2002-07-11
5
229 Views
Last Modified: 2006-11-17
Hi

I have a MySQL Database that contains a list of jobs (with unique job numbers etc).  I wish to be able to search on the job number, on the name of the person that completed said job and for who it was for.

So i would need three different pieces of search code - only problem is, I'm not sure where to start.

Can anyone help?

Thanks.
0
Comment
Question by:RonnieBrown
  • 4
5 Comments
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 7146543
Something like this may help ...


<input name="IdFrom" input="text">
<input name="IdTo" input="text">
<input name="WorkerFrom" input="text">
<input name="WorkerTo" input="text">
<input name="CompanyFrom" input="text">
<input name="CompanyFrom" input="text">


<?php

$iWhereCount = 0;
$aWheres[0] = "";

if (isset($_POST["IdFrom"])) { $aWheres[$iWhereCount++] = "ID >= '{$_POST["IdFrom"]}'"; }
if (isset($_POST["IdTo"])) { $aWheres[$iWhereCount++] = "ID <= '{$_POST["IdTo"]}'"; }
if (isset($_POST["WorkerFrom"])) { $aWheres[$iWhereCount++] = "WORKER >= '{$_POST["WorkerFrom"]}'"; }
if (isset($_POST["WorkerTo"])) { $aWheres[$iWhereCount++] = "WORDER <= '{$_POST["WorkerTo"]}'"; }
if (isset($_POST["CompanyFrom"])) { $aWheres[$iWhereCount++] = "COMPANY >= '{$_POST["CompanyFrom"]}'"; }
if (isset($_POST["CompanyTo"])) { $aWheres[$iWhereCount++] = "COMPANY <= '{$_POST["CompamyTo"]}'"; }

$sWhere = join(" AND ",$aWheres);

// Filter for SQL Query now in $sWhere;

?>

Maybe.
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 7146593
And I should have tested THAT!!!

Try ...

<form action="where.php" method="GET">
<input name="IdFrom" type="text">
<input name="IdTo" type="text">
<input name="WorkerFrom" type="text">
<input name="WorkerTo" type="text">
<input name="CompanyFrom" type="text">
<input name="CompanyTo" type="text">
<input type="submit">
</form>

<?php
$iWhereCount = 0;
$aWheres[0] = "";

function AddToWhere($sATW)
{
global $iWhereCount,$aWheres;

if (isset($_GET[$sATW . "From"]))
     {
     if (strlen($_GET[$sATW . "From"]) > 0)
          {
          $aWheres[$iWhereCount++] = "$sATW >= '{$_GET[$sATW . "From"]}'";
          }
     }
if (isset($_GET[$sATW . "To"]))
     {
     if (strlen($_GET[$sATW . "To"]) > 0)
          {
          $aWheres[$iWhereCount++] = "$sATW <= '{$_GET[$sATW . "To"]}'";
          }
     }
}

AddToWhere("Id");
AddToWhere("Worker");
AddToWhere("Company");

$sWhere = "WHERE " . join(" AND ",$aWheres);

// Filter for SQL Query now in $sWhere;

echo "$sWhere<br>$iWhereCount";
?>

0
 

Author Comment

by:RonnieBrown
ID: 7146680
Would I put my SQL Query in the bit where you've written

"// Filter for SQL Query now in $sWhere;" ?
0
 
LVL 40

Accepted Solution

by:
Richard Quadling earned 100 total points
ID: 7146689
More or less.

If you wanted to show ID,Worker,Company,Link_to_details from a table JOBS then ...

SELECT ID,WORKER,COMPANY,LINK_TO_DETAILS
FROM JOB
WHERE goes here
ORDER BY ID

sort of thing.

0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 7146690
$sQuery = "SELECT ID,WORKER,COMPANY,LINK_TO_DETAILS FROM JOB $sWhere ORDER BY ID";

Might make more sense.


0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

820 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