Solved

PHP Search facility

Posted on 2009-04-01
10
269 Views
Last Modified: 2012-05-06
I've setup a simple search facility to search for jobs. I have dropdowns for Job, Location, Type and Salary.

The salary dropdown is where I'm having issues, i have it as follows:

<select name="jobSalary" id="jobSalary">
            <option value="" selected >Select...</option>
            <option value="10,000" >Up to £10,000</option>
            <option value="10,000-11,000" >£10,000 - £11,000</option>
            <option value="12,000-13,000" >£12,000 - £13,000</option>
            <option value="14,000-15,000" >£14,000 - £15,000</option>
            <option value="16,000-17,000" >£16,000 - £17,000</option>
            <option value="18,000-19,000" >£18,000 - £19,000</option>
            <option value="20,000-21,000" >£20,000 - £21,000</option>
            <option value="22,000-23,000" >£22,000 - £23,000</option>
            <option value="24,000-25,000" >£24,000 - £25,000</option>
            <option value="25,000-30,000" >£25,000 - £30,000</option>
            <option value="30,000-34,000" selected="selected">£30,000 - £34,000</option>
            <option value="35,000-39,000" >£35,000 - £39,000</option>
            <option value="40,000-44,000" >£40,000 - £44,000</option>
            <option value="45,000" >£45,000 +</option>
          </select>

Whatever I select no jobs are displayed, my sql is as follows, I first split the jobSalary value into 2 so I can search between values:

$salary = $_POST["jobSalary"];
list($from, $to) = split('-', $salary);
       
mysql_select_db($database_mindset, $mindset);
$query_RSvac = "SELECT mind_client_jobs.*, mind_client.*\n"
    . "FROM mind_client_jobs LEFT JOIN mind_client ON mind_client_jobs.jobClient = mind_client.clientID WHERE mind_client.clientActive = 'Y' AND mind_client_jobs.jobActive = 'Y' AND mind_client_jobs.jobSector LIKE '%" . $_POST["jobSector"] . "%' AND mind_client_jobs.jobCounty LIKE '%" . $_POST["jobLocation"] . "%' AND mind_client_jobs.jobType LIKE '%" . $_POST["jobType"] . "%' AND mind_client_jobs.jobSalaryTo BETWEEN '$from' AND '$to' ORDER BY jobDate DESC";
$RSvac = mysql_query($query_RSvac, $mindset) or die(mysql_error());
$row_RSvac = mysql_fetch_assoc($RSvac);
$totalRows_RSvac = mysql_num_rows($RSvac);

I think it must be a problem with the salary as once I take this out of the sql query it works using the other fields.

Thanks

Chris
0
Comment
Question by:djfenom
10 Comments
 
LVL 3

Expert Comment

by:pkoops
ID: 24037148
I don't know if the "," might be a problem.
Try using 30000-34000 and split this.
You maybe need to alter your database then, but this might work better, if you add the "," later on, as you are showing the values.
0
 
LVL 14

Expert Comment

by:shobinsun
ID: 24037173
Hello,

Might be because of :

change after this line " list($from, $to) = split('-', $salary); " :

$from = ''.$from.'';
$to = ''.$to.'';

And try again.

Regards.
0
 

Author Comment

by:djfenom
ID: 24037206
Thanks guys, I've tried your suggestion shobinsun but it makes no difference.

I can actually write $from and $to to the page no problem, it just won't work in the SQL.
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 24037223
Two things to check/try:

1. You are using $_POST, make sure your form has method='post' in it

2. Take the commas out of the value clauses like so. Rather than

<option value="20,000-21,000" >£20,000 - £21,000</option>

try

<option value="20000-21000" >£20,000 - £21,000</option>

because the query will look like mind_client_jobs.jobSalaryTo BETWEEN '30,000' AND '34,000'  and MySQL does not store commas in integer or float fields. I am assuming that these fields are numeric, but that is where I would start.
0
 

Author Comment

by:djfenom
ID: 24037289
Hi bportlock, I've checked and it's definitely using method="post".

I've also taken out the comma in the values, but still the same? My field is set to INT in my database.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 500 total points
ID: 24037362
In that case, perhaps your quesy is selecting on exclusive conditions. For example a query to pick people called "Mary" might return some records, but one to pick someone called "Mary" and sex='Male' will return no records. I have restructured the query as follows

$query_RSvac = "SELECT mind_client_jobs.*, mind_client.* FROM mind_client_jobs
                    LEFT JOIN mind_client ON mind_client_jobs.jobClient = mind_client.clientID
                    WHERE mind_client.clientActive = 'Y' AND
                          mind_client_jobs.jobActive = 'Y' AND
                          mind_client_jobs.jobSector LIKE '%" . $_POST["jobSector"] . "%' AND
                          mind_client_jobs.jobCounty LIKE '%" . $_POST["jobLocation"] . "%' AND
                          mind_client_jobs.jobType LIKE '%" . $_POST["jobType"] . "%' AND
                          mind_client_jobs.jobSalaryTo BETWEEN '$from' AND '$to'
                    ORDER BY jobDate DESC";

