I am developing a search engine that match keyword search for game title and category game that is limited for three keyword search.i am facing some problem that need to swap among the keywords more than using one keyword search.Any guys can help me or give me some guide on the way of searching the keywords.
function search_game_test(){
global $CONF, $Q, $OUT, $DB, $DBSH, $CODE, $LANG;
$portalId = $Q->req['portalId'];
$keyword = $Q->req['keyword'];
$where = array();
$data = array();
$portal_db = $DB->getOne("SELECT dbName FROM Portal_DB WHERE portalId = $portalId");
array_push($where, array("PC.portalId = $portalId"));
array_push($where, array("PC.status = 'A'"));
array_push($where, array("PC.contentId = G.gameId"));
$trimmedKeyword = preg_replace('/ (?=[ .,])/', '', trim(preg_replace('/[\\~`!@$%^&*()+{}[\]<>?\/|="]/', '', trim($keyword))));
$explodeKeyword = explode(' ', $trimmedKeyword);
$keyword= ucfirst ($explodeKeyword);
//Check keyword in category list
$categoryCode = '';
for ($i = 0; $i < count($keyword); $i++)
{
if($i = 0){
$categoryCode = $CODE['GameCate2'][$keyword[0]];
if ($keyword != ''){
if($categoryCode!=''){
array_push($where, array("(G.titleFull LIKE '%$keyword[0]%' OR G.gameCate = '$categoryCode')"));
}
else{
array_push($where, array("G.titleFull LIKE '%$keyword[0]%'"));
}
$group = 'G.gameId';
$keyword = $keyword[0] ;
}
}
else if ($i = 1){
$categoryCode = $CODE['GameCate2'][$keyword[0].' '.$keyword[1]];
if ($keyword != ''){
if($categoryCode!=''){
array_push($where, array("(G.titleFull LIKE (('%$keyword[0]%'.' '.'%$keyword[1]%') OR ('%$keyword[0]%' OR '%$keyword[1]%')) OR G.gameCate = '$categoryCode')"));
}
else{
array_push($where, array("G.titleFull LIKE (('%$keyword[0]%'.' '.'%$keyword[1]%') OR ('%$keyword[0]%' OR '%$keyword[1]%'))"));
}
$group = 'G.gameId';
$keyword = $keyword[0].' '.$keyword[1] ;
}
}else{
$categoryCode = $CODE['GameCate2'][$keyword[0].' '.$keyword[1].' '.$keyword[2]];
if ($keyword != ''){
if($categoryCode!=''){
array_push($where, array("(G.titleFull LIKE ('%$keyword[0]%'.' '.'%$keyword[1]%'.' '.'%$keyword[2]%')OR ('%$keyword[0]%' OR '%$keyword[1]%' OR '%$keyword[2]%')) OR G.gameCate = '$categoryCode')"));
}
else{
array_push($where, array("G.titleFull LIKE (G.titleFull LIKE ('%$keyword[0]%'.' '.'%$keyword[1]%'.' '.'%$keyword[2]%') OR ('%$keyword[0]%' OR '%$keyword[1]%' OR '%$keyword[2]%'))"));
}
$group = 'G.gameId';
$keyword = $keyword[0].' '.$keyword[1].' '.$keyword[2] ;
}
}
};
$total = $DBSH->select(array(
'field' => array('G.gameId','G.titleFull','G.descFull','G.gameCate','G.publisherId','PC.price','PC.liveDate','P.publisherName','P.publisherId'),
'table' => 'Game G, PortalContent PC, Publisher P',
'where' => $where,
'order' => 'titleFull',
'group' => 'G.gameId',
));
//IF SEARCH GAME KEYWORD IS NUMERIC ei: 1944 for Air Strike 1944 gametitle
if( is_numeric($keyword) && $total == 0 ){
$where[count($where)-1] = array("G.titleFull LIKE '%$keyword%'");
$total = $DBSH->select(array(
'field' => array('G.gameId','G.titleFull','G.descFull','G.gameCate','G.publisherId','PC.price','PC.liveDate','P.publisherName','P.publisherId'),
'table' => 'Game G, PortalContent PC, Publisher P',
'where' => $where,
'group' => 'G.gameId',
'order' => $order,
'limit' => "$offset,$limit",
));
}
if ($total > 0){
if($categoryCode!=''){
$total_count_pg = $DB->getOne("SELECT COUNT(*) FROM Game G, PortalContent PC WHERE PC.portalId = '$portalId' AND (G.metaKeyword LIKE '%$keyword%' OR G.gameCate = '$categoryCode') AND PC.contentId = G.gameId AND PC.status='A'");
}elseif(is_numeric($keyword)){
$total_count_pg = $DB->getOne("SELECT COUNT(*) FROM Game G, PortalContent PC WHERE PC.portalId = '$portalId' AND PC.smskeyword LIKE '%$keyword%' AND PC.contentId = G.gameId AND PC.status='A'");
}else{
$total_count_pg = $DB->getOne("SELECT COUNT(*) FROM Game G, PortalContent PC WHERE PC.portalId = '$portalId' AND G.titleFull LIKE '%$keyword%' AND PC.contentId = G.gameId AND PC.status='A'");
}
while($row = $DBSH->fetchRow(DB_FETCHMODE_ASSOC)){
$row['gameName'] = stripslashes($row['titleFull']);
$row['publisherName'] = stripslashes($row['publisherName']);
$row['gameDesc'] = text_truncate2(stripslashes($row['descFull']),80);
$row['gameCate'] = $CODE['GameCate'][$row['gameCate']];
$row['price'] = $row['price'];
$row['rating'] = $DB->getOne("SELECT rating FROM $portal_db.Snapshot_Rating WHERE typeId='101' AND contentId='{$row['gameId']}'");
if ($row['rating'] == ''){
$row['rating'] = 0;
}
$data[] = $row;
}
}else{
if ($keyword != ''){
array_push($where, array("G.titleFull LIKE '%$keyword%'"));
array_push($where, array("PC.contentId = G.gameId"));
$group = 'G.gameId';
}
$total = $DBSH->select(array(
'field' => array('G.gameId','G.titleFull','G.descFull','G.gameCate','G.publisherId','PC.price','PC.liveDate','P.publisherName','P.publisherId'),
'table' => 'Game G, PortalContent PC, Publisher P',
'where' => $where,
'group' => $group,
'order' => $order,
//'limit' => "$offset,$limit",
));
while($row = $DBSH->fetchRow(DB_FETCHMODE_ASSOC)){
$row['gameName'] = stripslashes($row['titleFull']);
$row['publisherName'] = stripslashes($row['publisherName']);
$row['gameDesc'] = text_truncate2(stripslashes($row['descFull']),80);
$row['gameCate'] = $CODE['GameCate'][$row['gameCate']];
$row['price'] = $row['price'];
$row['rating'] = $DB->getOne("SELECT rating FROM $portal_db.Snapshot_Rating WHERE typeId='101' AND contentId='{$row['gameId']}'");
if ($row['rating'] == ''){
$row['rating'] = 0;
}
$data[] = $row;
}
}
if( count($data) < 1 ){
$data = array('Error' => 0);
}else{
$data = array('Result' => $data);
}
$this->provider->response($data);
}