[ create a new paste ] login | about

Link: http://codepad.org/oVSmZUds    [ raw code | output | fork ]

PHP, pasted on Jan 26:
<?php
// config
$table_name = "calendar_event"; //which table from database 
$first_field = "event"; //which field from table 
$second_field = "company"; //which field from table 
$third_field = "description"; 
$fourth_field = "city"; 
$limitchar = 3; //minimum of characters 
$records_number = 100;  // Number of records to show per page (different from 0) 
$page_number = 1;// default start page 

$offset = 0;
$arraySearch = array('inv', 'test');

// Generate query
$arrayFields = array(0 => $first_field, 1 => $second_field, 2 => $third_field, 3 => $fourth_field);
  $countSearch = count($arraySearch);
  $a = 0;
  $b = 0;
  $query = "SELECT * FROM `$table_name` WHERE (";
  $countFields = count($arrayFields);
  while ($a < $countFields)
  {
    while ($b < $countSearch)
    {
      $query = $query."`$arrayFields[$a]` LIKE '%$arraySearch[$b]%'";
      $b++;
      if ($b < $countSearch)
      {
        $query = $query." AND ";
      }
    }
    $b = 0;
    $a++;
    if ($a < $countFields)
    {
      $query = $query.") OR (";
    }
  }
  $query = $query.") LIMIT $offset, $records_number;";

var_dump($query);


Output:
1
string(269) "SELECT * FROM `calendar_event` WHERE (`event` LIKE '%inv%' AND `event` LIKE '%test%') OR (`company` LIKE '%inv%' AND `company` LIKE '%test%') OR (`description` LIKE '%inv%' AND `description` LIKE '%test%') OR (`city` LIKE '%inv%' AND `city` LIKE '%test%') LIMIT 0, 100;"


Create a new paste based on this one


Comments: