Answer the question
In order to leave comments, you need to log in
Not looking for Russian words/Cyrillic?
Hello everyone, I did a search for the site, everything works fine, except that it refuses to search for requests in Russian and gives an error
Here is Search.php, but I think it’s not an error in it, since it searches for numbers and English words perfectly.
include_once('DbConnect.php');
class Search extends DbConnect
{
/** @var null|string */
private $_table = null;
/** @var PDO */
private $_connection;
/** @var array */
private $_query = array();
/** @var bool|string */
private $_alias = false;
/** @var bool */
private $_one = false;
/** @var bool */
private $_where = false;
/**
* Search constructor.
* @param bool|string $_table
*/
public function __construct($_table = false)
{
if (!$this->_checkDB()) {
return array('error' => 'Failed to connect to DB');
}
if (!$_table) {
return array('error' => 'Table name is not specified');
}
$this->_table = $_table;
$this->_alias = $this->_getTableAlias($this->_table);
}
/**
* @param string $_fields
* @return $this
*/
public function select($_fields = '*')
{
if (is_array($_fields)) {
$_fields = implode(', ', $_fields);
}
$this->_query[] = "select {$_fields} from {$this->_table} as {$this->_alias}";
return $this;
}
/**
* @param string $_type
* @param bool $_table
* @param array $_conditions
* @return $this
* @internal param bool $_alias
*/
public function _join($_type = 'left', $_table = false, $_conditions = array())
{
switch ($_type) {
case 'left':
case 'right':
case 'inner':
$_alias = $this->_getTableAlias($_table);
$this->_query[] = "{$_type} join `{$_table}` as {$_alias} on";
foreach ($_conditions as $_condition) {
switch ($_condition['joinWith']) {
case 'this':
foreach ($_condition['fields'] as $_field => $_field_condition) {
$this->_query[] = "{$_condition['type']} {$_alias}.{$_field}{$_condition['operation']}'{$_field_condition}'";
}
break;
default:
foreach ($_condition['fields'] as $_field => $_field_condition) {
$this->_query[] = "{$_condition['type']} {$_field_condition}{$_condition['operation']}" .
"{$this->_getTableAlias($_condition['joinWith'])}.{$_field}";
}
break;
}
}
break;
default:
break;
}
return $this;
}
/**
* @param array $_conditions
* @return $this
*/
public function where($_conditions = array())
{
if (is_array($_conditions)) {
$_request = array();
if (!$this->_where) {
$_request[] = 'where';
$this->_where = true;
}
foreach ($_conditions as $_field => $_value) {
if (array_key_exists('type', $_value)) {
$_request[] = $_value['type'];
}
if (array_key_exists('operation', $_value)) {
$_request[] = "{$this->_alias}.{$_value['field']}{$_value['operation']}{$_value['value']}";
} else {
$_request[] = "{$this->_alias}.{$_value['field']} {$_value['value']}";
}
}
$this->_query[] = implode(' ', $_request);
}
return $this;
}
/**
* @return array|object
*/
public function toArray()
{
try {
$_query = $this->_connection
->query(implode(' ', $this->_query));
return $this->_preFormat($_query->fetchAll());
} catch (Exception $exception) {
return array('error' => $exception->getMessage());
}
}
private function _preFormat($_data = array())
{
$_results = array();
$_columns = array();
foreach ($_data as $_record) {
if (empty($_columns)) {
$_column_index = 0;
foreach ($_record as $_key => $_value) {
if (!in_array($_key, $_columns) && is_string($_key)) {
$_columns[$_column_index] = $_key;
$_column_index++;
}
}
}
$_object = array();
foreach ($_columns as $_column) {
$_object[$_column] = $_record[$_column];
}
if ($this->_one) {
$_results = (object)$_object;
} else {
$_results[] = (object)$_object;
}
}
return $_results;
}
public function limit($_limit = 1, $_offset = 0)
{
$this->_query[] = "limit {$_limit} offset {$_offset}";
return $this;
}
public function first($_field = 'id')
{
$this->_query[] = "order by {$this->_alias}.{$_field} asc";
$this->limit();
$this->_one = true;
return $this->toArray();
}
public function last($_field = 'id')
{
$this->_query[] = "order by {$this->_alias}.{$_field} desc";
$this->limit();
$this->_one = true;
return $this->toArray();
}
/**
* @return bool
*/
private function _checkDB()
{
try {
$this->_connection = DbConnect::getInstance();
return true;
} catch (Exception $exception) {
return false;
}
}
/**
* Returns table alias
*
* @param bool $_table
* @return bool|string
*/
private function _getTableAlias($_table = false)
{
if (!$_table) {
return false;
}
$_table_alias = explode('_', $_table);
if (count($_table_alias) > 1) {
$_alias = '';
foreach ($_table_alias as $_word) {
$_alias .= $_word[0];
}
return $_alias;
} else {
return $_table[0];
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question