SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Change language in a loop!?!?

    I have a site where the language is set depending a users native language. The language snippets are stored in a db table with a language id attached to them.

    The get language file looks like this:
    PHP Code:
    if ($language == 'da_DK') {
            
        
    ///// DANISH /////
        
        
    $result45 mysql_query('SELECT * FROM '.$prefix.'_lang_code WHERE lang_id=1');
        while (
    $row45 mysql_fetch_assoc($result45)) {  

        
    $string $row45['string'];             

        
    define("_".$row45['tagname']."","".$string."");

        }
        
    } else {
        
        
    ///// ENGLISH /////

        
    $result46 mysql_query('SELECT * FROM '.$prefix.'_lang_code WHERE lang_id=2'); 
        while (
    $row46 mysql_fetch_assoc($result46)) {  

        
    $string $row46['string'];             

        
    define("_".$row46['tagname']."","".$string."");

        }


    Now, what I want is to send useres who has not yet activated there account an reminder email, but I need it to be in there native language depending on what locale they have registret...

    That I thought I would do like this inside a loop:
    PHP Code:
    include ("includes/lang.php");
                
    echo 
    $row99['fname']. ' '.$row99['lname'].' ('.$newsDate.')<br>';
    echo 
    ''._LANG_TEST.'<br><br>'
    The problem here is that it takes the first user in the loops "locale" and uses it on every user...

    How do I get passed this?

    Thanks in advance for any help...

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,398
    Mentioned
    147 Post(s)
    Tagged
    4 Thread(s)
    I see no loop there?

  3. #3
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    I see no loop there?
    Sorry... Here you go:
    PHP Code:
    $datelimit date('Y-m-d'strtotime('-1 week'));

    $sql99="SELECT * FROM ".$prefix."_users WHERE ga_active=0 and regdate < '$datelimit' AND new_userid > 0";
    $result99 mysql_query($sql99);
    while(
    $row99 mysql_fetch_array($result99)){
        
        
    $user $row99['new_userid'];
        
    $fname $row99['fname'];
        
    $lname $row99['lname'];
        
    $email $row99['email'];
        
    $regdate $row99['regdate'];
        
    $buser $row99['by_user'];
        
    $language $row99['locale'];
        
    $validatecode $row99['validatecode'];
        
        include (
    "includes/mailunactivated_lang.php");
                
        echo 
    $row99['fname']. ' '.$row99['lname'].'<br>';
        echo 
    ''._LANG_TEST.'<br><br>';
        


  4. #4
    SitePoint Member
    Join Date
    Aug 2009
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you set a default value to locale in the table? If you don't, you could use a tertiary operator as follows:
    $language = (empty($row99['locale']))?'':$row99['locale'];

    This way, if the value is null or blank, it'll set the value to ''. Otherwise, it'll set the variable to the value stored in the database .

    Shaun Oleson
    blog.olebox.com

  5. #5
    SitePoint Zealot CalebMingle's Avatar
    Join Date
    Jul 2007
    Posts
    135
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well how are you determining if a user has activated his/her e-mail or not? Then use that (username, email, id, or whatever) to query the table.. instead of the timestamps and conditional values..
    :: Caleb Mingle (@dentafrice)
    :: PHP Developer.
    :: g+: http://gplus.to/mingle

  6. #6
    SitePoint Member
    Join Date
    Aug 2009
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you set a default value to locale in the table? If you don't, you could use a tertiary operator as follows:
    $language = (empty($row99['locale']))?'':$row99['locale'];

    This way, if the value is null or blank, it'll set the value to ''. Otherwise, it'll set the variable to the value stored in the database .

    Shaun Oleson
    http://blog.olebox.com

  7. #7
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This still doesnt help... The problem here is that the first user in the loop locale sets the locale for all users... Every user in the table has a locale, but the loop only reconize the first... ?

  8. #8
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,398
    Mentioned
    147 Post(s)
    Tagged
    4 Thread(s)
    I don't see you use the $language, nor send an email. Perhaps it happens all in here:
    includes/mailunactivated_lang.php
    ?

  9. #9
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    I don't see you use the $language, nor send an email. Perhaps it happens all in here:
    includes/mailunactivated_lang.php
    ?
    The "$language" is used for setting which "locale" a user uses... Forinstance, if a user has the locale "da_DK", the script should uses the tag "_LANG_TEST" with the lang_id as defined in the "includes/lang.php".

    lang.php script:
    PHP Code:
    if ($language == 'da_DK') {
            
        
    ///// DANISH /////
        
        
    $result45 mysql_query('SELECT * FROM '.$prefix.'_lang_code WHERE lang_id=1');
        while (
    $row45 mysql_fetch_assoc($result45)) {  

        
    $string $row45['string'];             

        
    define("_".$row45['tagname']."","".$string."");

        }
        
    } else {
        
        
    ///// ENGLISH /////

        
    $result46 mysql_query('SELECT * FROM '.$prefix.'_lang_code WHERE lang_id=2'); 
        while (
    $row46 mysql_fetch_assoc($result46)) {  

        
    $string $row46['string'];             

        
    define("_".$row46['tagname']."","".$string."");

        }


    And my loop script simplyfied:
    PHP Code:
    $sql="SELECT * FROM ".$prefix."_users";
    $result mysql_query($sql);
    while(
    $row mysql_fetch_array($result)){
        
        
    $username $row['username'];
        
    $language $row['locale'];
        
        include (
    "includes/lang.php");
                
        echo 
    $username'<br>';
        echo 
    ''._LANG_TEST.'<br><br>';
        

    So in my user table a user could look like this:
    username=test
    locale=da_DK


    Now if a user has "en_GB" or anything else than "da_DK" it should use the "_LANG_TEST" tag with lang_id=2.

    The problem is though, if I have 3 users in my table/loop like this:
    username=test1
    locale=no_NO

    username=test2
    locale=en_GB

    username=test3
    locale=da_DK


    The output looks like this:

    test1
    This is in english

    test2
    This is in english

    test3
    This is in english


    Now the username "test3" should have the "_LANG_TEST" written in danish, but somehow it also comes out in english?!?!?

    If the first users locale was "da_DK" all the users "_LANG_TEST" would come out in dansih, when infact the other users should be in english...

    Hope this claryfies a bit, and is able to make you help me better...

  10. #10
    SitePoint Member
    Join Date
    Aug 2009
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It looks to me like something else is going on here. The $language variable does not exist outsite of the initial while loop. It's local to that loop. This means that when it's re-setting the variable, it's setting it to something other than da_DK.

    print out the value of $language right before the include and see if it is da_DK. If it is, then it's the value returned in the $result45 query. var_dump $row45 and confirm that you have the right record selected.

  11. #11
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It seems like everything is right... When var_dump is returned it is in danish... Where the hell is the error?!?!?!? ARGHHH!

    Please help :-)

  12. #12
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,398
    Mentioned
    147 Post(s)
    Tagged
    4 Thread(s)
    http://www.php.net/manual/en/language.constants.php

    I quote:
    "As the name suggests, that value cannot change during the execution of the script"

    So once you defined the constant, it'll keep that value for the entire duration of the execution of the script. If you need to change the values, then you will have to use another way (variables for instance).

  13. #13
    SitePoint Member
    Join Date
    Aug 2009
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    is _LANG_TEST the constant you are testing against? I don't see where it's being set at.

  14. #14
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,398
    Mentioned
    147 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by shaunole View Post
    is _LANG_TEST the constant you are testing against? I don't see where it's being set at.
    They're being defined dinamically:

    define("_".$row45['tagname']."","".$string."");


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •