Skip to content

Instantly share code, notes, and snippets.

@amirkhiz
Last active January 6, 2019 14:52
Show Gist options
  • Save amirkhiz/5863acf11430e8941703fdfcb07d7cdd to your computer and use it in GitHub Desktop.
Save amirkhiz/5863acf11430e8941703fdfcb07d7cdd to your computer and use it in GitHub Desktop.
General Search in all tables of site
class Controller {
public function search()
{
$kelime = $_POST['query'];
$result = $this->site_model->search($keyword);
foreach ($result as $item) {
switch ($item->type) {
case 'products':
$item->id = site_url('products/' . $item->id);
break;
case 'posts':
$item->id = site_url('posts/' . $item->id);
break;
case 'services':
$item->id = site_url('services/' . $item->id);
break;
case 'articles':
$item->id = site_url('articles/' . $item->id);
break;
}
}
echo json_encode($result);
}
}
Class Model {
public function search($keyword)
{
$query = "
SELECT
id,
urun_adi AS ad,
'urun' AS type
FROM en_urunler
WHERE urun_adi LIKE ('%{$keyword}%')
UNION ALL
SELECT
id,
isim AS ad,
'sayfa' AS type
FROM en_sayfa
WHERE isim LIKE ('%{$keyword}%')
UNION ALL
SELECT
id,
isim AS ad,
'basinda' AS type
FROM en_basinda
WHERE isim LIKE ('%{$keyword}%')
";
$result = $this->db->query($query)->result();
return $result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment