Hi,

I have a table like the following one,

A,aaa
B,bbb
C,ccc
...

Suppose this table is in a database(Postgresql) or in the PHP code (array).

Using this table I would like to generate code like this (as I have many more rows in the original table)

switch($myvar)
{
case "A":
$varname=aaa;
break;
case "B":
$varname=bbb;
case "C":
$varname=ccc;
break;
....
}

I guess I could use a function like for look up in the table, to do the association (e.g. A -> aaa), but don't know which one is available in Drupal ...
What is a common Drupal/PHP strategy to achieve that ?...

Thanks,

Manuel

Comments

Santiago-1’s picture

... and sorry for my basic question ...

nevets’s picture

It depends on how big the "table" is, if not too large (you get to decide what that means) you could simple use

$mytable = array(
'A' => 'aaa',
'B' => 'bbb',
'C' => 'ccc',
);

$varname = $mytable[$myvar];

You could make $mytable static and place in a function if you need to do the lookup from more than one function.

You could also implement a module that has a *.install file, creates the table and populates it.

Santiago-1’s picture

I have over 100 rows, although your array code represents an improvement to what I was doing, it's still hardcoding the content into PHP code ...of course you followed my hint of using arrays ...
The other idea of a module sounds interesting, let me see what I could do ...
thank you for your help ...

Santiago-1’s picture

Really, yes, thank you Terry!