I’m surprised this file is required.
File: incs/_dbResults-GBOOKS.php
<?php DECLARE(STRICT_TYPES=1);
# error_reporting(-1);
# ini_set('display_errors', '1');
defined('LOCALHOST')
?:
define('LOCALHOST', 'localhost'===$_SERVER['SERVER_NAME']);
define('SHOP', LOCALHOST ? 'g' : substr($_SERVER['SERVER_NAME'], 0,1));
defined('jj') ?: define('jj', "<br>\n");
define('SHOWSQL', FALSE);
define('LIMITRECS', 22);
$table = 'booksGekko'; // GOOD
$table = 'booksDasa'; // GOOD
require '../../PASSWORD-LOGIN.php';
require '../../incs/Class_gbooks.php';
$gb = new GB;
# STANDARDIZE
$dCOLREF = 'ref';
$dCOLAUTH = 'author';
$dCOLMEMO = 'memo';
$dCOLTYPE = 'type';
$dCOLBAHT = 'baht';
$dCOLQQQ = 'qqq';
$cols = "`$dCOLAUTH`,`$dCOLREF`,`$dCOLMEMO`,`$dCOLBAHT`,`$dCOLTYPE`";
//==========================================================
// Display Search Results
//==========================================================
$search = $_GET['q'] ?? '';
if( empty($search) ):
# do nothing
else:
$aParams = getParams($search);
# GET/BUILD QUERY
$where = getSqlWhere($aParams, $cols);
$sqlCnt = getCombinedCount($table, $where, $cols);
$sqlRows = getCombinedRows( $table, $where, $cols, LIMITRECS);
# GET RECORDS
$link = new mysqli("localhost", uNAME, pWORD, dBASE);
if(SHOWSQL):
echo showSql($sqlCnt);
endif;
$resultCnt = mysqli_query($link, $sqlCnt);
if( $resultCnt ) :
$recNos = $resultCnt->fetch_object();
$recNos = intval($recNos -> recNo);
else:
$recNos = '';
endif;
if(SHOWSQL):
echo showSql($sqlRows);
endif;
$resultRows = mysqli_query($link, $sqlRows);
# RENDER RESULTS
$maybeShowing = '';
if($recNos > LIMITRECS):
$maybeShowing = ' (showing: ' .LIMITRECS .')';
endif;
echo '<p class="fwb fga">'
. number_format((float)$recNos)
. ' Books found: '
. $maybeShowing
.'</p>'
;
$lines = '';
$i2 = 1;
if($resultRows) :
while($row = $resultRows->fetch_object()) :
$togBg = ++$i2 % 2 ? 'bgc' : 'bge';
$gbook = '<i class="tac fsl fg0"> Reviews: </i>';
# SINGLE
if( 1===$recNos ) :
$gbook = $gb->fnGetGbooks($gb, $row);
endif;
$lines .= $gb->render($row, $togBg, $search, $gbook) ;
endwhile;
endif;
echo $lines;
endif; # if( empty($search) ):
/* =================================
# Validate and clean input text
================================== */
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 $result;
}//
/* ============================================
# Parse User's input input into a string
============================================== */
function getSqlWhere
(
string $aParams,
string $cols
)
:string
{
$result = '';
foreach($aParams as $i2 => $param):
if($i2 > 0):
$result .= ' AND ';
endif;
$result .= "
CONCAT($cols)
LIKE '%$param%'
";
endforeach;
return $result;
}
/* ===========================================
# Combine two SQL Statements - ORDER IMPORTANT
=========================================== */
function getCombinedCount
(
string $table,
string $sWhere,
string $cols
)
:string
{
$LIMITRECS = LIMITRECS;
$result = <<< ____TMP
SELECT COUNT(*) AS `recNo`
FROM `$table`
WHERE $sWhere
;
____TMP;
return $result;
}
/* ===========================================
# Conbine two SQL Statements - ORDER IMPORTANT
=========================================== */
function getCombinedRows
(
$table,
$sWhere,
$cols,
$limitrecs
)
:string
{
$result = <<< ____TMP
SELECT $cols
FROM `$table`
WHERE $sWhere
LIMIT 0, $limitrecs;
____TMP;
return $result;
}
//================================
function showSql($sql)
:string
{
$result = '';
$from = [
'SELECT',
'FROM',
'WHERE',
'LIKE',
'AND',
'CONCAT(',
'LIMIT',
];
$to = [
'<b>SELECT </b> ',
'<br><b>FROM </b> ',
'<br><b>WHERE </b> ',
'<br><b>LIKE </b>',
'<br><b>AND </b>',
'<br><b>CONCAT<br>(</b> ',
'<br><b>LIMIT </b>',
];
$str = str_replace($from, $to, $sql);
$result = ''
.'<dl class="bgy fg0 fss XXXbgr">'
. '<dt class="ooo">SQL:</dt>'
. '<dd>' .$str .'</dd>'
. '<dd> </dd>'
.'</dl>';
return $result;
}