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

  • SEARCH
  • Home
  • 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 8271153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:46:21+00:00 2026-06-08T06:46:21+00:00

I have a function in my Teacher model which returns categories array. getCaterogies() {

  • 0

I have a function in my Teacher model which returns categories array.

getCaterogies() {
   return array('1' => 'short tempered', '2' => 'funny', '3' => 'visionary', ...);
}

I am storing indexes in database and displaying values everywhere using the value of the array corresponding to that..

$categories = $teacher->categories;
$category = $categories[$teacher->category];

I am doing this because once somebody suggested to me not to store strings in a database which are statuses, instead store integer values, and either store the conversion in the database or define it in ht model. The problem with strings is that they are more prone to human errors in comparisons. Maybe because of case sensitiveness.

Now the problem I am facing is that while displaying values in gridview, I need to write the 2 lines in a value field, but it is an expression, and outside variables also it doesn’t take.

How can I get this working for gridview?

  • 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-08T06:46:22+00:00Added an answer on June 8, 2026 at 6:46 am

    You can use anonymous function as value which can take $row, $data params where $row holds the row number (zero-based) and $data contains the data model for the row.

    That way you can have it defined inside only.

    $this->widget('zii.widgets.grid.CGridView', array(
        'dataProvider'=>$dataProvider,
        'columns'=>array(
            array(
                'name'=>'..',
                'value'=>function($data,$row){
                    $categories = $teacher->categories;
                    return $categories[$data->category];
                },
            ),
        ),
    ));
    

    And if you want to use it from outside, you can rely on PHP’s use:

    $categories = $teacher->categories;
    $this->widget('zii.widgets.grid.CGridView', array(
        'dataProvider'=>$dataProvider,
        'columns'=>array(
            array(
                'name'=>'..',
                'value'=>function($data,$row) use ($categories){
                    return $categories[$data->category];
                },
            ),
        ),
    ));
    

    I would personally recommend second one, because that way the calculation of the array will be only once and will be used in all cases.

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

Sidebar

Related Questions

I have a function named resize, which takes a source array, and resizes to
I have this $ids = array_map(array($this, 'myarraymap'), $data['student_teacher']); function myarraymap($item) { return $item['user_id']; }
i have function public Menu Details(int? id) { return _dataContext.Menu.Include(ChildMenu).FirstOrDefault(m => m.MenuId == id);
I have function some_func_1 which will create an object of type some_type and will
Inside my Controller i have function that runs after user clicks on item, which
Lets have the following code : ( function($) { TeacherModel = Backbone.Model.extend({ defaults :
Basically, I have a Linked List which implements a display() function that simply loops
I have a data model in Doctrine/symfony. I have a 'Course' which has many
I have a CGridView widget for Lesson model $this->widget('zii.widgets.grid.CGridView', array( 'id'=>'lesson-grid', 'dataProvider'=>$model->search(), 'filter'=>$model, ...
I have function which append div (help icon) after all element with .help class.

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.