take all the WHERE conditions out and try adding them back one at a time and see if the query starts returning no records. With this

$query_RSvac = "SELECT mind_client_jobs.*, mind_client.* FROM mind_client_jobs
                    LEFT JOIN mind_client ON mind_client_jobs.jobClient = mind_client.clientID
                    ORDER BY jobDate DESC";


then the BETWEEN stuff

$query_RSvac = "SELECT mind_client_jobs.*, mind_client.* FROM mind_client_jobs
                    LEFT JOIN mind_client ON mind_client_jobs.jobClient = mind_client.clientID
                    WHERE
                          mind_client_jobs.jobSalaryTo BETWEEN '$from' AND '$to'
                    ORDER BY jobDate DESC";


add another condition...

$query_RSvac = "SELECT mind_client_jobs.*, mind_client.* FROM mind_client_jobs
                    LEFT JOIN mind_client ON mind_client_jobs.jobClient = mind_client.clientID
                    WHERE mind_client.clientActive = 'Y' AND
                          mind_client_jobs.jobSalaryTo BETWEEN '$from' AND '$to'
                    ORDER BY jobDate DESC";

and so forth.
0
 
LVL 6

Expert Comment

by:twocandles
ID: 24037380
did you try to issue an "echo" of the actual query to see what's the exact string?
0
 

Author Comment

by:djfenom
ID: 24037453
I've had a play with changing the WHERE conditions and as soon as I take out the salary one, it works fine:

$query_RSvac = "SELECT mind_client_jobs.*, mind_client.* FROM mind_client_jobs
                    LEFT JOIN mind_client ON mind_client_jobs.jobClient = mind_client.clientID
                    WHERE mind_client.clientActive = 'Y' AND
                          mind_client_jobs.jobActive = 'Y' AND
                          mind_client_jobs.jobSector LIKE '%" . $_POST["jobSector"] . "%' AND
                          mind_client_jobs.jobCounty LIKE '%" . $_POST["jobLocation"] . "%' AND
                          mind_client_jobs.jobType LIKE '%" . $_POST["jobType"] . "%'
                    ORDER BY jobDate DESC";

I've done an echo of the query and done a search just on the salary and this is what I got:

SELECT mind_client_jobs.*, mind_client.* FROM mind_client_jobs LEFT JOIN mind_client ON mind_client_jobs.jobClient = mind_client.clientID WHERE mind_client.clientActive = 'Y' AND mind_client_jobs.jobActive = 'Y' AND mind_client_jobs.jobSector LIKE '%%' AND mind_client_jobs.jobCounty LIKE '%%' AND mind_client_jobs.jobType LIKE '%%' AND mind_client_jobs.jobSalaryTo BETWEEN '30000' AND '34000' ORDER BY jobDate DESC
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 24037475
Have a look in your MySQL table. How many records are in there with a salary between 30000 and 34000? Can you dump some of those records and post them here?
0
 

Author Comment

by:djfenom
ID: 24037716
I've had a play and fixed it, I basically had to make sure that a salary was selected before the query was ran, here is my working version.

Thanks for your help
$query_RSvac = "SELECT mind_client_jobs.*, mind_client.* FROM mind_client_jobs

                    LEFT JOIN mind_client ON mind_client_jobs.jobClient = mind_client.clientID

                    WHERE mind_client.clientActive = 'Y' AND

                          mind_client_jobs.jobActive = 'Y' AND

                          mind_client_jobs.jobSector LIKE '%" . $_POST["jobSector"] . "%' AND

                          mind_client_jobs.jobCounty LIKE '%" . $_POST["jobLocation"] . "%' AND

                          mind_client_jobs.jobType LIKE '%" . $_POST["jobType"] . "%'";

						  if ($_POST["jobSalary"] == "") {

						  $query_RSvac = $query_RSvac . " ORDER BY jobDate DESC";

						  }

						  else {

						 	 if ($from == "10000" && $to == "") {

							  $query_RSvac = $query_RSvac . " 

							  AND mind_client_jobs.jobSalaryTo <= '$from' ORDER BY jobDate DESC";

							 }

							 else if ($from == "45000" && $to == "") {

							  $query_RSvac = $query_RSvac . " 

							  AND mind_client_jobs.jobSalaryTo >= '$from' ORDER BY jobDate DESC";

							 }

							 else {

							  $query_RSvac = $query_RSvac . " 

							  AND mind_client_jobs.jobSalaryTo BETWEEN '$from' AND '$to' ORDER BY jobDate DESC";

							 }

						  }

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
change password links 7 74
nagios 4 php error after installation 6 77
.php tree directory? 5 56
I am Updating mysql where id =something ...Not working  Jquery/PHP 4 38
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

920 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

11 Experts available now in Live!

Get 1:1 Help Now