We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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("is_active = 1"), "useEmpty" => true, "emptyText" => "Please select", "using" => array("country_id", "country_name"), )); ?>

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

        how can remove dupilcate entries..

Use DISTINCT or GROUP BY sql statment



43.9k
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.