Reputation: 148
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:
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.
Upvotes: 0
Views: 159
Reputation: 8996
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
Upvotes: 2
Reputation: 19251
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>
Upvotes: 1