0

I have an array:

$type = array(
    0 => "Car",
    1 => "SUV",
    2 => "Truck",
    3 => "Bike"
);

When a user selects the options from a form, the associated value is stored in a database.

I want this info to be displayed on another page in a table. Currently it only displays the database saved value (0,1,2,3), but i want it to show as ((Car, SUV, Truck, Bike). How do i do this?

I need a way to make it know that 0=Car, 1=SUV, 2=Truck, 3=Bike.

heres what the table looks like now: enter image description here

The make column represents the vehicle manufacturer (Toyota, Honda, etc)and type represents (Car, SUV, Truck, Bike).

3 Answers 3

1

In the page where you are displaying the data define the array you already defined in the form page i.e,

$type = array(
            0 => "Car",
            1 => "SUV",
            2 => "Truck",
            3 => "Bike"
        );

Now while displaying the column use echo $type[database saved value];

1
  • Thank you!... i used you suggested code and placed my original code inside [ ] and it worked perfect! :)
    – Nxlevel
    Apr 29, 2015 at 22:23
1

A little vague. If this information is saved to a collection & you're displaying it in a grid:

$this->addColumn('type',
    array(
        'header'=> $this->__('Type'),
        'index' => 'type',
        'type'  => 'options',
        'options'   => array(
            0   => 'Car',
            1   => 'SUV',
            2   => 'Truck',
            3   => 'Bike'
        )
    )
);

This will automatically associate the database values with the correct output for you, and allow you to filter by them.

Alternatively, however you are displaying it, just output it accordingly replacing the values with their relevant option value, i.e 0 = Car, 1 = SUV, etc.

1

Your question is a kind of "two broad". However I will show how magento manages such situations.

Basically, Magento will create an option array class for this kind of operation. That class will hold a method toOptionArray() which will hold label-value relationship as an array. When it is required to show these options, it will call this class and retrieve the array and then show in frontend.

You can find a classic example in app/code/core/Mage/Adminhtml/Model/System/Config/Source/Yesno.php. ie the class Mage_Adminhtml_Model_System_Config_Source_Yesno .

So this is what you need to do

File : app/code/local/Namespace/Module/Model/Options.php

<?php
class Namespace_Module_Model_Options
{
    public function toOptionArray()
    {
        $type = array(
            0 => "Car",
            1 => "SUV",
            2 => "Truck",
            3 => "Bike"
        );
        return $type;
    }
}

Now what you need to do is, collect the option value from database and then use this custom class for getting the label.

Hope that makes sense.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.