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).

Moose
7,5057 gold badges50 silver badges92 bronze badges
asked Apr 28, 2015 at 2:56

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];

answered Apr 28, 2015 at 8:28
1
  • Thank you!... i used you suggested code and placed my original code inside [ ] and it worked perfect! :) Commented 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.

answered Apr 28, 2015 at 6:31
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.

answered Apr 28, 2015 at 6:51

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.