How to make Codeigniter Split Search - mysql

I want codeigniter search to be match with any one word to column.
Example: Let's Say Search Query is "Fashion Outlet for Mens", Now in table column title is "Fashion Outlet" only so i want search input to be split word by word and match column.
Any Help Please
public function search($query)
{
$q = $this->db->from('tablename')
->like('title',$query)
->get();
return $q->result();
}
Updated Question
public function search_query($query,$limit,$offset)
{
$keywords = explode(' ', $query);
foreach ($keywords as $keyword)
{
$keyword = trim($keyword);
$this->db->or_where("`title` LIKE '%$keyword%'");
$this->db->join('table2', 'tablename.id = table2.id');
$this->db->limit( $limit , $offset );
$this->db->order_by('updated_on','desc');
}
$this->db->get('tablename');
return $this->db->result();
}
Error Got
Not unique table/alias: 'table2'
SELECT * FROM `tablename` JOIN `table2` ON `tablename`.`id` = `table2`.`id` JOIN `table2` ON `tablename`.`id` = `table2`.`id` WHERE `title` LIKE '%fashion%' OR `title` LIKE '%outlet%' ORDER BY `updated_on` DESC, `updated_on` DESC LIMIT 50

Please find the Updated Answer for Order By Keyword Priority.
$keywords = explode(' ', $query);
$this->db->select('*');
$this->db->from('tablename');
$this->db->join('table2', 'tablename.id = table2.id','left');
$orderbyQry = "CASE ";
foreach ($keywords as $key => $keyword)
{
$orderbyQry.="WHEN tablename.title LIKE '%$keyword%' THEN $key ";
$keyword = trim($keyword);
$this->db->or_where("tablename.title LIKE '%$keyword%'");
}
$orderbyQry.=" ELSE 100 END ";
$this->db->limit( $limit , $offset );
$this->db->order_by($orderbyQry,'asc');
$this->db->get();
return $this->db->result();

Please join the table only once, and put the limit and order_by clause out of the loop.
public function search_query($query, $limit, $offset) {
$keywords = explode(' ', $query);
$this->db->select('*');
$this->db->join('table2', 'tablename.id = table2.id');
foreach ($keywords as $keyword)
{
$keyword = trim($keyword);
$this->db->or_where("`title` LIKE '%$keyword%'");
}
$this->db->limit( $limit , $offset );
$this->db->order_by('updated_on','desc');
$this->db->get('tablename');
return $this->db->result();
}

Related

Laravel Query- When carrying out a query Laravel seems to be adding ?/spaces between each letter

select code from country_code where country = '?U?n?i?t?e?d? ?K?i?n?g?d?o?m' order by country asc limit 1
public function location_test(){
$locations = DB::table('accesses')
->select('country' ,DB::raw('count(*) as count'))
->join('ip2c_cache', 'accesses.ip', '=', 'ip2c_cache.ip')
->where('showcase_id', 5613)->groupby('country')
->get();
$mapData = "";
$locationData = array();
foreach ($locations as $location) {
$code = DB::table('country_code')
->select('code')->where('country', $location->country)
->orderBy('country')
->first();
$mapData = $mapData . '"' . $code->id . '":"' . $location->count . '",';
}
return $mapData;
}
Problem was fixed when I changes the Collation of the database

Merge arrays in php

In the above code in each foreach loop i get a seperate array every time . I wanna combine all these array . I have tried array_merge but it is not working . Is there any other way?
Some of the results are two dimentional array
foreach ($location as $loc)
{
$query = "select name
from locations l where l.location_id = $loc";
$query = $this->db->query($query);
$data = $query->result_array();
}
Thanks in advance.
Either use array_merge():
$data = array();
foreach ($location as $loc) {
$query = "SELECT name FROM locations l WHERE l.location_id = " . $loc;
$query = $this->db->query($query);
$data = array_merge($data, $query->result_array());
}
...or merge the location id's first and then do a single query, which is much faster:
$in = implode(', ', $location);
$query = "SELECT name FROM locations l WHERE l.location_id IN (" . $in . ")";
$query = $this->db->query($query);
$data = $query->result_array();
$q = 'SELECT `name` FROM `locations` AS `l` WHERE ';
foreach ($location as $loc) {
$q .= "`l`.`location_id` = '$loc' OR ";
}
$q = rtrim($q, 'OR ');
$query = $this->db->query($q);
$data = $query->result_array();

