1

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?

7
  • what are you trying to do exactly??? Commented Apr 6, 2011 at 11:21
  • i am asking if it's a good idea to load ALL texts from databace at ONCE instead of loading each one separately with it's own query Commented Apr 6, 2011 at 11:42
  • sorry i was offline, could you tell me how did you achieve this in your previous method. Commented Apr 7, 2011 at 5:16
  • well, told you, i single query to get each $String = get_texto_clave(3, 'hola'); ----> 'hello' (is that it?) Commented Apr 7, 2011 at 9:37
  • @ToniMichekCaubel Do you mean that you want to load all messages?? I would advise you to revise your database structure. Commented Apr 7, 2011 at 11:05

1 Answer 1

1

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  |
+----+---------------+---------------+----------+
  1. First create a database like

  2. 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);

  3. 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.

  4. 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;

  5. 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.

Sign up to request clarification or add additional context in comments.

8 Comments

i'm not sure if i'm following... so where is query? :$
@ToniMichelCaubet updated answer. but sure this will be difficult for you
thanks! so you mean this que $consulta will be accesing DDBB only once? i added an example of how and when i use it, i might didn't explain well :?
@ToniMichelCaubet well you did in the comment, also check on how to use it.
OK, i have only 1 text per $lan and per $keyword. i always use a while there because the if(mysql_num_rows>0) never worked very well on me... so you mean to load all texts using getData, and then, how do i acces each text?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.