_get_condition_tours - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of _get_condition_tours.php

  1. <?php
  2.      
  3.      function _get_condition_tours(array  $params = [])
  4.     {
  5.         $sql = NULL;
  6.         $kogyo_code = $params['kogyo_code'] ?? NULL;
  7.         $kogyo_sub_code_from = $params['kogyo_sub_code_from'] ?? NULL;
  8.         $kogyo_sub_code_to = $params['kogyo_sub_code_to'] ?? NULL;
  9.         $where_queries = [];
  10.  
  11.         if (!empty($kogyo_code)) {
  12.             $where_queries[] = "
  13.                 o.kogyo_code = '{$kogyo_code}'
  14.             ";
  15.  
  16.             if (!empty($kogyo_sub_code_from) && !empty($kogyo_sub_code_to)) {
  17.                 $where_queries[] = "
  18.                     o.kogyo_sub_code BETWEEN '{$kogyo_sub_code_from}' AND '{$kogyo_sub_code_to}'
  19.                 ";
  20.             }
  21.  
  22.             if (!empty($kogyo_sub_code_from) && empty($kogyo_sub_code_to)) {
  23.                 $where_queries[] = "
  24.                     o.kogyo_sub_code = '{$kogyo_sub_code_from}'
  25.                 ";
  26.             }
  27.  
  28.             if (empty($kogyo_sub_code_from) && !empty($kogyo_sub_code_to)) {
  29.                 $where_queries[] = "
  30.                     o.kogyo_sub_code = '{$kogyo_sub_code_to}'
  31.                 ";
  32.             }
  33.         }
  34.  
  35.         $sql = implode(" AND ", $where_queries);
  36.  
  37.         if (empty($sql)) {
  38.             $sql = '1=1';
  39.         }
  40.  
  41.         return '(' . $sql . ')';
  42.     }
  43.     
  44.     
  45.     var_dump(_get_condition_tours());
  46.     var_dump(_get_condition_tours([]));
  47.     
  48.     var_dump(_get_condition_tours(['kogyo_code' => 1]));
  49.     var_dump(_get_condition_tours(['kogyo_code' => 1, 'kogyo_sub_code_from' => 2]));
  50.     var_dump(_get_condition_tours(['kogyo_code' => 1, 'kogyo_sub_code_to' => 3]));
  51.     var_dump(_get_condition_tours(['kogyo_code' => 1, 'kogyo_sub_code_from' => 4, 'kogyo_sub_code_to' => 5]));
  52.     
  53.     
  54.     
  55.     
File Description
  • _get_condition_tours
  • PHP Code
  • 24 Nov-2020
  • 1.69 Kb
You can Share it: