Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

how to get only unique elements(no duplicate) in dropdown box in phalcon

how to get only unique elements(no duplicate) in dropdown box in phalcon.....below is my code

<?php echo $this->tag->select(array("country", TblCountry::find("isactive = 1"), "useEmpty" => true, "emptyText" => "Please select", "using" => array("countryid", "country_name"), )); ?>

        the country name here maybe same for various ids....

        how can remove dupilcate entries..

Use DISTINCT or GROUP BY sql statment



30.7k
Accepted
answer

Hi,

use in your request:

TblCountry::find([
    "is_active = 1"
    "distinct" => "country_name"
)]

by the way, it's better mvc practice to put that query in the controller and pass the result to the view::

//controller
$countries = TblCountry::find([
    "is_active = 1"
    "distinct" => "country_name",
    "columns" => "country_id, country_name" // fetch just the needed data
)];

$this->view->setVar("countries", $countries);

//view
echo $this->tag->select(array("country",$countries, "useEmpty" => true, "emptyText" => "Please select", "using" => array("country_id", "country_name")));

And last, if you plan to heavy use that country list in your app, you can cache the result.