Welcome Guest, Not a member yet? Register   Sign In
Databases: searching for a word in entries containing multiple words using 'like'
#13

(This post was last modified: 05-18-2021, 10:11 PM by John_Betong.)

These functions may be of interest which are used in my DasaBookCafe.tk book search:

Code:
/* =================================
#  Validate and clean input text
# $params = 'search for multiple words go here';
==================== */
function getParams
(
  $params=NULL
)
:array
{
  $result = NULL;

  # REMOVE DOUBLE SPACES
    while (strpos($params, '  ') ) {
      $params = str_replace('  ',  ' ', $params);
    }

  # CONVERT TO ARRAY
    $result = explode(' ', $params);
    if( empty($result) ):
      $result[] = $params;
    endif;

  return (array) $result;
}//


/* ============================================
#  Parse User's input input into a string
============================================== */
function getSqlWhere
(
  $aParams, $cols
)
:string
{
  $result = '';

  foreach($aParams as $i2 => $param):
    if($i2 > 0):
      $result .= ' AND ';
    endif;
    $result .= "
               CONCAT($cols)
               LIKE '%$param%'
              ";
  endforeach;

  return (string) $result;
}


/* ===========================================
# Conbine two SQL Statements - ORDER IMPORTANT
=========================================== */
function getCombinedCount
(
  $table,
  $sWhere,
  $cols
)
:string
{
  $uLIMITREX = uLIMITREX;
  $result = <<< ____TMP
    SELECT COUNT(*) AS `recNo`
    FROM  `$table`
    WHERE  $sWhere
    ;
____TMP;

  return (string) $result;
}

/* ===========================================
# Conbine two SQL Statements - ORDER IMPORTANT
=========================================== */
function getCombinedRows
(
  $table,
  $sWhere,
  string $cols,
  $uLIMITREX
)
:string
{
  $result = <<< ____TMP
    SELECT $cols 
    FROM   `$table` 
    WHERE  $sWhere 
    LIMIT  0, $uLIMITREX;
____TMP;

  return (string) $result;
}
Reply


Messages In This Thread
RE: Databases: searching for a word in entries containing multiple words using 'like' - by John_Betong - 05-18-2021, 10:06 PM



Theme © iAndrew 2016 - Forum software by © MyBB