Consider "0010000000100000". It's a string filled with nothing but "1" and "0" characters. Now, unless such a string is part of some classroom assignment where the goal is to programmatically convert Based 2 to Base 10 — or, perhaps, existing in some highly-limited and/or perverted language like MUMPS — there is never a good reason for it to exist in a program.

Because there are so many more appropriate data types — a boolean array, an integer bitmask, or even an integer array, just to name a few — a "boolean string" is a canary in the coalmine. If it's in an application's codebase, then chances are, there's something seriously wrong with that code. If, perchance, such a string is found in a VARCHAR column in some database, then something isn't just seriously wrong, the application is nothing short of an epic disaster.

Unfortunately, Jani Chaushev knows this fact all to well. In the application he was tasked with maintaining, there are several such columns, each filled with rows of data like this.

001000000010000000000000000000001001000000000000000000000000000000000010000000000
000000000000000000000000000000001010000000000000000000000000000000000000000000000
101000000010000000000001000000001001000000000000011110000000000000000010000000000
000000000001100000000000000000001010000000000000000001111000000000000000000000001

Fortunately, there's a perfectly sane way of querying for these rows. I'm kidding, of course. The rows are retrieved with a convoluted RegEx-based query.

SELECT *  FROM `redcated_table_name`
 WHERE `extras` REGEXP '^[0-1]{25}[1]{1}[0-1]{55}$' OR 
       `extras` REGEXP '^[0-1]{26}[1]{1}[0-1]{54}$' OR 
       `extras` REGEXP '^[0-1]{27}[1]{1}[0-1]{53}$' OR 
       `extras` REGEXP '^[0-1]{28}[1]{1}[0-1]{52}$' OR 
       `extras` REGEXP '^[0-1]{29}[1]{1}[0-1]{51}$' OR 
       `extras` REGEXP '^[0-1]{30}[1]{1}[0-1]{50}$' OR 
       `extras` REGEXP '^[0-1]{31}[1]{1}[0-1]{49}$' OR 
       `extras` REGEXP '^[0-1]{36}[1]{1}[0-1]{44}$' OR 
       `extras` REGEXP '^[0-1]{37}[1]{1}[0-1]{43}$' OR 
       `extras` REGEXP '^[0-1]{38}[1]{1}[0-1]{42}$' OR 
       `extras` REGEXP '^[0-1]{39}[1]{1}[0-1]{41}$' OR 
       `extras` REGEXP '^[0-1]{40}[1]{1}[0-1]{40}$' OR 
       `extras` REGEXP '^[0-1]{41}[1]{1}[0-1]{39}$' OR 
       `extras`='1'

And it only gets worse. This convoluted query for convoluted data is generated by even more convoluted code.

$sql = '';
$sql_sess = '';

