Reputation: 2984
I am working on a Phone Directory list; now when I download the .CSV file from our phone directory it prints only gives me user ID, Extension, Name, and a few other roles that are not very relevant.
My problem is I want to group the Extensions/People by office, and since the spreadsheet does not offer any hints in that sense I thought of creating an array and matching it with my .CSV element.
The office extensions are not very consistent, so for Office A you may have extensions: 103, 215, and 105. Office B may be: 150, 104.
Now I created an array where I group extensions by office, and it looks something like this:
$offices = array(
'Office' => array (
'110',
'107',
'137',
'113'
),
// ...
);
For actually reading the spreadsheet I am using fgetcsv
which works very well without any type of filters, but I whenever I try to put in a variation to "filter" the while
loop I run into problems.
I tried using MySQL, but this is not a good method because whenever it needs to be updated, someone else will be doing it, and they do not want to work with PHP or MySQL.
This is the code for the list to be shown:
<?php
$file_name = htmlentities($_GET['file']);
// open CSV file
$handle = fopen("./_files/" . $file_name, "r");
$i = 0;
// gets data from csv file
echo "<div class='row'>";
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
// stores dates as variable $date
$date[$i] = $data[0];
$i++;
// create vars
$name = $data[2];
$email = $data[0];
$ext = $data[1];
if ($ext < 400)
{
?>
<div class="col-sm-4">
<ul class="mdc-list mdc-list--dense">
<li class="mdc-list-item">
<span class="mdc-list-item__text">
<?php echo $name; ?>
</span>
<span class="mdc-list-item__end-detail" aria-label="extention">
<?php echo $ext; ?>
</span>
</li>
</ul>
</div>
<?php
}
}
echo "</div>";
?>
Just to reiterate what the problem I need to solve is:
I need to match the results I get from while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
with the $offices
array.
If
( Worker A's extension == 102
), look inside of the $office
array creating an office match and output his $name
and $ext
under that office section.
Upvotes: 0
Views: 80
Reputation: 2984
After much thinking and pondering, I have with the help of @satafaka and a friend being able to come up with a fix for my specific problem.
@satafaka was correct to output all my data as an array; and then the management of the array was a bit more complex than I had intended for it to be. So I changed the $offices
array to a simpler array that I would be able to manage better.
It now looks something along these lines:
$offices = array(
0 => array (
'Office1','Office1','Office1','Office2','Office2', ...
1 => array (
'110', '107', '137', '113', '181', ...
);
Output should look like:
array(2) {
[0] => array(62) {
[0] => string(15)
"Office1" [1] => string(15)
...
Now explanation for what the array means: the offices are listed multiple times and the extensions are in order of the offices: So as you can see I list Office1
3 times and then the first three extensions will be from Office1
Now, the array that is provided from the .CSV file the user gives me I used the method provided by @satafaka:
// open CSV file
$handle = fopen("./_files/" . $file_name, "r");
$i = 0;
$csv_array = array();
// gets data from csv file
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
{
// create vars
$csv_array[$i]['email'] = $data[0];
$csv_array[$i]['ext'] = $data[1];
$csv_array[$i]['name'] = $data[2];
$i++; // increment $i so value will increase
}
Given that the array is correct the output should be: $csv_array[0]['...']
(... for email, ext, and or name).
array(95) {
[0] => array(3) {
["email"] => string(21)
"[email protected]" ["ext"] => string(3)
"100" ["name"] => string(20)
"Worker100"
}
Putting the two together in a functional way and use $offices
to sort the $csv_array
was the tricky part, and it looks something like this:
<?php
function myArraySearch($off_list, $ext_list)
{
/*
This function is fairly simple; we need to match
existing extension numbers to their correct office
--
To do so, we take two arrays:
$off_list - this array holds the offices and their extentions (the order array)
$ext_list - holds the actual user provided array (the array that needs to be arranged)
With the two arrays ready, we run a `for` loop to loop trough $off_list
while it does so, we then loop through $ext_list to get the content
and match it in the correct office.
*/
// setup $content -- which will hold output
$content = "<div class='card'><ul class='mdc-list'><li><strong>";
$office = null;
// this for loop will take the master list I want to go by
// then first set $a to zero, count $a till it reaches the
// limit; which is the number of office extentions.
for ($a = 0; $a < count($off_list[0]); $a++)
{
// check to see if $office is null
if (!$office)
{
// given it is not null create assign
// $off_list[][] to $office
$office = $off_list[0][$a];
// then add $office to $content
$content .= $office;
}
// now check to see if office is not
// equals to $off_list[][]
else if ($office != $off_list[0][$a])
{
// if indeed it isnt, then assign it
$office = $off_list[0][$a];
// then add $office in the context of
// $content
$content .= "</strong></li></ul></div><div class='card'><ul class='mdc-list'><li><strong>" . $office . "</strong></li>";
}
// here we prepare to add our $ext_list array
$content .= "<ul class='mdc-list mdc-list--dense'>";
// create the loop for $ext_list
for ($c = 0; $c < count($ext_list); $c++)
{
// to make sure that the array was being looped
// I had to created a nested `foreach` loop that
// cycles through the $ext_list array
foreach ($ext_list as $c => $item)
{
// finally check to see if the $off_list extensions
// match the $ext_list extentions; if so we create
// `<li>` elements which will hold the content.
if ($off_list[1][$a] == $ext_list[$c]['ext'])
{
$content .= "<li class='mdc-list-item'>"
. $ext_list[$c]['name'] .
"<span class='mdc-list-item__end-detail'>" . $ext_list[$c]['ext'] . "</span>
</li>";
}
}
}
// close the `<ul>` element
$content .= "</ul>";
}
// close the surrounding elements as well
$content .= "</ul></div>";
// once complete return $content which holds
// the precious information.
return $content;
}
Upvotes: 1
Reputation: 1262
Whenever i faced a similar situation with csv or other files, i had to do some sorting, calculating etc. things, before printing the final result.
Knowing that the while
loop parses line per line, there wasn't any other way than adding all the data in a PHP array and do the things i want with PHP.
In your example (not really sure if this is gonna help you), your code could be:
<?php
$file_name = htmlentities($_GET['file']);
// open CSV file
$handle = fopen("./_files/" . $file_name, "r");
$i = 0;
$my_array = array();
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
//add your inputs in your multi-dimensional array, do not echo anything yet
$my_array[$i]['email'] = $data[0];
$my_array[$i]['name'] = $data[2];
$my_array[$i]['ext'] = $data[1];
$i++;
}
//Here, in $my_array you have your whole csv file, in PHP. You can do whatever you want it
var_dump($my_array);
edit
For the $offices
array, you can use this code
foreach ($offices as $key => $office) {
foreach($office as $office_key => $office_val) {
if ($office_val == $data[1]) {
$my_array['department'] = $office_key;
break;
}
}
}
Hope i helped.
Upvotes: 2