I've been dealing with a problem for a long time, I'll try to describe it. I have languages to choose from in a Droppbox (so far only German and English). By default, English is set, if I now switch to German everything goes perfectly, but as soon as I try to switch back to English, it is as if I had selected German again.
In langugae.php I select the language and pass it with the post method to language.inc.php where with the help of the selected language an array is loaded from a database in which the individual phrases are.
The function Defaulttext()
set the $_SESSION["text"];
variable to the English version and it is called at the top of the website.
language.php
<?php
require_once 'conf/db-config.inc.php';
$result = $db_xttv_sqli->query("SELECT name FROM rm_languages");
$text = array();
$text = $_SESSION["text"];
?>
<div>
<form action="includes/language.inc.php" method="post" id="languageid">
<label for="language" id="language_input">
<select id="language" class="dropdown-content" name="language" onchange="this.form.submit();">
<?php
while($rows = $result->fetch_assoc()) {
$language = $rows['name'];
if($_SESSION["language"] == $language) {
echo "<option selected value='$language '>$language</option>";
}
else {
echo "<option value='$language '>$language</option>";
}
}
?>
</select>
</label>
<input type="hidden" id="SelectedLanguage" name="language" value="<?php echo htmlspecialchars($language);?>">
<input type="hidden" name="path" value="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>">
</form>
</div>
language.inc.php
<?php
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
if (isset($_POST["language"])){
require_once '../conf/db-config.inc.php';
$language = $_POST['language'];
$path = $_POST['path'];
ChangeLanguage($language, $path, $db_xttv_sqli);
}
function Defaulttext($db_xttv_sqli) {
if(isset($_SESSION["text"])) {
}
else {
//Default English = 1
$text = array();
$result = $db_xttv_sqli->query("SELECT rm_texts_idrm_texts, text FROM rm_languages_has_rm_texts WHERE rm_languages_idrm_languages = 1");
while($rows = $result->fetch_assoc()) {
$text[$rows['rm_texts_idrm_texts']] = $rows['text'];
}
$_SESSION["text"] = $text;
$_SESSION["language"] = "English";
}
}
function ChangeLanguage($language,$path,$db_xttv_sqli) {
$text = array();
$query = "SELECT idrm_languages FROM rm_languages WHERE name = '$language'";
$result = mysqli_query($db_xttv_sqli, $query);
$LanguageID = mysqli_fetch_array($result);
$intLiD = (int)$LanguageID[0];
$result = $db_xttv_sqli->query("SELECT rm_texts_idrm_texts, text FROM rm_languages_has_rm_texts WHERE rm_languages_idrm_languages = $intLiD");
while($rows = $result->fetch_assoc()) {
$text[$rows['rm_texts_idrm_texts']] = $rows['text'];
}
$_SESSION["text"] = $text;
$_SESSION["language"] = $language;
header('location:'.$path);
}