Echo specifc id content

HI, i have a database table structure as:

I making a site that is multilanguage and throughout the site i need to echo specific keywords and phrases.

Previously i added the text via a php file using:

if ($lang == ‘en’)
{
$f1 = “Latest News”;
$f2 = “Featured Articles”;
}

if ($lang == ‘it’)
{
$f1 = “Ultime notizie”;
$f2 = “Articoli”;
}

Is it possible to construct a database table such as:
key_id | tx_en | tx_it | tx_ru

then, retrieve the data just once using:

SELECT keyid, text_“.$lang.”

echo the language specific text based on a specific id, something like echo tx_'.$lang (where key_id = X)?

I think I’d use a separate row based on key_id and language_code, and just return the text, rather than trying to store the text for each language in the same row. It would obviate the need to alter the table layout when you decide to add support for an extra language, too.

If i wanted another language i’d just add another column…

To clarify, i only want to query the record once and retrieve all the data from tx_“.$lang.”

Throughout the pages, the tx_“.$lang.” will be dotted around the here and there, so echo tx_“.$lang.” with id=17 here and echo tx_“.$lang.” with id 45 there etc etc

OK, if that’s your preference. Either way is possible, simply use the language code to build your query when retrieving the text. Might make it slightly more readable to use the “as” modifier, as in “select keyid, text_$lang as text” so when you use the text, that can always be the same.

Of course. I just figured it would be easier to add the data into an existing table rather than adding another column and having to change the layout. Also would get around the issue above as you’d just be doing “select keyid, text where keyid=x and langcode=y”, returned text would always have the same name.

You should design the database so that you can add as many extra languages as you like without changing the database.

Needding to change the database indicates extremely bad database design.

Read up on database normalization to find out how to do it properly.

Itll take 2 seconds to add another column, and as i created a dynamic cms, all form fields to add data are created on the fly, so i don’t understand your point…and similarly i don’t understand this response against my inital question

I think it would be more like
SELECT text_" . $lang . " FROM mytable WHERE keyid = 2
no?

As for normalization, your db design should be OK as long as the table is relatively small.

But IMHO it is not “robust” and will become more cumbersome and resource intensive if / when the table grows.

I think I would have separate tables for each language and do
SELECT the_text FROM $lang WHERE keyid = 2

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.