Home > Backend Development > PHP Tutorial > php 多语言 sql 要怎么写

php 多语言 sql 要怎么写

WBOY
Release: 2016-06-20 12:40:32
Original
1045 people have browsed it

每一个函数我都要判断$_SESSION['language']的值,可以怎么优化?

public function getA(){        if($_SESSION['language']=="tc"){            $sql="select a_tc from table";        }elseif($_SESSION['language']=="en"){            $sql="select a_en from table";        }    }    public function getB(){        if($_SESSION['language']=="tc"){            $sql="select b_tc from table";        }elseif($_SESSION['language']=="en"){            $sql="select b_en from table";        }    }
Copy after login


回复讨论(解决方案)

即然language是字段的一部分,直接拼接成SQL就行了

你可以优化表,设置一个语言字段,通过$_SESSION['language']取数据:
select content from table where language = $_SESSION['language']
这样不管后面来多少种语言都迎刃而解了,可扩展性好

$sql = "select a_$_SESSION[language] from table";
Copy after login

但需要指定默认值,第一次进入时是没有 $_SESSION['language'] 的
不过你的代码也没有进行处理

#2 的方案是不妥当的
1、冗余量太大
2、不易产生对照

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template