I have used a common.php for the language selection of the entire website and the Translations of the individual words/phrases of the web page reads per language variables from the language files, for example lang/lang.de.php , lang.en.php , etc. and outputs. This also works wonderfully and is not a Problem. Now that I have schemed a Blog in the website, whose posts are in a database located, I would like to this now, depending on the choice of the visitor language, from the entire table, speak, read and spend.
In short:
The visitor selects the German language, and everything is read from the following table, and output...
<?php
$sql = "SELECT Blogtitel, Blogdate, Blogpost FROM blog_de ORDER BY ID ASC LIMIT 3
?>
... or if the visitor selects, for example, the English language, then according to the table below to read ...
<?php
$sql = "SELECT Blogtitel, Blogdate, Blogpost FROM blog_en ORDER BY ID ASC LIMIT 3
?>
...and so on
common.php
<?php
session_start();
header('Cache-control: private');
if(isSet($_GET['lang'])) {
$lang = $_GET['lang'];
$_SESSION['lang'] = $lang;
setcookie('lang', $lang, time() + (3600 * 24 * 30));
} else if(isSet($_SESSION['lang'])) {
$lang = $_SESSION['lang'];
} else if(isSet($_COOKIE['lang'])) {
$lang = $_COOKIE['lang'];
} else { $lang = 'de'; }
switch ($lang) {
case 'en': $lang_file = 'lang.en.php'; break;
case 'de': $lang_file = 'lang.de.php'; break;
case 'ru': $lang_file = 'lang.ru.php'; break;
case 'fr': $lang_file = 'lang.fr.php'; break;
default: $lang_file = 'lang.de.php'; }
include_once 'lang/'.$lang_file;
?>
blog.php
<section id="blog" class="row">
<div class="center">
<h1 style="color:#fff; text-shadow: 1px 1px 2px #000000;"><i class="fa fa-newspaper-o" aria-hidden="true"></i> Blog</h1>
<div id="blogcontent">
<?php
$pdo = new PDO('mysql:host=localhost;dbname=DATENBANK', 'BENUTZER', 'PASSWORT');
if ($_GET["lang"]=="de") { $sql = "SELECT Blogtitel, Blogdate, Blogpost FROM blog_de ORDER BY ID ASC LIMIT 3"; }
else if ($_GET["lang"]=="en") { $abfrage = "SELECT Blogtitel, Blogdate, Blogpost FROM blog_en ORDER BY ID ASC LIMIT 3"; }
else if ($_GET["lang"]=="fr") { $abfrage = "SELECT Blogtitel, Blogdate, Blogpost FROM blog_fr ORDER BY ID ASC LIMIT 3"; }
else if ($_GET["lang"]=="ru") { $abfrage = "SELECT Blogtitel, Blogdate, Blogpost FROM blog_ru ORDER BY ID ASC LIMIT 3"; }
else { $abfrage = "SELECT Blogtitel, Blogdate, Blogpost FROM blog_ru LIMIT 3 ORDER BY Name ID LIMIT 3"; }
foreach ($pdo->query($sql) as $row) {
echo "<div class='post'><div class='blog_titel'><i class='fa fa-comment-o' aria-hidden='true'></i> " .$row['Name']. "</div>";
echo "<div class='blog_date'><i class='fa fa-calendar' aria-hidden='true'></i> " .$row['Phone']. "</div>";
echo "<div class='blog_post'> " .$row['Email']. "</div>";
echo "</div>";
}
?>
</div>
</div>
</div>
</section>
The voice output works wonderfully, and it will also be read out on all sides of the existing languages on the variable from the lang/lang.de.php , lang/lang.en.php , etc. properly and issued. When I now but for example with:
index.php?lang=en
The English language in the Navigation calls, then don't read the blog posts as desired from the corresponding table. In my opinion it should work but with the above Code or is there a typo in there or am I mistaken there? Thus, my question to the more experienced among you, how I can do that, depending on the desired choice of the appropriate table from the existing database and displayed in the language read.
In case $_GET['lang']=='de' you put the query in $sql, which is correct, because then you do $pdo->query($sql). In the others cases you put it in $abfrage, so the error is there.
Anyhow, a simpler approach, mantaining the same database structure, would have been:
foreach($pdo->query("SELECT ... FROM blog_".$_GET['lang']." WHERE ...") as row) ...
Anyhow, ppeterka solution is probably the best.