query with parentheses in zend framework 2.2

I want my query like this:
SELECT tbl_bids. * , tbl_department.vDeptName, tbl_user.vFirst
FROM tbl_bids
LEFT JOIN tbl_bids_department ON tbl_bids_department.iBidID = tbl_bids.iBidID
LEFT JOIN tbl_department ON tbl_department.iDepartmentID = tbl_bids_department.iDepartmentID
LEFT JOIN tbl_user ON tbl_user.iUserID = tbl_bids.iUserID
WHERE tbl_user.iUserID = '1' // with parantheses in where clause
AND (
tbl_department.vDeptName = 'PHP'
OR tbl_department.vDeptName = 'android'
)
GROUP BY tbl_bids.iBidID
ORDER BY iBidID DESC
LIMIT 0 , 30
But i can't find the way to get parantheses in my query,there are mutiple condition and loop will be there to make where clause..
here is my code
$select = $this->tableGateway->getSql()->select();
$select->columns(array('*'))
->join('tbl_bids_department', 'tbl_bids_department.iBidID = tbl_bids.iBidID', array(),"LEFT")
->join('tbl_department', 'tbl_department.iDepartmentID = tbl_bids_department.iDepartmentID',array(tbl_department.vDeptName),"LEFT")
->join('tbl_user', 'tbl_user.iUserID = tbl_bids.iUserID',array(tbl_user),"LEFT")
->group('tbl_bids.iBidID');
$where = new \Zend\Db\Sql\Where();
$where->equalTo( 'tbl_bids.eDeleted', '0' );
$sWhere = new \Zend\Db\Sql\Where();
for ( $i=0 ; $i<count($aColumns) ; $i++ )
{
if (isset($data['sSearch_'.$i]) && $data['sSearch_'.$i] != "")
{
if($aColumns[$i] == 'vDeptName'){
$allDept = explode(',', $data['sSearch_'.$i]);
foreach ($allDept as $key => $value) {
if($key == 0)
$sWhere->AND->equalTo("tbl_department.vDeptName", $value);
else
$sWhere->OR->equalTo("tbl_department.vDeptName", $value);
}
}elseif($aColumns[$i] == 'vFirst')
$sWhere->AND->equalTo("tbl_user.iUserID",$data['sSearch_'.$i]);
else
$sWhere->AND->like("tbl_bids.".$aColumns[$i], "%" . $data['sSearch_'.$i] . "%");
$select->where($sWhere); // here my where clause is create
}
}
//var_dump($select->getSqlString());
$resultSet = $this->tableGateway->selectWith($select);
return $resultSet;
}
I have others many fields to pass through where which also have same problem of paratheses
if there is no any condition i can use nest() and unnest() predicate , but it will show me that string is not nested error,
So pls help me to find the solution.
Pls attach example with solution.
here is a short example
$where = new Sql\Where();
$where->equalTo('col',thirdVal')
->NEST //start braket
->equalTo('col','someVal')
->OR
->equalTo('col','secondVal')
->UNNEST //close bracet
hope this will help

PDO multiple LIKE causes; how to?

function searchPaste($string, $err=false)
{
global $sql;
$buffer = $err?"SELECT * FROM `pastes` WHERE `exposure` = 'public' AND `title` LIKE ? OR `paste` LIKE ? OR `lang` LIKE ?":"SELECT * FROM `pastes` WHERE `exposure` = 'public' AND `title` LIKE ?";
echo $buffer."<br>";
$buffer = $sql->prepare($buffer);
$buffer->execute(array(sprintf("%%%s%%", $string)));
if(!$buffer->rowCount()>0)
return 0;
return $buffer->fetchAll(PDO::FETCH_OBJ);
}
As you see in my first query I am matching by multiple cases problem is "?" is only handled once and I'm unsure how I could go about using an array to do this for all causes.
Anyone know what I could do?
You need to provide a bind value / param for each placeholder in your query. You're going to have to do away with the ternary I think. You could try something like this...
$buffer = "SELECT * FROM `pastes` WHERE `exposure` = 'public' AND `title` LIKE ?";
$paramCount = 1;
if (!$err) {
$buffer .= ' OR `paste` LIKE ? OR `lang` LIKE ?';
$paramCount += 2;
}
$stmt = $sql->prepare($buffer);
$stmt->execute(array_fill(0, $paramCount, "%$string%"));
function searchPaste($string, $err=false)
{
global $sql;
$sql->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
$query = "SELECT * FROM `pastes` WHERE `exposure` = 'public' AND (`title` LIKE :search";
if ($err) {
$query .= " OR `paste` LIKE :search OR `lang` LIKE :search";
}
$query .= ")";
$stmt = $sql->prepare($query);
$stmt->execute(array("search" => "%$string%")));
return $stmt->fetchAll(PDO::FETCH_OBJ);
}
finally got the logic and fixed the query.

