Hi,

I have just started out crafting my own web page by modifying an existing design.
One part of my modification is to break up the existing static html code into php chunks where the header of my page is one part.
In the header part I have the following construction for language selection

<li><a href="language.php?lang=en" title="English"><img src="./img/flag_uk.gif" alt="Image description" /></a></li>

And the functionality is that the user should click on one of the maps to switch language. (it is actually one row for each supported language)
For this purpose did I make a simple function as

 function set_language($lang_sel)
 {
 	global $_lg;
	global $lg_en;
	global $lg_se;
	global $lg_ge;
	global $lg_es;

switch($lang_sel)
{
	case "en":
	$_lg = $lg_en;
	break;
	case "se":
	$_lg = $lg_se;
	break;
	case "de":
	$_lg = $lg_de;
	break;
	case "es":
	$_lg = $lg_es;
	break;
}

 }

My problem as a newbie to web design and coding in php is that I dont know where to put the function and how to call it with the selected language argument. This header is used on all pages and the language selection should not move the user to a new page.
I have found the following construction

header("Location: ".$session->referrer."");

Which I guess could be used in language.php (from the above html snippet) to get the user back to the same page as when clicking on one of the flags.

If I am not making myself clear please advise me on what more information you want from me.
The global language variables are declared in a separate php file included on every page and all content pages are referring to the $_lg variable.

Thanx for reading.

    Ok I have gotten further and now my problem is that I would like to return back to the referring page and reload so my language strings gets updated.

    The construction in language.php is now:

    include '../includes/language_selection.php';
    if(!empty($_GET['lang']))
    {
    	set_language($_GET['lang']);
    } 
    

    and the function set_language is defined in the earlier post.

    I am now trying to return to the referring page but my assumption made earlier does not work so i tried to understand how to do it and came up with the following

    header("Location: ". $_SESSION['referrer']);

    where I am setting the 'referrer' on each page to point back to itself.
    When I am making an

    print_r($_SESSION['referrer']);

    I can see for example:
    http://localhost/index.php
    or
    http://localhost/about.php
    depending on from where I click the maps.

    My problem is that the page is not updated.
    If however I make the language.php file to produce its own page with
    my header.php and footer.php scripts, that page gets updated according to language selection.

    A lot of text but no substance I am afraid.

    Thanx

      There are many ways to make your functions available to your program. You could write them straight in the body of your script, you could place them in a single file and include them using require() or you could even organize them in classes, if you were to use the OOP approach.

      On your second point, you could just post, or use a link to navigate and reload the same script. I don't see why you would use the header() function, unless I'm missing something?

        No I don't think you are missing anything, except the fact that I am a total beginner.
        The only reason for me using the header function is that I saw somebody else doing it, copy & paste....

        When I am executing the code in language.php I know to which script I need to return to and I also want it to be run again for the updated language variable to bite.
        If you could tell me how I should accomplish this I would be very grateful.

        Thanx for reading

          you could link to your homepage(same page where the link lays) instead of a language page. You simply add a condition that would check on your query string and run the appropriate script to display your page in the requested language.

          The problem is that your script is setting some global variables that I simply don't know what they do in the rest of your program. The problem when you try to revamp a program is that you need to know what the rest of your code does.

            Well that would work if I only had one home page.
            My current design is that I have split up the 'site' in a few different php files handling different aspects of the 'site' like
            home:index.php
            forums:forum.php
            albums:album.php
            about:about.php
            with a common header and footer script (but dependent on different CSS styles)

            All these 'pages' are dynamically created by the corresponding php scripts and I would like the language selection (which is in the header) to be transparent to the user.
            If the user is in one of the forums he/she should be able to switch language and just notice that the content I control has changed language. Text inputs from the users in albums, forums e.t.c is of course out of scope but the user interface should adapt.

            I have absolutely no experience in web design (but has coding experience from embedded control systems....) so I am at loss on how to partioning the code and am starting to learn how the workings of Server-Php(MySQL)-Client.

            As far as language handling goes my plan is to code all user interface texts in one php file where I replicate one tag for all languages and then I am setting the global language variable to the selected language container which then is used in the dynamically created pages.

            As I said earlier, the language selection is done via html code outputted from the header script and it currently has the

            <li><a href="language.php?lang=ge" title="Deutsch"><img src="./img/flag_germany.gif" alt="Image description" /></a></li>

            implementation with one flag for each language.
            Since the header script is common it is easier to have a common href in it but it could of course be scripted in the same way the breadcrumbs are scripted.

            However the header function is taking me back to the right location except that the output is not refreshed. The language variable, $lg, is global and changed in the set_language function dependant on $GET['lang'] argument but the

            header("Location: ". $_SESSION['referrer'])

            function does not make the browser force the server to reread the script it calls to get the new content of $lg before producing the output.
            I have tried with the following construction to no avail

             header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
             header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
             header("Location: ". $_SESSION['referrer']);
            

            I am at loss but I accept that since it takes some time to understand this.

            Regarding 'revamping' (I like that expression 🙂 ) the design I am starting with is pure static html and I am piggybacking on the HTML coding and corresponding CSS.

            The global variables should actually be only one, $_lg is an array for all text in the selected language. Currently $lg_se, $lg_en and so forth are also global variables but I will make them defines since that is what they actually are.

              .php files don't normally get cached, unless they are cached by something like the Zend optimizer, in which case it is usually smart enough to recognize changes. I know this doesn't help much, but it should serve to refocus your efforts in your script rather than trying to send out different headers.

              Look closely at your script and echo all variables that you need from within the script. Make sure they are being passed to the script that makes use of them. I saw you made use of sessions to pass your variables around, at least the $_SESSION['referrer']. My guess is that your code is broken somewhere.

                You were quite right.
                The script was broken... it did actually exactly what I told it to do instead of what I wanted it to do, i guess I should have seen that coming.
                I had forgotten to remove the default setting of language so it was always changing back to default after changing to selected expect when I tested outputting content in my language.php script.
                Well this was a good learning experience.
                Thanx for your help.

                  super, glad you got it fixed. See ya next time.

                    Write a Reply...