我是PHP的新手,也是一般的Web开发(第一个大项目)。我主要解决了这个问题,但我认为他们这样做的方式并不高效(不聪明),但仍然没有得到妥善解决,这就是为什么我想知道你的意见。
我有一个带语言的数组(student_language)。在每种语言中,我们都有一个名字的字符串,以及4个整数(这种语言中技能的标点符号)。 我正在将这些信息转换为PDF,我创建了一个HTML表格来展示那里的语言。 所以,我必须用我的数组填充它。在这里,我有我的问题:
代码:
foreach ($student_language as $sl){
//Dutch
if ($sl['StudentLanguage']['language_id'] == '5'){
$ned_listening=$sl['StudentLanguage']['listening'];
$ned_spoken=$sl['StudentLanguage']['speaking'];
$ned_reading=$sl['StudentLanguage']['reading'];
$ned_written=$sl['StudentLanguage']['written'];
}
//French
if ($sl['StudentLanguage']['language_id'] == '2'){
$fre_listening=$sl['StudentLanguage']['listening'];
$fre_spoken=$sl['StudentLanguage']['speaking'];
$fre_reading=$sl['StudentLanguage']['reading'];
$fre_written=$sl['StudentLanguage']['written'];
}
//English
if ($sl['StudentLanguage']['language_id'] == '1'){
$eng_listening=$sl['StudentLanguage']['listening'];
$eng_spoken=$sl['StudentLanguage']['speaking'];
$eng_reading=$sl['StudentLanguage']['reading'];
$eng_written=$sl['StudentLanguage']['written'];
}
//Flemish
if ($sl['StudentLanguage']['language_id'] == '5'){
$fle_listening=$sl['StudentLanguage']['listening'];
$fle_spoken=$sl['StudentLanguage']['speaking'];
$fle_reading=$sl['StudentLanguage']['reading'];
$fle_written=$sl['StudentLanguage']['written'];
}
//Others : German, Polish, Greek, Portuguese, Russian, Italian , Spanish
if ($sl['StudentLanguage']['language_id'] == '6' || '7' || '8' || '9' || '10' || '11' || '12'){
$oth_listening=$sl['StudentLanguage']['listening'];
$oth_spoken=$sl['StudentLanguage']['speaking'];
$oth_reading=$sl['StudentLanguage']['reading'];
$oth_written=$sl['StudentLanguage']['written'];
}
}
$talenkennis= <<<EOD
<br />
<br />
<br />
<table border="1" align="center">
<tr>
<th></th>
<th>Begrijpen</th>
<th>Spreken</th>
<th>Lezen</th>
<th>Schrijven</th>
</tr>
<tr>
<td>Nederlands</td>
<td>$ned_listening</td>
<td>$ned_spoken</td>
<td>$ned_reading</td>
<td>$ned_written</td>
</tr>
<tr>
<td>Frans</td>
<td>$fre_listening</td>
<td>$fre_spoken</td>
<td>$fre_reading</td>
<td>$fre_written</td>
</tr>
<tr>
<td>Engels</td>
<td>$eng_listening</td>
<td>$eng_spoken</td>
<td>$eng_reading</td>
<td>$eng_written</td>
</tr>
<tr>
<td>Duits</td>
<td>$fle_listening</td>
<td>$fle_spoken</td>
<td>$fle_reading</td>
<td>$fle_written</td>
</tr>
<tr>
<td>Andere</td>
<td>$oth_listening</td>
<td>$oth_spoken</td>
<td>$oth_reading</td>
<td>$oth_written</td>
</tr>
<tr>
( Noties = 1 , Voldoende = 2 , Grondig = 3 , Moedertaal = 4 ) <br />
</tr>
</table>
EOD;
$pdf->writeHTML($talenkennis, true, false, false, false, '');
提前致谢!
阿尔夫。
答案 0 :(得分:2)
使用数组会帮助你自动化整个过程,这是一种解决问题的方法,也许(几乎可以肯定)它不是最好的,但我可能是个主意。
//array with all languages (not andere's ones)
$langs = array(1=>"eng",2=>"fre",...);
//result array
$result = array();
//rsult array for others languages
$result['other'] = array();
foreach ($student_language as $sl)
{
//let's use a variable fot that index
$index = $langs[$sl['StudentLanguage']['language_id']];
//copy all info on relative language array element
$result[$index]['listening'] = $sl['StudentLanguage']['listening'];
$result[$index]['spoken'] = $sl['StudentLanguage']['speaking'];
$result[$index]['reading'] = $sl['StudentLanguage']['reading'];
$result[$index]['written'] = $sl['StudentLanguage']['written'];
//if other language
if(in_array($index,array(6,7,8,9,10,11,12)))
{
//create language array
$other = array();
//store language data
$other['listening'] = $sl['StudentLanguage']['listening'];
$other['spoken'] = $sl['StudentLanguage']['spoken'];
$other['reading'] = $sl['StudentLanguage']['reading'];
$other['written'] = $sl['StudentLanguage']['written'];
//add language to other's array
array_push($result['other'],$other);
}
}
那样你就会有这样的结构:
-english
--listening
--spoken
--reading
--written
-french
--listening
--spoken
--reading
--written
...
-others
--0
---listening
---spoken
---reading
---written
....
但是我建议你像对待主要语言一样对待他人语言我觉得拥有'规范化'数据结构是好的
答案 1 :(得分:1)
首先,您应该查看“switch()”语句。
其次,最后一个“if”将始终等于true,因为||启动一个全新的表达式,非空字符串总是等于true,所以|| '7'应该是|| $ sl ['StudentLanguage'] ['language_id'] =='7'
第三,如果您以不同方式构建数组,则根本不需要ifs。如果您使用学生ID作为数组键,则只需打印所需的数据即可。例如:echo $ student_language [1] ['listening'];
第四,这种类型的数据可能需要自己的类而不是多维数组。
第五,不是将你想要打印的所有文本都放到一个变量中,你可以简单地用“?&gt;”打开PHP,然后输入你想要的html。
?>
<br />
<br />
<br />
<table border="1" align="center">
<tr>
<th></th>
<th>Begrijpen</th>
<th>Spreken</th>
<th>Lezen</th>
<th>Schrijven</th>
</tr>
<tr>
<td>Nederlands</td>
<td><?php= $ned_listening ?></td>