douyong7199 2013-12-11 15:37 采纳率: 0%
浏览 33

在PHP中循环进入一个开关?

I am trying to make a website in different languages and I retrieve this information from a database. For now I have two languages in there: English and Dutch.

What I would like to do is getting all the language possibilities and put this into a switch. So if people will click on for example this link: index.php?page=NL they will get the Dutch translation of the page.

So this is the code that I am using. It works so far but it shows I think the language option and the default option. Because when I remove default I only get the code ones.

    <div id="content">
    <?php 
    //Retrieve languages
    $querySwitchLang = "SELECT * FROM languages";
    $resultSwitchLang = mysql_query($querySwitchLang);
    ?>
    <?php
    while($rowSwitchLang = mysql_fetch_array($resultSwitchLang)){
        switch($page){
            case $rowSwitchLang['langCode']:include('page.php');break;
            default: include('pageDefault.php'); 
            break;
        }
    }
    ?>   
    </div>

So actually I wanted to have it like this because now it runs the switch statements as many times as there are languages I think:

    <div id="content">
    <?php 
    //Retrieve languages
    $querySwitchLang = "SELECT * FROM languages";
    $resultSwitchLang = mysql_query($querySwitchLang);
    ?>
    <?php
    include('functions.php');

        switch($page){
                        while($rowSwitchLang = mysql_fetch_array($resultSwitchLang)){
            case $rowSwitchLang['langCode']:include('page.php');break;
                        }
            default: include('pageDefault.php'); 
            break;

    }
    ?>   
    </div>

But this doesn't work because then I will get this error:

Parse error: syntax error, unexpected 'while' (T_WHILE), expecting case (T_CASE) or default (T_DEFAULT) or '}' in C:\wamp\www\glimpler\index.php on line 6

I can't realy figure out how to solve this problem.

  • 写回答

1条回答 默认 最新

  • dsgk0386 2013-12-11 15:47
    关注

    i think you're approach is a bit messy is to use file for the translation instead of db, like this:

    NL.php

       <?php
          return [
             'welcome' =>'welkom',
             'goodbye' =>'afscheid'
             ];
    

    then in your index

    $translationFile =$_GET['page'].'.php';
    if(file_exists(translationFile)){
        $language = include(translationFile);
    }
    else{
     die("Couldn't find translation file. Page Terminated!");
    }
    

    and then just echo your translation.

    echo $language['welcome'] . ' USER';
    

    the advantage is not havinge multiple page for every language and that make it easier to update. reguarding the default you can just set $_GET['page'] to 'EN' if is not set.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题