Solved

Simple PHP & MySQL search engine

Posted on 2002-07-11
5
200 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:RQuadling
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:RQuadling
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:
RQuadling 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:RQuadling
ID: 7146690
$sQuery = "SELECT ID,WORKER,COMPANY,LINK_TO_DETAILS FROM JOB $sWhere ORDER BY ID";

Might make more sense.


0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
The viewer will learn how to count occurrences of each item in an array.
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now