Search Form Multi Input - php

I have a search form with 2 select fields and one input, total 3 options, so i created some if statements, depending of each fields are set it haves is own query, but its not working well, is all buggy, the results doesn't get right, it gets mixed up with the statements queries, it's not getting right.
Here is my code for the search form result:
$keywords = $_GET["Keywords"];
$location = $_GET['Location'];
$jobtype = $_GET["Category"];
if (isset($location) && empty($jobtype) && empty($keywords)){
$sql_jobs = "SELECT * FROM jobs
WHERE
active = '1' AND
country = '$location'
ORDER BY id_job DESC";
}elseif(isset($location) && isset($jobtype) && empty($keywords)){
$sql_jobs = "SELECT * FROM jobs
WHERE
active = '1' AND
country = '$location' AND
jobType_en = '$jobtype'
ORDER BY id_job DESC";
}elseif(isset($location) && isset($jobtype) && isset($keywords)){
$sql_jobs = "SELECT * FROM jobs
WHERE
active = '1' AND
country = '$location' AND
jobType_en = '$jobtype' AND
title_en LIKE '%$keywords%'
ORDER BY id_job DESC";
}elseif(empty($location) && isset($jobtype) && empty($keywords)){
$sql_jobs = "SELECT * FROM jobs
WHERE
active = '1' AND
jobType_en = '$jobtype'
ORDER BY id_job DESC";
}elseif(empty($location) && isset($jobtype) && isset($keywords)){
$sql_jobs = "SELECT * FROM jobs
WHERE
active = '1' AND
jobType_en = '$jobtype' AND
title_en LIKE '%$keywords%'
ORDER BY id_job DESC";
}elseif(empty($location) && isset($jobtype) && isset($keywords)){
$sql_jobs = "SELECT * FROM jobs
WHERE
active = '1' AND
jobType_en = '$jobtype' AND
title_en LIKE '%$keywords%'
ORDER BY id_job DESC";
}
else{
$sql_jobs = "SELECT * FROM jobs
WHERE
active = '1'
ORDER BY id_job DESC";
}
$consultaJob = mysql_query($sql_jobs);

