Content of the lesson:
This lesson requires the result of this lesson: Processing and Editing Text File in PHP.
Try to consider possible problems of our solution to visualize the text file from the previous lesson. When we will not be able to use the simple replacement?
All these questions lead us to the fact that we need the ability to manipulate with every column separately. We want to be able to access each column separately and adjust it as we want.
An ideal situation would be if we were able to do something like this: "Save the content of the cell jmeno inside the variable $jmeno, content of the cell prijmeni inside the variable $prijmeni ... and then write only some of these variables into the HTML code.". To get a solution like this you have to use one of the PHP functions - the function explode.
To divide a string of variables connected by a separator (usually semicolon) to single parts you can use the function explode (description). This function requires two parameters at least:
This function returns an array of all variables (the length can differ according to the source string). You can see that arrays in PHP are indexed from 0.
$pole = explode(";",$radek);
After this command we can use every part separately. We can write any combination of columns as we want. Take a look at the following sample.
We might want to write a simple table with three columns which should look like the following one (only first two rows are available):
Last Name, First Name, Title | Abbreviation | Phone number |
---|---|---|
Babíková Eliška | 577007450 | |
Bárta Radim, Mgr. | Bár | 577007448 |
You can see several problems in these two rows:
Before editing our script we should think about the variables we are interested in for our output:
We did not have to create these variables but we did create them to better orientate inside our program. It is even better not to create additional variables. Why? Because you create copies of existing variables and need more memory to run your script which is not a good idea. However, we will use these variables in the next steps so leave them created.
We will modify our script:
<?php error_reporting(E_ALL ^ E_NOTICE); $nazev_souboru="data/zamestnanci.csv"; if (file_exists($nazev_souboru)) { $soubor=fopen($nazev_souboru, "r"); if($soubor){ $pocetradku = 0; while (!feof($soubor)){ $pocetradku = $pocetradku + 1; $radek = fgets($soubor,5000); if ($pocetradku == 1) { print("<h1>".$radek."</h1>"); } else { if ($pocetradku == 2){ print("<table cellspacing=\"0\" cellpadding=\"0\">"); print("<thead><tr><th>Last Name, First Name, Title</th><th>Abbreviation</th><th>Phone</th></tr></thead>"); print("<tbody>"); }else{ $pole = explode(";",$radek); $jmeno = $pole[0]; $prijmeni = $pole[1]; $titul = $pole[2]; $telefon = $pole[4]; $zkratka = $pole[6]; print("<tr>"); print("<td>".$prijmeni." ".$jmeno); if ($titul<>""){ print(", ".$titul); } print("</td><td>".$zkratka."</td><td>".$telefon."</td>"); print("</tr>"); } } } print("</tbody>"); print("</table>"); } else { print("Soubor ".$nazev_souboru." se nepodařilo otevřít pro čtení."); } } else { print("Soubor ".$nazev_souboru." neexistuje."); } ?>
We did two more adjustments of the nested condition if ($pocetradku == 2). The first one is that we wrote the titles of columns from our table by hand and did not use their titles from the CSV file. We have full control over the titles now.
if ($pocetradku == 2){ print("<table cellspacing=\"0\" cellpadding=\"0\">"); print("<thead><tr><th>Last Name, First Name, Title</th><th>Abbreviation</th><th>Phone</th></tr></thead>"); print("<tbody>"); }else{
The second adjustment is using the explode function to divide the line to single values. We created variables for single columns from the array and saved corresponding values inside. The process can be described in these steps:
}else{ $pole = explode(";",$radek); $jmeno = $pole[0]; $prijmeni = $pole[1]; $titul = $pole[2]; $telefon = $pole[4]; $zkratka = $pole[6]; print("<tr>"); print("<td>".$prijmeni." ".$jmeno); if ($titul<>""){ print(", ".$titul); } print("</td><td>".$zkratka."</td><td>".$telefon."</td>"); print("</tr>"); }
Try to find information about the function list in PHP (use the Internet). Try to understand its functionality and consider using it inside our program. How can it be used?
This function can assign items of array into a list of variables (description). This function has n parameters - variables. To this "function" (it is a constructor rather than a function) of n arguments (n variables) is assigned an array. The first item of the array will be assigned inside the first argument (variable), the second item inside the second variable...
We can show the functionality of the constructor list using an easy example. Consider an array $logininfo=(333,michalmiklas,XDfg4rF,Michal,Miklas). Execute the command list($uid,$login,$encryptedpasswd,$name, $surname) = $logininfo;. What will happen? A variable $uid will be created and assigned with value 333, variable $login will be created and assigned with value michalmiklas ... and finally the variable $surname will store the value Miklas.
$list($promenna-0, promenna-1, ... , $promenna-n) = $pole; /* $pole
should contain n+1 items */
We will use the construct list in our script instead of using single commands like $jmeno = $pole[0];, $prijmeni = $pole[1];,... to put the values of $pole inside variables.
}else{ $pole = explode(";",$radek); $jmeno = $pole[0]; $prijmeni = $pole[1]; $titul = $pole[2]; $telefon = $pole[4]; $zkratka = $pole[6]; print("<tr>"); print("<td>".$prijmeni." ".$jmeno); if ($titul<>""){ print(", ".$titul); } print("</td><td>".$zkratka."</td><td>".$telefon."</td>"); print("</tr>"); }
}else{ list($jmeno,$prijmeni,$titul,$email,$telefon,$fotografie,$zkratka) = explode(";", $radek); print("<tr>"); print("<td>".$prijmeni." ".$jmeno); if ($titul<>""){ print(", ".$titul); } print("</td><td>".$zkratka."</td><td>".$telefon."</td>"); print("</tr>"); }
The final file can be downloaded here: zpracovani-souboru-3.rar. You can also see the previous solution without construct list in this file (it is closed into comment marks).
Try to adjust the script to write only employees with filled phone number.
Try to write the number of employees after the headline and before the table. That means you should add a sentence "Total number of employees: " and the number of employees who are inside the following table.