php - How to minimize if else conditions for mysql query? -


need:

i need select value in field of table using mysql query when condition false.

below show few codes because db fields large.

code:

<?php  include "db.php"; $type1=$_post['type']; if($type1=='all') {    $type='*'; } else { $type=$type1; } $selecttype=mysqli_query($con, "select * product type='$type'"); ?> 

i konw * not correct. used example select all.

but when use above code query not success. solution of this?

my full code:

    <?php          $sort=$_post['sort'];         $city=$_post['city'];         $location=$_post['location'];         $type=$_post['type'];         $res_type=$_post['res_type'];         $cus_type=$_post['cus_type'];                              $i=0;                             include "db.php";                             if($sort=='name')                                $sort_by='name';                             else                                $sort_by=null;                                 if($city!=null && $location!=null && $sort!=null)                             {                                                                     if($type=='cod')                                 {                                     if($cus_type=='all')                                     {                                         $qy=mysqli_query($con, "select * add_res cod='on' , type='$res_type' , city='$city' , location='$location' order $sort_by ");                                           }                                     else                                     {                                         $qy=mysqli_query($con, "select * add_res cod='on' , type='$res_type' , cusine_type regexp '$cus_type' , city='$city' , location='$location' order $sort_by ");                                           }                                  }                                 else if($type=='pickup')                                 {                                     if($cus_type=='all')                                     {                                         $qy=mysqli_query($con, "select * add_res pickup='on' , type='$res_type' , city='$city' , location='$location' order $sort_by ");                                     }                                     else                                     {                                           $qy=mysqli_query($con, "select * add_res pickup='on' , type='$res_type' , cusine_type regexp '$cus_type' , city='$city' , location='$location' order $sort_by ");                                     }                                  }                                 else                                 {                                     if($cus_type=='all')                                     {                                         $qy=mysqli_query($con, "select * add_res city='$city' , type='$res_type' , location='$location' order $sort_by ");                                     }                                     else                                     {                                         $qy=mysqli_query($con, "select * add_res city='$city' , type='$res_type' , cusine_type regexp '$cus_type' , location='$location' order $sort_by ");                                             }                                  }                             }                             else if($city!=null && $location==null && $sort!=null)                             {                                                                     if($type=='cod')                                 {                                     if($cus_type=='all')                                     {                                         $qy=mysqli_query($con, "select * add_res cod='on' , type='$res_type' , city='$city' order $sort_by ");                                     }                                     else                                     {                                         $qy=mysqli_query($con, "select * add_res cod='on' , type='$res_type' , cusine_type regexp '$cus_type'and city='$city' order $sort_by ");                                     }                                  }                                 else if($type=='pickup')                                 {                                     if($cus_type=='all')                                     {                                         $qy=mysqli_query($con, "select * add_res pickup='on' , type='$res_type' , city='$city'  order $sort_by ");                                     }                                     else                                     {                                         $qy=mysqli_query($con, "select * add_res pickup='on' , type='$res_type' , cusine_type regexp '$cus_type' , city='$city'  order $sort_by ");                                     }                                  }                                 else                                 {                                     if($cus_type=='all')                                     {                                         $qy=mysqli_query($con, "select * add_res city='$city' , type='$res_type' order $sort_by ");                                     }                                     else                                     {                                         $qy=mysqli_query($con, "select * add_res city='$city' , type='$res_type' , cusine_type regexp '$cus_type'  order $sort_by ");                                             }                                  }                             }                               else if($city!=null && $location!=null && $sort==null)                             {                                  if($type=='cod')                                 {                                     if($cus_type=='all')                                     {                                         $qy=mysqli_query($con, "select * add_res cod='on' ,  city='$city' , location='$location' , type='$res_type' ");                                     }                                     else                                     {                                         $qy=mysqli_query($con, "select * add_res cod='on' ,  city='$city' , location='$location' ,  cusine_type regexp '$cus_type' , type='$res_type' ");                                             }                                  }                                 else if($type=='pickup')                                 {                                     if($cus_type=='all')                                     {                                         $qy=mysqli_query($con, "select * add_res pickup='on' ,  city='$city'  , type='$res_type' , location='$location' ");                                         }                                     else                                     {                                         $qy=mysqli_query($con, "select * add_res pickup='on' ,  city='$city'  , type='$res_type' , cusine_type regexp '$cus_type' , location='$location' ");                                         }                                  }                                 else                                 {                                     if($cus_type=='all')                                     {                                         $qy=mysqli_query($con, "select * add_res city='$city' , type='$res_type' , location='$location' ");                                      }                                     else                                     {                                         $qy=mysqli_query($con, "select * add_res city='$city' , cusine_type regexp '$cus_type'  , type='$res_type' , location='$location' ");                                              }                                  }                             }                              else if($city!=null && $location==null && $sort==null)                             {                                  if($type=='cod')                                 {                                     if($cus_type=='all')                                     {                                         $qy=mysqli_query($con, "select * add_res cod='on' ,  city='$city' , type='$res_type' ");                                     }                                     else                                     {                                         $qy=mysqli_query($con, "select * add_res cod='on' ,  city='$city' , find_in_set('$cus_type', cusine_type) , type='$res_type' ");                                             }                                  }                                 else if($type=='pickup')                                 {                                     if($cus_type=='all')                                     {                                         $qy=mysqli_query($con, "select * add_res pickup='on' ,  city='$city' , type='$res_type' ");                                       }                                     else                                     {                                         $qy=mysqli_query($con, "select * add_res pickup='on' ,  city='$city' , cusine_type regexp '$cus_type' , type='$res_type' ");                                               }                                  }                                 else                                 {                                     if($cus_type=='all')                                     {                                         $qy=mysqli_query($con, "select * add_res city='$city' , type='$res_type' ");                                       }                                     else                                     {                                         $qy=mysqli_query($con, "select * add_res city='$city'  , cusine_type regexp '$cus_type' , type='$res_type' ");                                               }                                  }                             }                             else if($city==null && $location==null && $sort==null)                             {                                   if($type=='cod')                                 {                                     if($cus_type=='all')                                     {                                         $qy=mysqli_query($con, "select * add_res cod='on' ,  type='$res_type' ");                                     }                                     else                                     {                                         $qy=mysqli_query($con, "select * add_res cod='on' , cusine_type regexp '$cus_type' ,  type='$res_type' ");                                             }                                  }                                 else if($type=='pickup')                                 {                                     if($cus_type=='all')                                     {                                         $qy=mysqli_query($con, "select * add_res pickup='on' , type='$res_type' ");                                         }                                     else                                     {                                         $qy=mysqli_query($con, "select * add_res pickup='on' , cusine_type regexp '$cus_type' , type='$res_type' ");                                                 }                                  }                                 else                                 {                                     if($cus_type=='all')                                     {                                         $qy=mysqli_query($con, "select * add_res  type='$res_type ");                                        }                                     else                                     {                                         $qy=mysqli_query($con, "select * add_res  type='$res_type' , cusine_type regexp '$cus_type' ");                                        }                                  }                             }                             else if($city==null && $location==null && $sort!=null)                             {                                  if($type=='cod')                                 {                                     if($cus_type=='all')                                     {                                         $qy=mysqli_query($con, "select * add_res cod='on' ,  type='$res_type'  order $sort_by ");                                     }                                     else                                     {                                         $qy=mysqli_query($con, "select * add_res cod='on' , cusine_type regexp '$cus_type' ,  type='$res_type'  order $sort_by ");                                             }                                  }                                 else if($type=='pickup')                                 {                                     if($cus_type=='all')                                     {                                         $qy=mysqli_query($con, "select * add_res pickup='on' , type='$res_type'  order $sort_by ");                                         }                                     else                                     {                                         $qy=mysqli_query($con, "select * add_res pickup='on' , cusine_type regexp '$cus_type' , type='$res_type'  order $sort_by ");                                         }                                  }                                 else                                 {                                     if($cus_type=='all')                                     {                                         $qy=mysqli_query($con, "select * add_res type='$res_type' order $sort_by ");                                       }                                     else                                     {                                         $qy=mysqli_query($con, "select * add_res type='$res_type' , cusine_type regexp '$cus_type' order $sort_by ");                                               }                                  }                             }                             while($res=mysqli_fetch_array($qy))                             {                                    $id=$res['id'];                                 $name=$res['name'];                                 $image=$res['logo'];                                 $desciption=$res['description'];                                 $cod=$res['cod'];                                 $pickup=$res['pickup'];                                 $min_order=$res['min_order'];                                 $i++;                             ?> // html code php                             <?php } ?> 

change code instead:

<?php  include "db.php"; $type1=$_post['type'];  if($type1=='all')     $type="where 1=1"; else    $type='where type = "'.$type1.'"';  $selecttype = mysqli_query($con, "select * product ".$type); ?> 

edit
works on multiple conditions, have append value $type variable this:

if($type1=='all')     $type="where 1=1"; else    $type='where type = "'.$type1.'"';  if($somethingelse == "example")     $type .= ' , somethingelse = "'.$somethingelse.'"'; 

Comments