Jump to content

Recommended Posts

Hi, i have a db table in this structure:

 

langid |  tx_en        | tx_it             | tx_ru

 

1           welcome    benvenuto    Добро пожаловат

2           contact      Contatto       Контактная

 

 

etc, etc

 

After the initial query of 'SELECT tx_".$lang." FROM langs' , is it possible to echo via php these values, however id or record number based? Such as 

 

<?php echo $row_langs['tx_'.$lang] (WHERE langid = 2); ?> or <?php echo $row_langs['tx_'.$lang] (WHERE langid = 1); ?> ?

 

Throughout pages i have 10 - 20 different keywords and phrases stored in the above table that need to be echo throughout page. Rather than making 20 queries (to retrieve specifc id's) per page, i thought just make one query and echo the data after....is this actually possible or should i rethink?

 

Thanks in advance 

Link to comment
https://forums.phpfreaks.com/topic/299647-echo-specific-row/
Share on other sites

for ease of use and code readability, you need to end up with the language strings either in defined constants or an array, with the defined constant name or the array index indicating the meaning of the string. you can dynamically produce the defined constants/array from wherever the data is actually stored at.

 

for these two examples, the Italian language was selected. only the building of the defined constant/array is dependent on the language choice. the actual usage in the code is the same regardless of the language choice.

 

if you use the defined constant method - 

// definition
define('LANG_contact', 'Contatto'); // the define() statements can be dynamically called if the data is stored in a database table.

// usage
echo LANG_contact;

if you use the array method - 

// definition
$lang['contact'] = 'Contatto'; // the array can be build dynamically if the data is stored in a database table.

// usage
echo $lang['contact'];
Link to comment
https://forums.phpfreaks.com/topic/299647-echo-specific-row/#findComment-1527578
Share on other sites

Thanks...so.....

 

From an array perspective, would it be possible to create string based in id, i.e.


$lang['2'] = 'Contatto';

echo $lang['2'];

..as i havent done one before, would the code by something like this?

$array1 = array();
$result = mysql_query("SELECT langs.tex_".$lang.", langs.langid FROM langs");
while ($row = mysql_fetch_assoc($result)) {
   $lang=$row['tex_".$lang."'];
    $langs[] = $lang;
}
Link to comment
https://forums.phpfreaks.com/topic/299647-echo-specific-row/#findComment-1527580
Share on other sites

Your main problem right now is that your database schema is very poor. Cryptic IDs and language names buried in column names will turn this trivial task into a PITA.

 

So before you do anything, I recommend you fix your database structure. For example:

languages
- code CHAR(2) PRIMARY KEY (a unique language code, e. g. the two-letter ISO 639-1 code) 
- name TEXT (e. g. “English”)

items (the items to be translated)
- id VARCHAR(100) PRIMARY KEY (a textual identifier of the item, e. g. “welcome”)

translations
- item VARCHAR(100) REFERENCES items (id)
- language CHAR(2) REFERENCES languages (code)
- translation TEXT
- PRIMARY KEY (item, language)

So the translations are stored as individual rows and can easily be accessed:

item    | language | translation
--------+----------+----------------
welcome | en       | welcome
welcome | it       | benvenuto
welcome | ru       | Добро пожаловат
SELECT
  item,
  translation
FROM
  translations
WHERE
  language = :language_code
  • Like 1
Link to comment
https://forums.phpfreaks.com/topic/299647-echo-specific-row/#findComment-1527581
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.