2011-03-14 161 views
0

我是新來的PHP和一般網頁開發(第一大項目)。我主要解決了這個問題,但我認爲他們的方式我做到了效率不高(不聰明),但仍然沒有妥善解決,這就是爲什麼我想知道您的意見。以高效的方式填充矩陣

我有語言(student_language)的陣列。在每種語言中,我們都有一個名字的字符串和4個整數(用這種語言的技能的標點符號)。 我將這些信息轉換爲PDF格式,並創建了一個HTML表格來呈現那裏的語言。 所以,我必須用我的數組填充它。在這裏,我有我的問題:

  • 首先,我想我使用了很多變量:你看到一種方式有更少的?
  • 第二,如您在下面的代碼中看到的,「Andere(Others)」類別中有多種語言。所以,如果此類別中有多種語言,我想添加另一行。任何提示?

代碼:提前

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

謝謝!

Alf。

+0

從哪裏開始... – dqhendricks 2011-03-14 20:58:06

回答

2

使用數組西港島線幫助您很多使自動化的全過程,這裏是解決proble,可能(幾乎是肯定的),它是不是最好的,但我可能是一個想法的方式。

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

,但我會建議你去對待別人的語言,就像主要的,我認爲這是好事,有一個「標準化「數據結構

+0

它看起來太棒了!我會檢查並評論它,謝謝你! – alfizqu 2011-03-15 11:59:52

1

首先,您應該查看「switch()」語句。

其次,最後一個「如果」永遠等同於真實的,因爲||開始一個全新的表達式,並且非空字符串總是等於真,所以|| '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> 
+0

我明白了,這就是我一直在尋找!比你更好! – alfizqu 2011-03-15 12:00:25