English 中文(简体)
Yii: Customize the results of CAutoComplete
原标题:

I need to make a dropdown list using CAutoComplete. Everything is set and works fine, here is my code of the action:

<?php
    public function actionSuggestCharacter() {
        if(Yii::app()->request->isAjaxRequest && isset($_GET[ q ])) {
            $name = $_GET[ q ]; 
            $criteria = new CDbCriteria;
            $criteria->condition= `Character` LIKE :keyword ;
            $criteria->params=array( :keyword =>"$name%");
            $criteria->limit = 5;
            $suggestions = zCharacter::model()->findAll($criteria);
            $returnVal =   ;
            foreach($suggestions as $suggestion) {
                $returnVal .= $suggestion->Character."
";
            }
            if (isset($suggestion)) {
                echo $returnVal;
            }
            $criteria->condition= `Character` LIKE :keyword ;
            $criteria->params=array( :keyword =>"%$name%");
            $criteria->limit = 5;
            $suggestions = zCharacter::model()->findAll($criteria);
            $returnVal =   ;
            foreach($suggestions as $suggestion) {
                $returnVal .= $suggestion->Character."
";
            }
            if (isset($suggestion)) {
                echo $returnVal;
            }
        }
    }
?>

What this code does is that it shows the first 5 matches with the keyword at the beginning and the next 5 matches are with the keyword in any place.

Example. Let s say a user types in the input field "pdd" (doesn t really matter, could be any text), so the results returned by autocomplete will look like:

1. pddtext...
2. pddtext...
3. pdd_some_other_text
4. pdd_text
5. pdd_text
1. text_text_pdd
2. text_pdd_text
3. etc...

The problem is I need to separate these two blocks by some kind of line (<hr> or <div> with the border). How can I do this?

Thank you.

问题回答

Can t you do something like this?

<?php
    public function actionSuggestCharacter() {
        if(Yii::app()->request->isAjaxRequest && isset($_GET[ q ])) {
            ...
            if (isset($suggestion)) {
                echo $returnVal;
            }
            echo "Hey this is the delimiter
";
            $criteria->condition= `Character` LIKE :keyword ;
            ....
        }
    }
?>

And then on the client side check for this string and when you encounter ""Hey this is the delimiter" replace it with your separator.





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签