我有一系列由前任开发人员编写的函数,现在我正在尝试优化它们。
function translateAdmSection() {
$lang = $_REQUEST['lang'];
if($lang == '') {
$content = 'content';
}
if($lang == 'en') {
$content = 'content';
}
if($lang == 'es') {
$content = 'es_content';
}
if($lang == 'fr') {
$content = 'fr_content';
}
if($lang == 'ko') {
$content = 'kr_content';
}
if($lang == 'mdn') {
$content = 'mdn_content';
}
if ($lang == 'pt') {
$content = 'pt_content';
}
$title_query = ("SELECT adm_name, ".$content." as content FROM adm_sect WHERE adm_name = 'title';");
$title_result = mysql_query($title_query);
$title_row = mysql_fetch_assoc($title_result);
$sub_head_query = ("SELECT adm_name, ".$content." as content FROM adm_sect WHERE adm_name = 'Sub-headline';");
$sub_head_result = mysql_query($sub_head_query);
$sub_head_row = mysql_fetch_assoc($sub_head_result);
$content_query = ("SELECT adm_name, ".$content." as content FROM adm_sect WHERE adm_name = 'content';");
$content_result = mysql_query($content_query);
$content_row = mysql_fetch_assoc($content_result);
$section_name = $title_row['content'];
$section_sub_head = $sub_head_row['content'];
$section_content = $content_row['content'];
echo '<div class="section-title">
<h2>'.$section_name.'</h2>
<h3>'.$section_sub_head.'</h3>
</div>
<div class="row">
<div class="column column-1-2">'.$section_content.'</div><!-- .column-1-2 -->
<div class="column column-1-2">
<div class="contact-info">
<h4>Main Campus Address</h4>
<p>
Oral Roberts University<br />
Office of Admissions<br />
<span class="locality">
7777 South Lewis Avenue<br />
Tulsa, Oklahoma 74171<br />
United States of America
</span>
</p>
<h4>Call or Email Undergraduate Admissions</h4>
<p>
<span class="tel">+1 800.678.8876<br /></span>
<span class="tel">+1 918.495.6518<br /></span>
<span class="email">E: <a href="mailto:admissions@oru.edu">admissions@oru.edu</a></span>
</p>
<h4>Call or Email ORU's Coordinator of Chinese Intiatives</h4>
<p>
<span class="tel">+1 918.495.6540<br /></span>
<span class="email">E: <a href="mailto:yfang@oru.edu">yfang@oru.edu</a></span>
</p>
</div><!-- .contact-info -->
</div><!-- .column-1-2 -->
</div><!-- .row -->
<div class="row">
<div class="column column-1-2 centered">
<h3><a class="button arrow-button" href="#">Request More Information</a></h3>
</div><!-- .column-1-2 -->
<div class="column column-1-2 centered">
<h3><a class="button arrow-button" href="#">APPLY TO ORU TODAY</a></h3>
</div><!-- .column-1-2 -->
</div><!-- .row -->';
}
引用的表格设置如下:
| ID | adm_name | content | es_content |
-------------------------------------------------
| 1 | title | content | translated |
| 2 | content | content | translated |
| 3 | address_info | content | translated |
| 4 | RFI Button Text | content | translated |
| 5 | RFI Link | content | translated |
| 6 | Apply Button Text | content | translated |
| 7 | Apply Link | content | translated |
| 8 | Sub-headline | content | translated |
$lang
到最高IF语句确定页面语言,并根据语言选择数据库内容列。因此,如果$lang == 'es'
,则$content
表格标题为es_content
我的问题如下:
1)如果可能的话,如何在函数开头分离$lang
参数调用,以便我可以为“adm_sect”,“finaid_sect”和“onl_learning_sect”复制此函数
2)我可以简化为1个查询,因为唯一的区别是“adm_name”变量吗?
3)如何生成单个列值,以便将它们全部放在一个导出值中?或者这是可能的,因为它们基于单独的SQL查询?
最终目标是让一个查询从DB生成每个已翻译部分的值。这是否可能,如果有的话,建议如何实现这个目标?
答案 0 :(得分:1)
1)您可以将该代码放在一个单独的文件中,并使用require
加载。
2)您可以将此作为单个查询执行。将所有值放在关联数组中。
$values = array();
$query = "SELECT adm_name, $content AS content FROM adm_sect
WHERE adm_name IN ('title', 'Sub-headline', 'content')";
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result)) {
$values[$row['adm_name']] = $row['content'];
}
现在使用$title_row
代替$values['title']
。
3)我不明白这个问题。将列放在一个导出值中是什么意思?除非上面代码中的数组是你所说的。