Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Apr 2007
    Posts
    141
    Thanks
    3
    Thanked 1 Time in 1 Post

    two languages. the require does not update ?

    Code:
    	function process_choice()
    		{
    			$value=$_POST['value'];
    			$submit=$_POST['submit'];
    			
    			if(isset($submit))
    				{
    					if($value=="en")
    						{
    						require ("lib/en.php");
    						$session=="en";	
    						}else {
    						require ("lib/id.php");
    						$session=="id";
    						}
    				
    					show_choice();
    				} else {
    					show_choice();
    				}
    		}
    		
    	function show_choice()
    		{
    			echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post" name="value" id="value">';
    			echo '<label>Select your language: ';
    			echo '<select name="value">';
    			echo '<option value="en">English</option>';
    			echo '<option value="id">french</option>';
    			echo '</select>';
    			echo '<input type="submit" name="submit" value="Select" />';
    			echo '</label>';
    			echo '</form>';
    		}
    
    
    
    process_choice();

    hi guys the problem if .. either you select english or french .

    the require does not update.

    say all the code above this is hd.php

    and index.php require hd.php .which run all the code above

    yes . it shows the choice.
    but when you submit it . the require ("lib/en.php"); does not update the index.php .

    you know what i mean ?

    but if you place after the if($value=="en") echo "testetsaetaste";
    then the index.php will load echo "testet"

    is there anything wrong with the require function as the result of if($value=="en") ?

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    If you enable you're error reporting, it may complain about a non-existant file. This is where include/require gets annoying with PHP. Anything that has been included into another file takes the cwd of the currently executing script, so unless hd.php and index.php are in the same directory, hd.php will be unable to find the files relative to index.php. I'm guessing that hd.php is in a different directory from index.php.

    Given the code you have posted, there is absolutely no way to determine if the files have included unless the included files have output. The $session == "en"; and $session == "id"; are probably supposed to be assignments, not comparisons, but it doesn't really make a difference since $session will be crushed on the function stack. If you want to keep it, you need to either return it, globalize it, or pass it into the show_choice call.

    Other than that it looks ok. Might want to change you're form name and id so that it doesn't conflict with you're input fields though.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    Regular Coder
    Join Date
    Apr 2007
    Posts
    141
    Thanks
    3
    Thanked 1 Time in 1 Post
    i dont get what you said.
    I am a newbie hahahaa

    and .. can you show me any example how to make this done ?

    the file structure is like this.. hd.php are in the same directory as index.php

    project/index.php
    project/hd.php
    project/lib/en.php
    project/lib/fr.php

    for example .

    and inside index.php is . this
    Code:
    require ("hd.php"); 
    echo $translate['test'];
    file in en.php is

    $translate=array();
    $translate['test']="this is test";


    of course the hd. php is all the code in my previous post .

    if I just use this code in my index.php
    require("lib/en.php");
    echo $translate['test'];

    it works..

    so .. i dont know. i am logical error ?

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Ok, first we'll need to alter you're code so that it keeps track of what language has been selected. I'm not doing this with cookies, so it will only last as long as the browser is open. Setting a cookie (if the browser will take it) will let you remember what language was last used. Next, we need to alter the scope of the $translate variable. Include/require inherits the scope of where it was called. This means that the $translate array is unable to leave the process_choice function call.

    Edit:
    Yes, I changed the if to a switch syntax. This will make expansion easier if you add more languages instead of using an elseif, elseif, elseif...


    PHP Code:
    <?php
    session_start
    ();

    function 
    process_choice()
    {    
        
    $value 'en';
        
    $aResult null;
        if (isset(
    $_POST['value']))
        {
            
    $value $_POST['value'];
        }
        else if (isset(
    $_SESSION['lang']))
        {
            
    $value $_SESSION['lang'];
        }
        switch (
    $value)
        {
            case 
    'fr':
                require_once 
    './lib/fr.php';
                break;
            default:
                require_once 
    './lib/en.php';
                
    $value 'en';
        }
        
    $_SESSION['lang'] = $value;
        
    $aResult = (isset($translate)) ? $translate : array();
        
    show_choice();
        return 
    $aResult;
    }
            
    function 
    show_choice()
    {
        echo 
    '<form action="'.$_SERVER['PHP_SELF'].'" method="post" name="frmLang" id="frmLang">';
        echo 
    '<label for="selLang">Select your language: </label>';
        echo 
    '<select name="value" id="selLang">';
        echo 
    '<option value="en"' . ($_SESSION['lang'] == 'en') ? ' selected="selected" : '' . '>English</option>';
        echo '
    <option value="fr"' . ($_SESSION['lang'] == 'fr') ? ' selected="selected" '' '>french</option>';
        echo 
    '</select>';
        echo 
    '<input type="submit" name="submit" value="Select" />';
        echo 
    '</form>';
    }

    $translate process_choice();
    If you need to use the $translate array within the show_choice() function, you'll need to pass it as a function argument instead of globalizing it. It will not exist by this point.
    That work?

    Edit:
    I think I broke'd something in the selection option values. The syntax highlighting is all off. If you have problems, post back and when I wake up a little more I can fix it for you then, lol.
    Last edited by Fou-Lu; 08-03-2008 at 08:49 PM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    likon (08-04-2008)

  • #5
    Regular Coder
    Join Date
    Apr 2007
    Posts
    141
    Thanks
    3
    Thanked 1 Time in 1 Post
    thanks .

    i havent tried it yet. just got home ..


    but $translate .. i dont want to make it as process_choice though


    ex .. $translate['data']="this sentence will be translated";

    so when you echo $translate['data']... the "this sentence will be translated"
    should be translated according to the result of "process choice" which is either english / french . .

    and i am going to translate line by line..

    oh well lemme try out your code .and i will post again .
    thanks !


  •  

    Posting Permissions

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