iTux
iTux

Reputation: 53

Use a query according to php value

I'm new on PHP. Hope someone could help me :)

I have a page to edit a list of recipes on 3 different languages, on my database I have mark them as ES for Spanish, EN for English and FR for French.

Is there a way to use an specific query according to the users selection? I thought on passing a value to the URL

<p><a class="btn btn-success" href="recetas.php?ES" role="button">Editar &raquo;</a>
   <a class="btn btn-success" href="recetas.php?EN" role="button">Edit &raquo;</a>
   <a class="btn btn-success" href="recetas.php?FR" role="button">Éditer &raquo;</a></p>

Thanks in advance :) Regards

Upvotes: 1

Views: 51

Answers (3)

trincot
trincot

Reputation: 350272

Use a named URL argument, like lang, as follows:

href=recetas.php?lang=ES

Then you can processes it on the PHP side with $_GET['lang']

I would suggest to use sessions, so you keep that information at least during the user's session.

Example PHP code:

<?php
    // use sessions to keep track of user's language choice
    session_start();
    if (isset($_GET['lang'])) {
        // set user's language if it was passed via URL
        $_SESSION['lang'] = $_GET['lang'];
    };
    if (!isset($_SESSION['lang'])) {
        // ask for language if choice has not yet been made
?>
    <p>
        <a class="btn btn-success"
           href="recetas.php?lang=ES" role="button">Editar &raquo;</a>
        <a class="btn btn-success"
           href="recetas.php?lang=EN" role="button">Edit &raquo;</a>
        <a class="btn btn-success"
           href="recetas.php?lang=FR" role="button">Éditer &raquo;</a>
    </p>

<?php
        exit();
    }    
    echo "Your language is " . $_SESSION['lang'];
    // any other content goes here
?>

Upvotes: 3

Pavlin
Pavlin

Reputation: 5528

You're passing the query string incorrectly. The correct format to pass query string is like this:

URL?key1=value1&key2=value2

You would want to pass your lang to your php script doing something like ?lang=en. Or better yet, if you know how to do url rewriting, you should make your URLs somethinng like this: URL/en/.

Upvotes: 1

Gouda Elalfy
Gouda Elalfy

Reputation: 7023

you can pass your language in URL like this:

<p><a class="btn btn-success" href="recetas.php?lang=ES" role="button">Editar &raquo;</a>

then read this lang through your PHP code like this:

$lang = $_REQUEST['lang']; //lang variable = ES

Upvotes: 2

Related Questions