Reputation: 20173
Let's say I have:
class language_text{
protected $id;
protected $keyword;
protected $language;
protected $text;
function __construct($clave,$lan){
$consulta = mysql_query("SELECT id,clave,lengua,texto,id_usuario FROM textos WHERE clave = '$clave' AND lengua = '$lan'");
if(mysql_num_rows($consulta)>0){
while($item = mysql_fetch_array($consulta)){
$this->id = $item['id'];
$this->clave = $item['clave'];
$this->lengua = $item['lengua'];
$this->texto = $item['texto'];
$this->id_usuario = $item['id_usuario'];
$this->exists = true;
}
return true;
}else{
$this->exists = false;
$this->clave = $clave;
return $this->clave;
}
}
}
function get_texto_clave_html($clave){
$clave = htmlspecialchars($clave);
$lan = detectarIdioma();
$temporal = new texto($clave, $lan);
if($temporal->exists()==true){
return $temporal->get_texto_html();
}else{
return $clave;
}
}
}
So everytime I need some language text I call: get_texto_clave($lan, $keyword)
, but in every page might be about 25 texts, so I think I should load all language in an array and then access it instead of the database.
For example:
get_texto_clave(2,'welcome_html')
;get_texto_clave(2,'bye_bye_html')
;Questions:
Thanks to experimentX contribution, here we have current solution:
Use static method to get the instances of object.
$data = language_text::getData($lan, $keyword);
And on getData method
public function getData($lan, $keyword)
{
$data = array();
//query here
$consulta = mysql_query("SELECT id,clave,lengua,texto,id_usuario FROM textos WHERE clave = '$clave' AND lengua = '$lan'");
while($item = mysql_fetch_array($consulta))
{
$selfitem = new self;
$selfitem->id = $item['id'];
$selfitem->clave = $item['clave'];
...
...
$data[] = $selfitem;
}
return $data[]; //end the end you send an array of data
}
Now $data will contain an array of objects. Use the loop to access its properties.
foreach($data as $d)
{
echo $d->id;
echo $d->clave;
...
...
}
but I can't see it yet,
I asume in $data we have all language text for current languageId, so, how i can i extract, for example, 'welcome_body' from $data?
Upvotes: 1
Views: 168
Reputation: 14318
UPDATE::
I think database is not quite suited for this kind of operation. I would advise you to revise your database design.
+----+---------------+---------------+----------+
| id | message_type | message_text | language |
+----+---------------+---------------+----------+
| 1 | good morning | good morning | english |
| 2 | good_morning | something.... | spanish |
+----+---------------+---------------+----------+
First create a database like
Create a database object to query database like this one.
//do not use keyword, its no use at all
//Of course it's pointless say good morning and at the end ...goodbye in some language
//and also logically, it's useless because we have to query data
$message = language_text::getData($lan);
Your query must be something like this SELECT* FORM message WHERE language='english'
or something. //also it's better to add DISTINCT
for selecting. I will explain later why.
And use the the content message_type
type to add property to the object.
while($item = mysql_fetch_array($consulta))
{
$selfitem = new self;
$selfitem->$item['message_type'] = $item['message_type'];
$data[] = $selfitem;
}
return $data;
And now echo out the property echo $data->hello_message;
or goodbye message
Now, you have to consider that no two duplicate entry for message_type
should be added for same language. So, it's better to add DISTINCT
in select statement. And also you have to check it.
OTHER WAY:: You can also create database with static messages, its not flexible, but reliable, and can be used with the other method i presented to you.
+----+------------+--------------+-------------+
| id | language | goodmorning | goodnight |
+----+------------+--------------+-------------+
| 1 | english | good morning | good night | -- and so on and on
| 2 | spanish | something....| .......... | -- and so on and on
+----+---------------+-----------+-------------+
NOTE::Usually the content of page is loaded at once than to load one by one.
PS::Your english is terrible.
Upvotes: 1