You need to simplify the code:
$sql = "SELECT * FROM jobs
WHERE
active = '1' AND
country = '$location' ";
$order = "ORDER BY id_job DESC";
$where = "";
if( isset( $jobtype ) { $where .= " AND jobType_en = '$jobtype'"; }
if( isset($keywords) { $where .= " AND title_en LIKE '%$keywords%'" };
$sqlx = $sql . $where . $order;
You are obviously processing $jobtype and $keywords earlier on. In this processing you should be setting some default values to make later processing easier.
$jobtitle = (isset( $_GET['jobtitle'] ) ) ? $_GET['jobtitle'] : "";
$keywords= (isset( $_GET['keywords'] ) ) ? $_GET['keywords'] : "";
This way you can use:
if( $jobtitle != '') { .... }

You may want to try strlen() rather than isset or empty
otherwise you have to use both isset() and empty()
empty() does not generate a warning if the variable does not exist.
strlen() will return zero on NULL and ''.
You can set minimum lengths by changing the zero to a higher value.
elseif(strlen($location) > 0 && strlen($jobtype) > 0 && strlen($keywords) > 0 ){
If the submitted values are text inputs then you may need the trim also:
elseif(strlen(trim($location)) > 0 && strlen(trim($jobtype)) > 0 && strlen(trim($keywords)) > 0 ){

Related

ORDER BY is not working with mysql multiple queries

I am using multiple queries and it's working fine, the only issue I am facing is that order by is not working. I tried some code like
$query = "SELECT id, name, reg_number, class, section FROM register where id IS NOT NULL ORDER BY `id` DESC";
it's working fine in phpmyadmin and giving me a proper result. But it's not working where I want to use it.
$query = "SELECT id, name, reg_number, class, section FROM register where id IS NOT NULL ";
if ( $name !="" ){
$query .= " AND `name` LIKE '".$name."%'"; // id is greater then
}
if ( $status !="" ){
$query .= " AND `status` LIKE '".$status."%'"; // id is greater then
}
if ( $id_from !="" ){
$query .= " AND id >= $id_from "; // id is greater then
}
if ( $id_to !="" ){
$query .= " AND id <= $id_to "; // id is shorter then
}
if ( $class !="" ){
$query .= " AND class IN($class)"; // Selecting class
}
if ( $section !="" ){
$query .= " AND section IN($section)"; // selecting section
}
$result = mysql_query($query);
I want to use order by in this query but order by is not working with this.
$query = "SELECT id, name, reg_number, class, section FROM register where id IS NOT NULL ORDER BY `id` DESC";
AND also used
$query = "SELECT id, name, reg_number, class, section FROM register where id IS NOT NULL ORDER BY id DESC";
I don't know what's the problem with my code.
Just add ORDER BY when finish your WHERE:
if ( $section !="" ){
$query .= " AND section IN($section)"; // selecting section
}
$query .= " ORDER BY id DESC";
$result = mysql_query($query);

PHP & MySQL totalrow counting with condition

I have a problem to get total rows counting with condition. This is my code
$pagesize = 10;
$recordstart = (int)$_GET['recordstart'];
$recordstart = (isset($_GET['recordstart'])) ? $recordstart : 0;
$town = $_GET['label_town'];
$sub = ucfirst($_GET['label_sub']);
Here is my selecting condition. I also use this condition to count totalrows
if (isset($_GET['label_town']) === true && isset($_GET['label_sub']) === true) {
$where = "WHERE p.label_town = '$town' AND p.label_sub = '$sub' AND `visible` = 1";
} else if (isset($_GET['label_town']) === true && isset($_GET['label_sub']) === false) {
$where = "WHERE p.label_town = '$town' AND `visible` = 1";
} else {
$where = "WHERE `visible` = 1";
}
// Selecting data
$all_page_index = mysql_query("SELECT p.page_id, p.timestamp, p.label_town, p.label_sub, p.ime_nekretnine, p.mjesto, p.cijena_noc, p.krevet_apart, p.broj_apart, p.min_nocenja, p.description, p.visits, i.image_id, i.page_id, i.ext
FROM data_page AS p
LEFT JOIN (
SELECT MAX( image_id ) AS max, page_id
FROM images
GROUP BY page_id
) AS n ON p.page_id = n.page_id
LEFT JOIN images AS i ON i.image_id = n.max
$where
ORDER BY p.page_id DESC
LIMIT $recordstart, $pagesize");
// Counting rows with conditions
function totalrows() {
return mysql_result(mysql_query("SELECT COUNT(p.page_id) FROM data_page AS p $where"), 0);
}
Selecting and displaying data works fine but problem is with SELECT COUNT. Always count totalrows no mater what condition is. Where I making wrong?
Thanks!
Your COUNT is within function definition, where $where isn't defined. You need to pass it as a parameter or move query outside the function.

if argument inside mysql query

I have now three different PHP pages that contain almost the same information so to be able to reduce this to one page I need to have a php variable inside the mysql query.
Today it is like this:
$query1 = "SELECT * FROM `Yrker` WHERE `Kategori` = '1' AND `Bruk` = '1' ORDER BY yearstart DESC, mndstart DESC";`
I need that the " AND Bruk = '1'" is removed from this query-line if i put ?nobruk=no in the adressbar. Is this possible and if so, how?
You don't want to (and can't) put an if inside your query; you want to use an if to create your query based on some condition. There are lots of ways to write this, one of which is
if (!empty($_GET['nobruk'])) {
$query1 = "SELECT ... WHERE `Kategori` = '1' ORDER BY ...";
}
else {
$query1 = "SELECT ... WHERE `Kategori` = '1' AND `Bruk` = '1' ORDER BY ...";
}
Another way, which is shorter and involves the ternary operator, is
$includeBruk = empty($_GET['nobruk']);
$query1 = "SELECT ... WHERE `Kategori` = '1' ".
($includeBruk ? "AND `Bruk` = '1' " : "").
"ORDER BY ...";
A simple if statement:
$query1 = "SELECT * FROM `Yrker` WHERE `Kategori` = '1'";
if ($_GET['nobruk']!='no') {
$query1.=" AND `Bruk` = '1'";
}
$query1.= " ORDER BY yearstart DESC, mndstart DESC";
Like this :
<?php
$query = ($_REQUEST['nobruk'] == "no") ? "SELECT * FROM `Yrker` WHERE `Kategori` = '1' ORDER BY yearstart DESC, mndstart DESC": "SELECT * FROM `Yrker` WHERE `Kategori` = '1' AND `Bruk` = '1' ORDER BY yearstart DESC, mndstart DESC";
echo $query;
?>
$query1 = "SELECT * FROM `Yrker` WHERE `Kategori`='1' ".($_GET['nobruk'] === 'no' ? "" : "AND `Bruk`='1' ")."ORDER BY yearstart DESC, mndstart DESC";

Put a condition for a field within query

I have a sql query :
$cond = "";
if($cid >0 )
{
$quali = $this->getCandidatesQualification($cid);
$cond = "WHERE emp_qualification LIKE '%$quali%'";
}
$sql = "SELECT
emp_job_id,emp_job_profie,emp_qualification,emp_experience
FROM
tbl_emp_data
$cond
ORDER BY job_add_date DESC LIMIT 0,10
";
$res = $this->db->returnArrayOfObject($sql,$pgin = 'no', $odr='no');
Now what I want if emp_qualification field is equal to any_graduate I want to select all the jobs for the candidate even if his qualification is say BA .
so modify your WHERE clause to
WHERE emp_qualification LIKE '%$quali%'
OR emp_qualification = 'any_graduate'
$cond = '';
if($cid >0 ) {
if ($this->getCandidatesQualification($cid) != 'any_graduate') {
$cond = "WHERE emp_qualification LIKE '%{$this->getCandidatesQualification($cid)}%'";
}
}
simplified, try this

MySQL: Combining two repetitive queries to make WHERE clause dynamic

How can I combine these two queries into one so I don't have to repeat the same lines again and again?
if(empty($cat_id))
{
$sql = "
SELECT *
FROM root_category_contacts
ORDER by cat_order ASC
";
$items_category = $connection->fetch_all($sql);
}
else
{
$sql = "
SELECT *
FROM root_category_contacts
WHERE root_category_contacts.cat_id != ?
ORDER by cat_order ASC
";
$items_category = $connection->fetch_all($sql,array($cat_id));
}
I don't need WHERE clause when I don't have the cat_id.
Is it feasible?
Test if ? is null or equals cat_id. Something like this:
Edit based on xdazz's comment. And assuming that cat_id > 0
$sql = "
SELECT *
FROM root_category_contacts
WHERE root_category_contacts.cat_id != ?
ORDER by cat_order ASC"
if(empty($cat_id)) {
$cat_id = 0;
}
$items_category = $connection->fetch_all($sql,array($cat_id));
if(empty($cat_id)) $where = "";
else $where = "WHERE root_category_contacts.cat_id != '$cat_id'"
$sql = "
SELECT *
FROM root_category_contacts
$where
ORDER by cat_order ASC
";
$items_category = $connection->fetch_all($sql);

Categories