Skip to content

Instantly share code, notes, and snippets.

@Maxcorp
Created April 4, 2014 06:19
Show Gist options
  • Save Maxcorp/9969160 to your computer and use it in GitHub Desktop.
Save Maxcorp/9969160 to your computer and use it in GitHub Desktop.
public function actionSearch()
{
if(!empty($_GET['search_text']) && is_array($_GET)) {
$condition = "";
$search_text = urldecode($_GET['search_text']);
$words = explode(" ", $search_text);
$total_words = count($words);
if($total_words) {
for($i=0; $i < $total_words; $i++) {
if($i > 0 && $i != $total_words) {
$condition .= " OR s1.name LIKE :search_text{$i} OR s1.content LIKE :search_text{$i} ";
} else {
$condition .= " s1.name LIKE :search_text{$i} OR s1.content LIKE :search_text{$i} ";
}
}
}
$query = "SELECT
DISTINCT s1.id,
s1.name,
s1.url_alias,
s2.url_alias as root_section_url,
s1.pid
FROM
tbl_sections s1
LEFT JOIN
tbl_sections s2
ON
(s1.pid=s2.id)
WHERE
{$condition}";
$command = Yii::app()->db->createCommand($query);
if(!empty($words)) {
for($i=0; $i < $total_words; $i++) {
$word = "%{$words[$i]}%";
$command->bindValue(":search_text{$i}", $word, PDO::PARAM_STR);
}
}
$search_results = $command->queryAll();
$this->render("search", array("search_results"=>$search_results));
} else {
throw new CHttpException(404);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment