Solved

Simple PHP & MySQL search engine

Posted on 2002-07-11
5
220 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

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…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

803 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