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:
- I want welcome text under Spanish
get_texto_clave(2,'welcome_html'); - I want goodbye text under Spanish
get_texto_clave(2,'bye_bye_html');
Questions:
- What do you think about it?
- Would you add a new class like language_text_collection or something like that?
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?