MySql LIKE returns false if search term is same as entire string in the column, why is that?

So I have following as part of my query
SELECT * FROM $table WHERE columname LIKE '%$searchterm%'
I have tried taking out leading and/or ending wildcards meaning
SELECT * FROM $table WHERE columname LIKE '$searchterm%'
AND
SELECT * FROM $table WHERE columname LIKE '%$searchterm'
AND
SELECT * FROM $table WHERE columname LIKE '%$searchterm%' OR columname LIKE '$searchterm'
and also tried adding following to the query with no luck
OR columname = '$searchterm'
So when my search term is "myval" and if column has whole string "myval", I would like to have that selected. But ALL of my queries above, return false/return nothing where myval is searchterm and column value as full.
I can not use MATCH because this is not Full-Text index.
EDIT:
PHP Code:
$sterm = NULL;
$table = 'mytable';
if(isset($_GET['s'])) { $sterm = explode(" ", mysql_real_escape_string($_GET['s'])); }
if(isset($_POST['s'])) { $sterm = explode(" ", mysql_real_escape_string($_POST['s'])); }
if(!empty($sterm)){
$getdata = "SELECT * FROM $table WHERE termsi != 'Special' ";
foreach ($sterm as $value){
$getdata .= "AND netid_all LIKE '%$value%' OR netid_all = '$value' ";
} //End foreach
$getdata .= "LIMIT 10";
$result = mysql_query($getdata) or die(mysql_error());
$row = mysql_fetch_array($result, MYSQL_ASSOC);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo <<<PRINTALL
{$row[0]}, {$row[1]}, {$row[2]}, {$row[3]}, {$row[4]}, {$row[5]}, {$row[6]}, {$row[7]}, ' <br />'
PRINTALL;
} //End While
} //End If search exists
Okay So As you guys suggested, i tried PHPMyAdmin sql console and it works fine, so it would have to be by PHP!? so here it is.
I'd suggest writing your query building like this:
$fullvalues = array();
$partials = array();
foreach ($sterm as $value){
$partials[] = "(netid_all LIKE '%" . mysql_real_escape_string($value) . "%')";
$fullvalues[] = "'" . mysql_real_escape_string($value) . "'";
}
$partials = implode(' OR ', $partials);
$fullvalues = implode(', ', $fullvalues);
$sql = <<<EOL
SELECT *
FROM $table
WHERE (termsi != 'Special')
AND (($partials) OR (netid_all IN ($fullvalues));
EOL;
Assuming your search string is a b c, you'd get this query:
SELECT *
FROM yourtable
WHERE (termsi != 'Special')
AND (((netid_all LIKE '%a%') OR (netid_all LIKE '%b%') OR (netid_all LIKE '%C%')) OR (netid_all IN ('a', 'b', 'c')))
If your search requires that all terms be present, then change the 'OR' to 'AND' in the implode.
Well found it,
$row = mysql_fetch_array($result, MYSQL_ASSOC);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
Was the problem, earlier when I was testing things, anyhow, it should have been the following
$row = mysql_fetch_array($result, MYSQL_ASSOC);
while($row)