以有效的方式填充矩阵

发布于 2024-10-21 19:18:16 字数 3683 浏览 2 评论 0原文

我是 PHP 和一般 Web 开发的新手(第一个大项目)。我主要解决了这个问题,但我认为他们的方式我这样做的效率不高(不聪明),而且仍然没有得到妥善解决,这就是为什么我想知道你的意见。

我有一个包含语言(student_language)的数组。在每种语言中,我们都有一个字符串(即名称)和 4 个整数(该语言中技能的标点符号)。 我正在将此信息转换为 PDF,并创建了一个 HTML 表来显示其中的语言。 所以,我必须用我的数组填充它。在这里我有我的问题:

  • 首先,我认为我使用了很多变量:您是否看到一种减少变量的方法?
  • 其次,正如您在下面的代码中看到的,“Andere(其他)”类别中有不止一种语言。因此,如果此类别中有多种语言,我想添加另一行。有什么提示吗?

代码:

    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, '');

提前致谢!

阿尔夫.

I'm new to PHP , and web development in general (first big project). I mostly solved this issue, but I think they way I did it it's not efficient(not smart) and still it's not properly solved, that's why I would like to know your opinion.

I have an array with Languages(student_language). In every language we have a string which is the name, and 4 integers (punctuations of the skills in this language).
I'm converting this information to PDF and I created a HTML table to present the languages there.
So , I have to populate it with my array. Here I have my issues:

  • First of all, I think I use many variables : Do you see a way to have less?
  • Second, As u can see in the code below, there are more than one languages in the category "Andere(Others)". So, I would like to add another row if there is more than one language in this category. Any hints?

The code:

    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, '');

Thanks in advance!

Alf.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

新雨望断虹 2024-10-28 19:18:16

使用数组将帮助您实现整个过程的自动化,这是解决问题的一种方法,也许(几乎可以肯定)它不是最好的方法,但我可以是一个想法。

//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
....

但我建议你像对待主要语言一样对待其他语言我认为拥有“标准化”数据结构是件好事

using an array wil help you a lot to automatize the whole process, here is a way to solve the proble, maybe (almost for sure) it is not the best one but i could be an idea.

//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);
   }
}

that way you'll have a structure like that:

-english
--listening
--spoken
--reading
--written
-french
--listening
--spoken
--reading
--written
...
-others
--0
---listening
---spoken
---reading
---written
....

but i'll suggest you to treat others language just like the main ones i think it is good to have a 'normalized' data structure

孤檠 2024-10-28 19:18:16

首先,您应该查看“switch()”语句。

其次,最后一个“if”始终等于 true,因为 ||开始一个全新的表达式,非空字符串始终等于 true,因此 || “7”应该是 || $sl['StudentLanguage']['language_id'] == '7'

第三,如果您以不同的方式构造数组,则根本不需要 ifs。如果您使用学生 ID 作为数组键,您可以简单地打印您想要的数据。例如: echo $student_language[1]['listening'];

第四,这种类型的数据可能需要它自己的类而不是多维数组。

第五,您可以简单地用“?>”退出 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>

First, you should look into the "switch()" statement.

Second, the last "if" will always equate to true, because the || starts a whole new expression, and a non-empty string always equates to true, so || '7' should be || $sl['StudentLanguage']['language_id'] == '7'

Third, if you structured your array differently, you wouldn't need the ifs at all. if you used the student id as the array key, you could simply print the data you want. example: echo $student_language[1]['listening'];

Fourth, this type of data probably calls for its own class instead of multi-dimensional arrays.

Fifth, instead of putting all of the text you want to print into a variable, you can simply break out of PHP with a "?>", and put the html you want.

?>
<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文