Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9017479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:17:06+00:00 2026-06-16T04:17:06+00:00

I have three const in controller: const TEST1 = 1; const TEST2 = 2;

  • 0

I have three const in controller:

const TEST1 = 1;
const TEST2 = 2;
const TEST3 = 3;

How I can call these values in view as a dropDown?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T04:17:07+00:00Added an answer on June 16, 2026 at 4:17 am

    If you will get messy with all constants you have in diferent models, you can use code snippet I wrote to manage them. Please see Managing constants easily (yii wiki article)

    I will copy just function and one example, but for full wiki and details please go visit link above.

    Put this method in parent class or your model class directly:

    class ActiveRecord extends CActiveRecord {
        const TEST_1 = 1;
        const TEST_2 = 2;
        const TEST_3 = 3;
    
    
        /*
            Get class constants by token.
            If you set constants with same prefix, like:
            TEST_1
            TEST_2
            TEST_3
    
            , you can get it by calling
            Class::getConstants('TEST_');
        */
        public static function getConstants($token,$objectClass) {
            $tokenLen = strlen($token);
    
            $reflection = new ReflectionClass($objectClass); //php built-in 
            $allConstants = $reflection->getConstants(); //constants as array
    
            $tokenConstants = array(); 
            foreach($allConstants as $name => $val) {
            if ( substr($name,0,$tokenLen) != $token ) continue;
            $tokenConstants[ $val ] = $val;
            }
            return $tokenConstants;
        }
    
    }
    

    And after that you can use this method to get specific constants (from one group) in array:

    self::getConstants('TEST_',__CLASS__); //inside same class
    ActiveRecord::getConstants('TEST_','ActiveRecord'); //outside, somewhere else in view or controller
    

    For dropdown it would be looking as (if MyModel have parent ActiveRecord)

    echo CHtml::dropDownList('name','selected',
        MyModel::getConstants('TEST_','MyModel'),
        array(// for htmlOptions
        )
    );
    

    Now you can forget to edit all places in code if you adding new constant. It will automaticaly added to all your dropdowns or whatever.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a hard time understanding the difference between these three: const char *
I have some view controller, that uses my own view class and XIB interface,
I have the below ajax call. jQuery.post('index.php',{ 'option' : 'com_one', 'controller': 'product', 'task' :
Have three classes User, Group and Field. Many to many relationship on User /
Have three divs in a container that I want to float over a large
i have three lists with the same number of elements in each other, i
I have three separate table variables in my Function, 1 of them is not
I have three files: lib.c lib.h => They should be built as a .so
i have three java based web application app1,app2 and app3 at production. All 3
I have three tables. I have to retrieve the data using Linq statement. My

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.