<?php
$search_array = array('application', 'recordtime', 'emr_soft_vend');
$where = array();
foreach ( $search_array as $search ) {
  $value = JRequest::getVar($search, '' , 'post');
  if ( $value ) {
    $where[] = " $search LIKE '%$value%' ";
  }
}
if ( !empty($where) ) {
  echo " WHERE ".implode(' AND ', $where);
}

 $valuefd = JRequest::getVar('datefrom', '' , 'post');
 $valuetd = JRequest::getVar('dateto', '' , 'post');
 $sfd = JRequest::getVar('sdatefrom', '' , 'post');
 $std = JRequest::getVar('sdateto', '' , 'post');
 $user = JFactory::getUser();
if($valuefd && $valuetd){ ?>
WHERE updatedate >= '<?php echo $valuefd; ?>' AND updatedate <='<?php echo $valuetd; ?>' AND USERNAME='<?php echo $user->username; ?>'
<?php }else if($valuefd){ ?>
WHERE updatedate LIKE '<?php echo $valuefd; ?>%' AND USERNAME='<?php echo $user->username; ?>'
<?php }else if($valuetd){ ?>
WHERE updatedate LIKE '<?php echo $valuetd; ?>%' AND USERNAME='<?php echo $user->username; ?>'
<?php }else if($sfd && $std){ ?>
WHERE recordtime >= '<?php echo $sfd; ?>' AND recordtime <='<?php echo $std; ?>' AND USERNAME='<?php echo $user->username; ?>'
<?php }else if($sfd){ ?>
WHERE recordtime LIKE '<?php echo $sfd; ?>%' AND USERNAME='<?php echo $user->username; ?>'
<?php }else if($std){ ?>
WHERE recordtime LIKE '<?php echo $std; ?>%' AND USERNAME='<?php echo $user->username; ?>'
<?php } else if(JRequest::getVar('reset', '' , 'post')=='Reset'){ ?>
WHERE USERNAME='<?php echo $user->username; ?>'
<?php } else { ?>
WHERE USERNAME='<?php echo $user->username; ?>'
<?php } ?>