Solved

Simple PHP & MySQL search engine

Posted on 2002-07-11
5
237 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

734 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