[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 253
  • Last Modified:

Simple PHP & MySQL search engine

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
RonnieBrown
Asked:
RonnieBrown
  • 4
1 Solution
 
Richard QuadlingSenior Software DeverloperCommented:
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
 
Richard QuadlingSenior Software DeverloperCommented:
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
 
RonnieBrownAuthor Commented:
Would I put my SQL Query in the bit where you've written

"// Filter for SQL Query now in $sWhere;" ?
0
 
Richard QuadlingSenior Software DeverloperCommented:
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
 
Richard QuadlingSenior Software DeverloperCommented:
$sQuery = "SELECT ID,WORKER,COMPANY,LINK_TO_DETAILS FROM JOB $sWhere ORDER BY ID";

Might make more sense.


0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now