I was thinking about this some time. I'm using a lot if statements, and sometimes I have a lot of them. I know I can use switch
to shorten a bit the code. Is there another way to shorten them?
Let's say we have this example:
$all_conditions = '';
if($post_status != ""){
$conditions[] = "posts.post_status = '".$post_status."'";
}
if($post_type != ""){
$conditions[] = "posts.post_type = '".$post_type."'";
}
if($meta_key != ""){
$conditions[] = "postmeta.meta_key = '".$meta_key."'";
}
if($meta_value != ""){
$conditions[] = "postmeta.meta_value = '".$meta_value."'";
}
if($date_begin != "" && $date_end != ""){
$conditions[] = "( posts.post_date BETWEEN '".$date_begin."' AND '".$date_end."' )";
}
if(count($conditions)>0){
$all_conditions = implode(" AND ",$conditions);
}
if($all_conditions != ""){
$all_conditions = "WHERE ".$all_conditions;
}
What should I do to not repeat all those if's?
Try this:
<?php
$all_conditions = '';
$checkArr = array('post_status' => 'posts.post_status',
'post_type' => 'posts.post_type',
'meta_key' => 'postmeta.meta_key',
'meta_value' => 'postmeta.meta_value');
foreach ($checkArr AS $key => $val) {
if (${$key} != '') {
$conditions[] = "$val = '${$key}'";
}
}
if($date_begin != "" && $date_end != ""){
$conditions[] = "( posts.post_date BETWEEN '".$date_begin."' AND '".$date_end."' )";
}
if(count($conditions)>0){
$all_conditions = "WHERE ".implode(" AND ",$conditions);
} ?>
My best educated guess is the you retrieve the $post_status via a $post_status = $_POST['status']
kinda way, in that case you can shorten it like this (one of more ways to do it):
$all_conditions = '';
$mandatoryFields = array( 'status', 'type', 'key', 'value' );
foreach ( $mandatoryFields as $mandatoryField ) {
if ( $_POST[$mandatoryField] != '' ) {
$conditions[] = 'posts.`post_' . $mandatoryField . '` = "'.$_POST[$mandatoryField].'"';
}
}
Then go and add begin and end dates and take your code from there.
I use checks like that sometimes too, and I do not think that it should be packed with a foreach statements, because that reduces code legibility and mainainablility. However, it does make sense if you have more than 10 similar conditions. But you can make it imho much more readable by not putting the conditional inside a block, and maybe adding some structure && comments:
// check fields
if($post_status != "") $conditions[] = "posts.post_status = '".$post_status."'";
if($post_type != "") $conditions[] = "posts.post_type = '".$post_type."'";
if($meta_key != "") $conditions[] = "postmeta.meta_key = '".$meta_key."'";
if($meta_value != "") $conditions[] = "postmeta.meta_value = '".$meta_value."'";
// check dates
if($date_begin != "" && $date_end != "") $conditions[] = "( posts.post_date BETWEEN '".$date_begin."' AND '".$date_end."' )";
// any conditions present?
if(count($conditions)>0) {
$all_conditions = implode(" AND ",$conditions);
$all_conditions = "WHERE ".$all_conditions;
}
else $all_conditions = '';
I have also put the string concatenation inside the count($conditions)>0
, so you need not check twice.