Reputation: 1339
I have a database that contains the name of some buttons and icons for two languages. the table is called icons and it has 4 fields (id, abbr, lang1, lang2) where abbr is the name of the variable I want to create and if the language is set to lang1 to assign the cell lang1 to the variable.
Example of the table:
id abbr lang1 lang2
-----------------------------------------------
1 air AirConditioner Condizionatore
2 pool Pool Piscina
I know that I can do that with something like that
foreach (array('wifi','fridge','air','balcony','kitchen','tv','bathroom','pool') as $varname) {
if ($$varname==1)
{
SQL Select to get the information for each field
}
But I was thinking that it will be nicer if I can do that dynamically, in case I add additional rows in the table
The desired result will be to have something like this created automatically for each row
if ($lang==lang1)
{
$air=$row['lang1'];
$pool=$row['lang1'];
}
else
{
$air=$row['lang2'];
$pool=$row['lang2'];
}
I have searched if someone faced this before, but couldn't find anything usefull for my case, so any help will be welcome.
Thanks
Upvotes: 0
Views: 282
Reputation: 135
If I mean right $lang
is 1 or 2, this code may help.
$language = 'lang'.$lang; // creates lang1 or lang2
$result = mysql_query("SELECT * FROM icons;");
while ($row = mysql_fetch_assoc($result))
{
$varname = $row['abbr'];
$$varname = $row[$language];
}
Upvotes: 1
Reputation: 1362
The simplest dynamic way I can think of is that you can query the abbr
you want, then pass the $lang
variable as a selector to select the column of the language you want, depending on your code you can do this:
${$row['abbr']}=$row[$lang];
Upvotes: 1