PHP:MySQL的多语言系统

时间:2014-01-11 19:04:10

标签: php mysql

我有一个问题:我正在使用MySQL为PHP网站制作多语言系统。 这是通过使用会话和cookie来设置变量来完成的,它将告诉mysql_query从数据库中选择哪些数据。 我给你一个片段: lang.php:

<?php
ob_start();
session_start();
header('Cache-control: private');

if(isset($_GET['lang'])){
$lang = $_GET['lang'];
$_SESSION['lang'] = $lang;
setcookie("thomasvanvugt_lang", $lang, time() + (3600 * 24 * 30));
} else if(isset($_SESSION['lang'])){
$lang = $_SESSION['lang'];
} else if(isset($_COOKIE['thomasvanvugt_lang'])){
$lang = $_COOKIE['thomasvanvugt_lang'];
} else {
$lang='nl';
}

switch ($lang) {
    case "nl":
    $web_lang="nl";
    break;

    case "en":
    $web_lang="en";
    break;

    case "de":
    $web_lang="de";
    break;

    case "fr":
    $web_lang="fr";
    break;

    default:
    $web_lang="nl";
}
?>

footer.php

<?php
$text_footer = mysql_query("SELECT * FROM text_footer WHERE lang='$web_lang' AND line='copyright'");
while($text_footer_row = mysql_fetch_array($text_footer)){
    $text_footer_copyright = $text_footer_row['text'];
}
$text_footer = mysql_query("SELECT * FROM text_footer WHERE lang='$web_lang' AND line='about'");
while($text_footer_row = mysql_fetch_array($text_footer)){
    $text_footer_about = $text_footer_row['text'];
}
$text_footer = mysql_query("SELECT * FROM text_footer WHERE lang='$web_lang' AND line='adres'");
while($text_footer_row = mysql_fetch_array($text_footer)){
    $text_footer_adres = $text_footer_row['text'];
}
?>

<footer id="footer">
    <!-- 960 Container -->
    <div class="container">
        <!-- Over mij -->
        <div class="four columns">
            <img id="logo-footer" src="images/logo-footer.png" alt="" />
            <p><?php echo $text_footer_about; ?></p>
        </div>

        <!-- Foto's -->
        <div class="four columns">
            <h4>Photo Stream</h4>
            <div class="flickr-widget">
                <script type="text/javascript" src="http://www.flickr.com/badge_code_v2.gne?count=6&amp;display=latest&amp;size=s&amp;layout=x&amp;source=user&amp;user=48035108@N07"></script>
                <div class="clearfix"></div>
            </div>
        </div>

        <!-- Twitter -->
        <div class="four columns">
            <h4>Twitter</h4>
            <ul id="twitter">
<?php
$tweet_timeline = $twitter_connection->get('https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name='.$twitter_username.'&count=2');
foreach($tweet_timeline as $tweet_timeline){
            echo '<li>'.$tweet_timeline->text.'</li>';
}
?>
            </ul>
            <div class="clearfix"></div>
        </div>

        <!-- Contact -->
        <div class="four columns">
            <h4>Contact</h4>
            <ul class="contact-details-alt">
                <li><i class="halflings white map-marker"></i> <p><strong><?php echo $text_footer_adres; ?></strong> 123 Seward Street, Oklahoma City, USA</p></li>
                <li><i class="halflings white user"></i> <p><strong>Phone:</strong> +48 880 440 110</p></li>
                <li><i class="halflings white envelope"></i> <p><strong>Email:</strong> <a href="#">mail@example.com</a></p></li>
            </ul>
        </div>

    </div>
    <!-- 960 Container / End -->

</footer>
<!-- Footer / End -->


<!-- Footer Bottom / Start  -->
<footer id="footer-bottom">

    <!-- 960 Container -->
    <div class="container">

        <!-- Copyrights -->
        <div class="eight columns">
            <div class="copyright">
            <?php echo $text_footer_copyright; ?>
            </div>
        </div>

        <!-- Menu -->
        <div class="eight columns">
            <nav id="sub-menu">
                <ul>
                    <li><a href="#">Veelgestelde vragen</a></li>
                    <li><a href="#">Sitemap</a></li>
                    <li><a href="#">Contact</a></li>
                </ul>
            </nav>
        </div>

    </div>
    <!-- 960 Container / End -->

</footer>

是的,我是荷兰人。所以,如果我是,请不要介意我:有点愚蠢,不擅长英语等。 我的问题是,我可以缩短这段代码:

<?php
$text_footer = mysql_query("SELECT * FROM text_footer WHERE lang='$web_lang' AND line='copyright'");
while($text_footer_row = mysql_fetch_array($text_footer)){
    $text_footer_copyright = $text_footer_row['text'];
}
$text_footer = mysql_query("SELECT * FROM text_footer WHERE lang='$web_lang' AND line='about'");
while($text_footer_row = mysql_fetch_array($text_footer)){
    $text_footer_about = $text_footer_row['text'];
}
$text_footer = mysql_query("SELECT * FROM text_footer WHERE lang='$web_lang' AND line='adres'");
while($text_footer_row = mysql_fetch_array($text_footer)){
    $text_footer_adres = $text_footer_row['text'];
}
?>

问候,托马斯。

3 个答案:

答案 0 :(得分:2)

$query = mysql_query("SELECT * FROM text_footer WHERE lang='$web_lang' AND line IN ('copyright', 'about', 'adres')");
while ($row = mysql_fetch_array($footerTexts)) {
    $line = $row['line'];
    $footerTexts[$line] = $row['text'];
}
echo $footerTexts['copyright'];

答案 1 :(得分:0)

$text = mysql_query("SELECT * FROM text_footer WHERE lang='$web_lang' AND line IN  ('copyright' , 'about' , 'adres') ORDER BY line");

while($text_row = mysql_fetch_array($text)){
   switch($text_row['line']){
        case 'copyright' : 
            $text_footer_copyright = $text_row['text'];
            break;
        case 'about' : 
            $text_footer_about = $text_row['text'];
            break;
        case 'adres' :
            $text_footer_adres = $text_row['text'];
            break;
    }
}

写入时间可能更长,但执行速度更快,因为您只执行一次SQL查询。

答案 2 :(得分:0)

您想阅读本手册。从functions开始。