Jump to content

Recommended Posts

Hello, I searched on the web and this forum, cant get what im looking for, maybe someone can help me with this.

 

Im working on some classes for a CMS (own project) I wanted to make everything language independent, so i can change the language without having to edit all the website. This was the solution i choose: Constants. I use a PHP file for every language with a list of constants, so the key words that would be static, change depending which language file is loaded.

My problem: when i get data from a SQL, lets say it gets a list, and save it to a variable, it doesnt change:

 

Example

define(L_IMG,"image");

 

$register= "L_IMG" (get it from SQL query)

echo $register;

 

L_IMG should appear as "image", but it appears as "L_IMG"... so, is there a way to force the variable to get the constant value? another way to do it?

Link to comment
https://forums.phpfreaks.com/topic/241550-constat-for-multilanguage-site-problem/
Share on other sites

Dont place constants within quotes. Constants are not parsed within strings.

 

Display a constants value

echo CONSTANT_NAME_HERE;

 

Using a constant within a string, you'd use concatenation

echo "some text here " . CONSTANT_NAME_HERE . " more text here";

Ok, thanks for your answer,  but my problem really is that the constant i want to change is loaded from a register of MySQL, the "L_IMG" its actually the answer from the query, its not written in the code. That is what i want to replace to the constant value, I have used constants as you indicated, no problem with that, the problem is when the constant is loaded from the query and then is echoed. Another problem is that the php can get from mysql either constants to be replace or real info.

Maybe there is a better way to do this?

Hi,

 

Just make it simple:

 

There are two ways to achieve:

 

Number One: Using CONSTANT

arabic.php

define("WEB_TITLE", "Welcome To My Site - Arabic Site");

 

 

english.php

define("WEB_TITLE", "Welcome To My Site - ENGLISH Site");

 

Based On the Language load the above file

and Print

 

<?php

echo WEB_TITLE;

?>

 

 

Number Two: Using SQL

 

 

TABLE phrases:

Phrase_ID Language Phrase_Name Phrase_value

 

1                      arabic            WEB_TITLE      WELOCME TO ARABIC

2                      english          WEB_TITLE      WELOCME TO ENGLISH

 

 

$phrases ="";

$sql = "select * FROM phrases where Language = '".Language."'";

if($temps = $DB_site->query($sql))

{

while ($temp = $DB_site->fetch_array($temps))

{

$phrases[$temp['Phrase_Name']] = $temp['Phrase_value'];

 

 

}

}

 

Print the VAlues:

echo $phrases[WEB_TITLE];

echo $phrases[WEB_ADDRESS];

 

 

 

Thank you, all the best

 

 

 

 

Thanks both for your answers, partly solve some doubts, but the problem I have was a little more specific. I finally got the solution:

 

I needed to read form SQL a string that may o may not be a constant (for language portability purposes) so what i did was:

 

$salida=defined($registro)?constant($registro):$registro;

 

It may read a little slow... but nowadays it wont be too much of a problem.

 

If you see some better way to do it? im open to other options.

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.