if(isset($_POST['search_act']) && $_POST['search_act']== 1)
{
  if ( isset($_POST['show_extras']) || 
       isset($_POST['show_extras_x']) || 
       isset($_POST['show_extras_y']) )
  {              
    for($i=1;$i<=81;$i++)
    {
      if(isset($_POST['extra'.$i]) && $_POST['extra'.$i]==1)
      {            
        //if (in_array($i, array(1,2,3,4,5,6,7,8,9,10,11,12,13,14)))
        if (in_array($i, array(77,78,79,80,81)))
        {
          $iii=0;
          //for($ii=77;$ii<82;$ii++)
          for($ii=1;$ii<15;$ii++)
          {
            if($_POST['extra'.$ii]==1)
            {
              $sql .= " `extras` REGEXP '^[0-1]{".($ii-1)
	              ."}[1]{1}[0-1]{".($i-$ii-1)
		      ."}[1]{1}[0-1]{".(81-$i)."}$' OR";
              $iii++;
            } 
          }
          if($iii==0)  
          {
            $sql .= " `extras` REGEXP '^[0-1]{"
	            .($i-1)."}[1]{1}[0-1]{"
		    .(81-$i)."}$' OR";
          }
        } else if($i==15 or $i==16 or $i==17 or 
	          $i==18 or $i==19 or $i==20){
          $iii=0;
          for($ii=21;$ii<25;$ii++)
          {
            if($_POST['extra'.$ii]==1)
            {
              $sql .= " `extras` REGEXP '^[0-1]{"
	              .($i-1)."}[1]{1}[0-1]{".($ii-$i-1)
		      ."}[1]{1}[0-1]{".(81-$ii)."}$' OR";
              $iii++;
            } 
          }
          if($iii==0)  
          {
            $sql .= " `extras` REGEXP '^[0-1]{"
	            .($i-1)."}[1]{1}[0-1]{".(81-$i)."}$' OR";
          }
        } else if($i==43 or $i==44 or $i==45){
          $iii=0;
          for($ii=46;$ii<47;$ii++)
          {
            if($_POST['extra'.$ii]==1)
            {
              $sql .= " `extras` REGEXP '^[0-1]{"
	              .($i-1)."}[1]{1}[0-1]{".($ii-$i-1)
		      ."}[1]{1}[0-1]{".(81-$ii)."}$' OR";
              $iii++;
            } 
          }
          if($iii==0)  
          {
            $sql .= " `extras` REGEXP '^[0-1]{"
	            .($i-1)."}[1]{1}[0-1]{".(81-$i)."}$' OR";
          }
        } else if($i==47 or $i==48 or $i==49){
          $iii=0;
          for($ii=50;$ii<52;$ii++)
          {
            if($_POST['extra'.$ii]==1)
            {
              $sql .= " `extras` REGEXP '^[0-1]{".($i-1)
	              ."}[1]{1}[0-1]{".($ii-$i-1)."}[1]{1}[0-1]{"
		      .(81-$ii)."}$' OR";
              $iii++;
            } 
          }
          if($iii==0)  {
            $sql .= " `extras` REGEXP '^[0-1]{".($i-1)
	            ."}[1]{1}[0-1]{".(81-$i)."}$' OR";
          }
        } else if($i==52 or $i==53){
          $iii=0;
          for($ii=54;$ii<56;$ii++)
          {
            if($_POST['extra'.$ii]==1)
            {
              $sql .= " `extras` REGEXP '^[0-1]{".($i-1)
	              ."}[1]{1}[0-1]{".($ii-$i-1)
		      ."}[1]{1}[0-1]{".(81-$ii)."}$' OR";
              $iii++;
            } 
          }
          if($iii==0)  {
            $sql .= " `extras` REGEXP '^[0-1]{".($i-1)
	            ."}[1]{1}[0-1]{".(81-$i)."}$' OR";
          }
        } else if($i==56 or $i==57){
          $iii=0;
          for($ii=58;$ii<60;$ii++)
          {
            if($_POST['extra'.$ii]==1)
            {
              $sql .= " `extras` REGEXP '^[0-1]{".($i-1)
	              ."}[1]{1}[0-1]{".($ii-$i-1)
		      ."}[1]{1}[0-1]{".(81-$ii)."}$' OR";
              $iii++;
            } 
          }
          if($iii==0)  {
            $sql .= " `extras` REGEXP '^[0-1]{".($i-1)
	            ."}[1]{1}[0-1]{".(81-$i)."}$' OR";
          }
        } else{
          //if($i!=21 and $i!=22 and $i!=23 and $i!=24 and 
	  //   $i!=46 and $i!=50 and $i!=51 and $i!=54 and 
	  //   $i!=55 and $i!=58 and $i!=59 and $i!=77 and 
	  //   $i!=78 and $i!=79 and $i!=80 and $i!=81)
          if($i!=21 and $i!=22 and $i!=23 and $i!=24 and 
	     $i!=46 and $i!=50 and $i!=51 and $i!=54 and 
	     $i!=55 and $i!=58 and $i!=59 && 
	     !in_array($i, array(1,2,3,4,5,6,7,8,9,10,11,12,13,14)))
          {
            $sql .= " `extras` REGEXP '^[0-1]{".($i-1)
	            ."}[1]{1}[0-1]{".(81-$i)."}$' OR";
          }
        }  
        $sql_sess .= "1";
      } else {
        $sql_sess .= "0";
      }
    }
    $_SESSION['sql'] = '0'.$sql_sess;
  }
  
  
  if (isset($_POST['show_all']) || isset($_POST['show_all_x']) || 
      isset($_POST['show_all_y']))
  {
    $sql = "";
    $sql_sess = str_pad("0", 81, "0", STR_PAD_RIGHT);
    $_SESSION['sql'] = $sql_sess;
  }
  
} else {
        
  $sql_sess = str_split($_SESSION['sql']);
  $isnul = 'T';
  for($i=1;$i<=81;$i++)
  {
    if(isset($sql_sess[$i]) && $sql_sess[$i]==1)
    {
      $isnul = 'F';
      //if (in_array($i, array(1,2,3,4,5,6,7,8,9,10,11,12,13,14)))
      if (in_array($i, array(77,78,79,80,81)))
      {
        $iii=0;
        //for($ii=77;$ii<82;$ii++)
        for($ii=1;$ii<15;$ii++)
        {
          if($sql_sess[$ii]==1)
          {
            $sql .= " `extras` REGEXP '^[0-1]{".($ii-1)
	            ."}[1]{1}[0-1]{".($i-$ii-1)
		    ."}[1]{1}[0-1]{".(81-$i)."}$' OR";
            $iii++;
          } 
        }
        if($iii==0)  {
          $sql .= " `extras` REGEXP '^[0-1]{".($i-1)
	          ."}[1]{1}[0-1]{".(81-$i)."}$' OR";
        }
      } else if($i==15 or $i==16 or $i==17 or $i==18 or $i==19 or $i==20){
        $iii=0;
        for($ii=21;$ii<25;$ii++)
        {
          if($sql_sess[$ii]==1)
          {
            $sql .= " `extras` REGEXP '^[0-1]{".($i-1)."}[1]{1}[0-1]{"
	            .($ii-$i-1)."}[1]{1}[0-1]{".(81-$ii)."}$' OR";
            $iii++;
          } 
        }
        if($iii==0)  {
          $sql .= " `extras` REGEXP '^[0-1]{".($i-1)."}[1]{1}[0-1]{"
	          .(81-$i)."}$' OR";
        }
      } else if($i==43 or $i==44 or $i==45){
        $iii=0;
        for($ii=46;$ii<47;$ii++)
        {
          if($sql_sess[$ii]==1)
          {
            $sql .= " `extras` REGEXP '^[0-1]{".($i-1)."}[1]{1}[0-1]{"
	            .($ii-$i-1)."}[1]{1}[0-1]{".(81-$ii)."}$' OR";
            $iii++;
          } 
        }
        if($iii==0)  {
          $sql .= " `extras` REGEXP '^[0-1]{".($i-1)."}[1]{1}[0-1]{"
	          .(81-$i)."}$' OR";
        }
      } else if($i==47 or $i==48 or $i==49){
        $iii=0;
        for($ii=50;$ii<52;$ii++)
        {
          if($sql_sess[$ii]==1)
          {
            $sql .= " `extras` REGEXP '^[0-1]{".($i-1)."}[1]{1}[0-1]{"
	            .($ii-$i-1)."}[1]{1}[0-1]{".(81-$ii)."}$' OR";
            $iii++;
          } 
        }
        if($iii==0)  
        {
          $sql .= " `extras` REGEXP '^[0-1]{".($i-1)."}[1]{1}[0-1]{"
	          .(81-$i)."}$' OR";
        }
      } else if($i==52 or $i==53){
        $iii=0;
        for($ii=54;$ii<56;$ii++)
        {
          if($sql_sess[$ii]==1)
          {
            $sql .= " `extras` REGEXP '^[0-1]{".($i-1)."}[1]{1}[0-1]{"
	            .($ii-$i-1)."}[1]{1}[0-1]{".(81-$ii)."}$' OR";
            $iii++;
          } 
        }
        if($iii==0)  {
          $sql .= " `extras` REGEXP '^[0-1]{".($i-1)."}[1]{1}[0-1]{"
	          .(81-$i)."}$' OR";
        }
      } else if($i==56 or $i==57){
        $iii=0;
        for($ii=58;$ii<60;$ii++)
        {
          if($sql_sess[$ii]==1)
          {
            $sql .= " `extras` REGEXP '^[0-1]{".($i-1)."}[1]{1}[0-1]{"
	            .($ii-$i-1)."}[1]{1}[0-1]{".(81-$ii)."}$' OR";
            $iii++;
          } 
        }
        if($iii==0)  
        {
          $sql .= " `extras` REGEXP '^[0-1]{".($i-1)."}[1]{1}[0-1]{".(81-$i)."}$' OR";
        }
      } else{
        //if($i!=21 and $i!=22 and $i!=23 and $i!=24 and  
	//   $i!=46 and $i!=50 and $i!=51 and $i!=54 and 
	//   $i!=55 and $i!=58 and $i!=59 and $i!=77 and 
	//   $i!=78 and $i!=79 and $i!=80 and $i!=81 )
        if($i!=21 and $i!=22 and $i!=23 and $i!=24 and 
	   $i!=46 and $i!=50 and $i!=51 and $i!=54 and 
	   $i!=55 and $i!=58 and $i!=59 && 
	   !in_array($i, array(1,2,3,4,5,6,7,8,9,10,11,12,13,14)) )
        {
          $sql .= " `extras` REGEXP '^[0-1]{".($i-1)."}[1]{1}[0-1]{".(81-$i)."}$' OR";
        }
      }
    } 
  }
  if($isnul=='T')
  {
    $sql_sess = '00000000000000000000000000000000000000000'.
                '0000000000000000000000000000000000000000';
  }
}  
if($sql_sess!='0000000000000000000000000000000000000000'.
              '00000000000000000000000000000000000000000')
{
  $sql = $sql." `extras`='1'";
  //$sql .= " 1";
} else{
  $sql = "1";
}

$ProductsController = new ProductsController;
$arr = $ProductsController->getProducts($dbo, $sql, $_GET);
$smarty->assign('results_tpl', 1);
if(count($arr['catalog_arr'])>0)
{
  $smarty->assign('catalog_arr', $arr['catalog_arr']);
}