Okay so I built this search page that searches for posts but my current code seems so bloated and inefficient.
My script(search page)
if(isset($_GET['title']))$title = $_GET['title'];else{ $title = ''; }
if(isset($_GET['ad_brand']))$brand = $_GET['ad_brand'];else{ $brand = ''; }
if(isset($_GET['min_range']))$min_range = $_GET['min_range'];else{ $min_range = ''; }
if(isset($_GET['max_range']))$max_range = $_GET['max_range'];else{ $max_range = ''; }
if(isset($_GET['sub_cat']))$sub_cat = $_GET['sub_cat'];else{ $sub_cat = ''; }
if(isset($_GET['for_r_s']))$for_r_s = $_GET['for_r_s']; else{ $for_r_s = ''; }
if(isset($_GET['main_cat']))$main_cat = $_GET['main_cat']; else{ $main_cat = ''; }
if(isset($_POST['filter_button'])){
if(isset($_POST['ad_brand']))
$brand = $_POST['ad_brand'];
else{$brand = '';}
if(isset($_POST['main_cat']))
$main_cat = $_POST['main_cat'];
else{ $main_cat = ''; }
if(isset($_POST['sub_cat']))
$sub_cat = $_POST['sub_cat'];
else{ $sub_cat = ''; }
if($_POST['min_range'] != '')
$min_range = $_POST['min_range'];
else{ $min_range = ''; }
if(isset($_POST['max_range']))
$max_range = $_POST['max_range'];
else{ $max_range = ''; }
if(isset($_POST['for_r_s']))
$for_r_s = $_POST['for_r_s'];
else{ $for_r_s = ''; }
header('location:search?title='. $title .'&main_cat='.$main_cat.'&sub_cat='. $sub_cat .'&ad_brand='. $brand .'&min_range='.$min_range.'&max_range='.$max_range.'&for_r_s='.$for_r_s.'');
}
if(isset($_GET['sub_cat'])){
if($_GET['sub_cat'] != ''){
if(isset($_GET['min_range'])){
if(isset($_GET['max_range']) && $_GET['max_range'] != ''){
if(isset($_GET['for_r_s'])){
if($_GET['for_r_s'] == 1 || $_GET['for_r_s'] == 2){
$query = "SELECT * FROM `posts`
WHERE `ad_title` LIKE CONCAT('%',?,'%') AND `ad_sub_cat` = ?
AND `ad_brand` LIKE CONCAT('%',?,'%') AND `ad_price` >= ?
AND `ad_price` <= ? AND `for_r_s` = ?";
$get_posts = $conn_posts->prepare($query);
$get_posts->bind_param("sssiii", $title, $sub_cat ,$brand, $min_range, $max_range, $for_r_s);
}else{
$query = "SELECT * FROM `posts` WHERE `ad_title` LIKE CONCAT('%',?,'%') AND `ad_sub_cat` = ? AND `ad_brand`
LIKE CONCAT('%',?,'%') AND `ad_price` >= ? AND `ad_price` <= ?";
$get_posts = $conn_posts->prepare($query);
$get_posts->bind_param("sssii", $title, $sub_cat ,$brand, $min_range, $max_range);
}
}else{
$query = "SELECT * FROM `posts` WHERE `ad_title` LIKE CONCAT('%',?,'%') AND `ad_sub_cat` = ? AND `ad_brand`
LIKE CONCAT('%',?,'%') AND `ad_price` >= ? AND `ad_price` <= ?";
$get_posts = $conn_posts->prepare($query);
$get_posts->bind_param("sssii", $title, $sub_cat ,$brand, $min_range, $max_range);
}
}else{
if(isset($_GET['for_r_s'])){
if($_GET['for_r_s'] == 1 || $_GET['for_r_s'] == 2){
$query = "SELECT * FROM `posts` WHERE `ad_title` LIKE CONCAT('%',?,'%') AND `ad_sub_cat` = ? AND `ad_brand`
LIKE CONCAT('%',?,'%') AND `ad_price` >= ? AND `for_r_s` = ?";
$get_posts = $conn_posts->prepare($query);
$get_posts->bind_param("sssii", $title, $sub_cat ,$brand, $min_range, $for_r_s);
}else{
$query = "SELECT * FROM `posts` WHERE `ad_title` LIKE CONCAT('%',?,'%') AND `ad_sub_cat` = ? AND `ad_brand`
LIKE CONCAT('%',?,'%') AND `ad_price` >= ?";
$get_posts = $conn_posts->prepare($query);
$get_posts->bind_param("sssi", $title, $sub_cat ,$brand, $min_range);
}
}else{
$query = "SELECT * FROM `posts` WHERE `ad_title` LIKE CONCAT('%',?,'%') AND `ad_sub_cat` = ? AND `ad_brand`
LIKE CONCAT('%',?,'%') AND `ad_price` >= ?";
$get_posts = $conn_posts->prepare($query);
$get_posts->bind_param("sssi", $title, $sub_cat ,$brand, $min_range);
}
}
}else{
if(isset($_GET['max_range']) && $_GET['max_range'] != ''){
if(isset($_GET['for_r_s'])){
if($_GET['for_r_s'] == 1 || $_GET['for_r_s'] == 2){
$query = "SELECT * FROM `posts` WHERE `ad_title` LIKE CONCAT('%',?,'%') AND `ad_sub_cat` = ? AND `ad_brand`
LIKE CONCAT('%',?,'%') AND `ad_price` <= ? AND `for_r_s` = ?";
$get_posts = $conn_posts->prepare($query);
$get_posts->bind_param("sssii", $title, $sub_cat ,$brand, $max_range,$for_r_s);
}else{
$query = "SELECT * FROM `posts` WHERE `ad_title` LIKE CONCAT('%',?,'%') AND `ad_sub_cat` = ? AND `ad_brand`
LIKE CONCAT('%',?,'%') AND `ad_price` <= ?";
$get_posts = $conn_posts->prepare($query);
$get_posts->bind_param("sssi", $title, $sub_cat ,$brand, $max_range);
}
}else{
$query = "SELECT * FROM `posts` WHERE `ad_title` LIKE CONCAT('%',?,'%') AND `ad_sub_cat` = ? AND `ad_brand`
LIKE CONCAT('%',?,'%') AND `ad_price` <= ?";
$get_posts = $conn_posts->prepare($query);
$get_posts->bind_param("sssi", $title, $sub_cat ,$brand, $max_range);
}
}else{
if(isset($_GET['for_r_s'])){
if($_GET['for_r_s'] == 1 || $_GET['for_r_s'] == 2){
$query = "SELECT * FROM `posts` WHERE `ad_title` LIKE CONCAT('%',?,'%') AND `ad_sub_cat` = ? AND `ad_brand`
LIKE CONCAT('%',?,'%') AND `for_r_s` = ?";
$get_posts = $conn_posts->prepare($query);
$get_posts->bind_param("sssi", $title, $sub_cat ,$brand, $for_r_s);
}else{
$query = "SELECT * FROM `posts` WHERE `ad_title` LIKE CONCAT('%',?,'%') AND `ad_sub_cat` = ? AND `ad_brand` LIKE CONCAT('%',?,'%')";
$get_posts = $conn_posts->prepare($query);
$get_posts->bind_param("sss", $title, $sub_cat ,$brand);
}
}else{
$query = "SELECT * FROM `posts` WHERE `ad_title` LIKE CONCAT('%',?,'%') AND `ad_sub_cat` = ? AND `ad_brand` LIKE CONCAT('%',?,'%')";
$get_posts = $conn_posts->prepare($query);
$get_posts->bind_param("sss", $title, $sub_cat ,$brand);
}
}
}
}else{
if(isset($_GET['min_range'])){
if(isset($_GET['max_range']) && $_GET['max_range'] != ''){
if(isset($_GET['for_r_s'])){
if($_GET['for_r_s'] == 1 || $_GET['for_r_s'] == 2){
$query = "SELECT * FROM `posts` WHERE `ad_title` LIKE CONCAT('%',?,'%') AND `ad_brand`
LIKE CONCAT('%',?,'%') AND `ad_price` >= ? AND `ad_price` <= ? AND `for_r_s` = ?";
$get_posts = $conn_posts->prepare($query);
$get_posts->bind_param("ssiii", $title, $brand, $min_range, $max_range, $for_r_s);
}else{
$query = "SELECT * FROM `posts` WHERE `ad_title` LIKE CONCAT('%',?,'%') AND `ad_brand`
LIKE CONCAT('%',?,'%') AND `ad_price` >= ? AND `ad_price` <= ?";
$get_posts = $conn_posts->prepare($query);
$get_posts->bind_param("ssii", $title, $brand, $min_range, $max_range);
}
}else{
$query = "SELECT * FROM `posts` WHERE `ad_title` LIKE CONCAT('%',?,'%') AND `ad_brand`
LIKE CONCAT('%',?,'%') AND `ad_price` >= ? AND `ad_price` <= ?";
$get_posts = $conn_posts->prepare($query);
$get_posts->bind_param("ssii", $title, $brand, $min_range, $max_range);
}
}else{
if(isset($_GET['for_r_s'])){
if( $_GET['for_r_s'] == 1 || $_GET['for_r_s'] == 2){
$query = "SELECT * FROM `posts` WHERE `ad_title` LIKE CONCAT('%',?,'%') AND `ad_brand`
LIKE CONCAT('%',?,'%') AND `ad_price` >= ? AND `for_r_s` = ?";
$get_posts = $conn_posts->prepare($query);
$get_posts->bind_param("sssi", $title, $brand, $min_range, $for_r_s);
}else{
$query = "SELECT * FROM `posts` WHERE `ad_title` LIKE CONCAT('%',?,'%') AND `ad_brand`
LIKE CONCAT('%',?,'%') AND `ad_price` >= ?";
$get_posts = $conn_posts->prepare($query);
$get_posts->bind_param("ssi", $title, $brand, $min_range);
}
}else{
$query = "SELECT * FROM `posts` WHERE `ad_title` LIKE CONCAT('%',?,'%') AND `ad_brand`
LIKE CONCAT('%',?,'%') AND `ad_price` >= ?";
$get_posts = $conn_posts->prepare($query);
$get_posts->bind_param("ssi", $title, $brand, $min_range);
}
}
}else{
if(isset($_GET['max_range']) && $_GET['max_range'] != ''){
if(isset($_GET['for_r_s'])){
if($_GET['for_r_s'] == 1 || $_GET['for_r_s'] == 2){
$query = "SELECT * FROM `posts` WHERE `ad_title` LIKE CONCAT('%',?,'%') AND `ad_brand`
LIKE CONCAT('%',?,'%') AND `ad_price` <= ? AND `for_r_S` = ?";
$get_posts = $conn_posts->prepare($query);
$get_posts->bind_param("ssii", $title, $brand, $max_range, $for_r_s);
}else{
$query = "SELECT * FROM `posts` WHERE `ad_title` LIKE CONCAT('%',?,'%') AND `ad_brand`
LIKE CONCAT('%',?,'%') AND `ad_price` <= ?";
$get_posts = $conn_posts->prepare($query);
$get_posts->bind_param("ssi", $title, $brand, $max_range);
}
}else{
$query = "SELECT * FROM `posts` WHERE `ad_title` LIKE CONCAT('%',?,'%') AND `ad_brand`
LIKE CONCAT('%',?,'%') AND `ad_price` <= ?";
$get_posts = $conn_posts->prepare($query);
$get_posts->bind_param("ssi", $title, $brand, $max_range);
}
}else{
if(isset($_GET['for_r_s'])){
if($_GET['for_r_s'] == 1 || $_GET['for_r_s'] == 2){
$query = "SELECT * FROM `posts` WHERE `ad_title` LIKE CONCAT('%',?,'%') AND `ad_brand` LIKE CONCAT('%',?,'%') AND `for_r_s` = ?";
$get_posts = $conn_posts->prepare($query);
$get_posts->bind_param("ssi", $title, $brand, $for_r_s);
}else{
$query = "SELECT * FROM `posts` WHERE `ad_title` LIKE CONCAT('%',?,'%') AND `ad_brand` LIKE CONCAT('%',?,'%')";
$get_posts = $conn_posts->prepare($query);
$get_posts->bind_param("ss", $title, $brand);
}
}else{
$query = "SELECT * FROM `posts` WHERE `ad_title` LIKE CONCAT('%',?,'%') AND `ad_brand` LIKE CONCAT('%',?,'%')";
$get_posts = $conn_posts->prepare($query);
$get_posts->bind_param("ss", $title, $brand);
}
}
}
}
}else{
$sub_cat = '';
if(isset($_GET['for_r_s'])){
if($_GET['for_r_s'] == 1 || $_GET['for_r_s'] == 2){
$query = "SELECT * FROM `posts` WHERE `ad_title` LIKE CONCAT('%',?,'%') AND `ad_brand` LIKE CONCAT('%',?,'%') AND `for_r_s` = ?";
$get_posts = $conn_posts->prepare($query);
$get_posts->bind_param("ssi", $title, $brand, $for_r_s);
}else{
$query = "SELECT * FROM `posts` WHERE `ad_title` LIKE CONCAT('%',?,'%') AND `ad_brand` LIKE CONCAT('%',?,'%')";
$get_posts = $conn_posts->prepare($query);
$get_posts->bind_param("ss", $title, $brand);
}
}else{
$query = "SELECT * FROM `posts` WHERE `ad_title` LIKE CONCAT('%',?,'%') AND `ad_brand` LIKE CONCAT('%',?,'%')";
$get_posts = $conn_posts->prepare($query);
$get_posts->bind_param("ss", $title, $brand);
}
}
My Html code
<form action="" method="post">
<select name="main_cat" id="main_cat">
<option value="null" disabled selected>Select Category</option>
<?php
$get_category = $conn_posts->prepare("SELECT * FROM `cats` WHERE `main_cat` = 0;");
$get_category->execute();
$get_category_results = $get_category->get_result(); // get result
while($row = $get_category_results->fetch_assoc()){
echo '<option value="'.$row['ID'].'">'.$row['cat_name'].'</option>';
}
?>
</select>
</br>
<select name="sub_cat" id="sub-category-dropdown">
<option value="">Select SubCategory</option>
</select>
</br>
<input type="text" name="ad_brand" Placeholder="Brand(Cat,Jcb,Doosan)">
</br>
<label for="for_r_S">Price</label>
<div class="range_sliders">
<input type="text" name="min_range" Placeholder="Min">
<span> - </span>
<input type="text" name="max_range" Placeholder="Max">
</div>
</br>
<label for="for_r_S">For Rent</label>
<input type="radio" name="for_r_s" id="for_r_s" value="1">
<label for="for_r_S">For Sale</label>
<input type="radio" name="for_r_s" id="for_r_s" value="2">
</br>
<div id="content"></div>
<button type="submit" name="filter_button" class="filter_button">Search</button>
</form>
My Database table layout
ID | ad_title | ad_sub_cat | ad_price | used_new | for_r_s | ad_brand | ad_des | ad_location |
---|---|---|---|---|---|---|---|---|
1 | title | 12 | 100 | 1 | 1 | loerm | ipsum | new york |
for_r_s:: 1 = for rent, 2 = for sale
used_new:: 1 = used, 2 = new
Also the sub cat is gotten from ajax but i don't want to include that code here so just fill it in with any category like this <option value="12">Select car</option>
The script is working how I want it to but I just want to clean this code(i don't know of any bugs and it should work just fine) and I need some help doing that my friend told me to ask here so I am still ned please tell me if this post is not good and i will put more info.
if () {...} else {...} elseif () {...}
in your code that makes little sense. What does that do, and why? Understanding code can be so much easier if the idea behind it is known. Remember, we have no data, we cannot run your code, all we have is the information you give. \$\endgroup\$