0

I`m trying to make a simple page in php where data from an excel sheet is displayed.

I have gone through this link.

when I use foreach loop to print the values of $rowData I get only one value printed. My Excel Document consists of 2 Columns Attendance and register number only the first value is displayed.

here`s teh foreach am using

foreach($rowData as $row){
    foreach($row as $var){
         echo "{$var}<br/>";
    }
}

I searched for information regarding rangeToArray method but am not able to get information regarding what parameters are passed to it.Please can someone explain how the method works..

Edit: output when print_r used

Array ( 
    [0] => Array ( 
        [0] => Register Number 
        [1] => Attendance 
    ) 
) 
Array ( 
    [0] => Array (
        [0] => UR11CS012 
        [1] => 0.78 
    ) 
) 
Array (
    [0] => Array ( 
        [0] => UR11CS013 
        [1] => 0.65 
    ) 
) Array ( 
    [0] => Array ( 
        [0] => UR11CS015 
        [1] => 0.67
    )
) 

Edit: here is the code still printing last value only whats wrong ? should i have saved file in a seaprate folder uploaded file rather than use it from the temporary folder

<?php
            $name=$_FILES['u_file']['tmp_name'];
                try {
                    $inputFileType = PHPExcel_IOFactory::identify($name);
                    $objReader = PHPExcel_IOFactory::createReader($inputFileType);
                    $objPHPExcel = $objReader->load($name);
                } catch(Exception $e) {
                    die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
                }
            $sheet = $objPHPExcel->getSheet(0); 
            $highestRow = $sheet->getHighestRow(); 
            $highestColumn = $sheet->getHighestColumn();
            for ($row = 1; $row <= $highestRow; $row++){ 
                $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row,NULL,TRUE,FALSE);      
            }
            print_r($rowData);
        ?>
Community
  • 1
  • 1
AAB
  • 1,594
  • 2
  • 25
  • 40

3 Answers3

1

I think you print_r'd the $row and not the $rowData, Either way what I would do is use the list function like this

foreach($rowData as $row){
    list($rNum,$atten) = $row;
    echo $rNum .":". $atten ."\n";
}

As you always have only two columns using list will help reduce using a loop within a loop. Also the first echo will be the column names, so you can skip it using a for loop and not a foreach

for($i=1;$i<=count($rowData);$i++){
    list($rNum,$atten) = $rowData[$i];
    echo $rNum .":". $atten ."\n";
}

This will start you on the second index of the array, essentially skipping the column headers

Beyerz
  • 787
  • 2
  • 9
  • 20
  • I used print_r in a for() loop From the Link I mentioned above putting print_r outside the loop prints only the last value Currrently only 2 columns but there may be more like cgpa,placement cell atend etc... – AAB Dec 17 '13 at 15:01
  • 1
    Doing `print_r($rowData);` outside of the loops will print the entire array, not just the last "columns". Anywho... this answer is exactly what you need and will work. – superphonic Dec 17 '13 at 15:08
  • Please try the solution provided, and let me know if it doesn't work. Then we can try get at it from a different angel. If it does work, please accept it as the valid answer. – Beyerz Dec 17 '13 at 15:11
0

Change this line

$rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row,NULL,TRUE,FALSE);

to

$rowData[] = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row,NULL,TRUE,FALSE);

Notice the square brackets, this will make it an array. Before it was just over writing the one variable each time....

You will then be able to loop through $rowData using @Beyerz answer above...

EDIT: Quick explanation of the what that function is doing.

This is searching the excel file the for last row number with data in it and storing it in $highestRow

$highestRow = $sheet->getHighestRow(); 

This is doing the same but with the columns

$highestColumn = $sheet->getHighestColumn();

It is then looping through starting at 1 until it reaches the $highestRow, and storing the data in each column in the array.

So for example it starts at row A1, gets the data from each column on row A1 up to the $highestColumn <- the last column with data on it, and then it loops and grabs each column in A2, then A3... etc...

It stores each row in the array $rowData[]. The NULL, TRUE, FALSE are some settings you probably don't have to worry about, like calculating formulas if the cells contain them....

Hope that helps...

superphonic
  • 7,954
  • 6
  • 30
  • 63
  • @supersonic Thank You! :) can you also explain about rangeToArray method like U understand the First argument but the others I don`t know for what I`m passing – AAB Dec 17 '13 at 15:34
  • I have not used this PHPExcel class before, but it looks like it is finding the highest row (i.e. the last row with data in it) and then starting at A1 looping through and grabbing the data from each cell. Hang on... I'll update this answer with an explanation. – superphonic Dec 17 '13 at 15:43
-1

Your $rowData contains only one row. there for you have to output the data in the for loop or put the data in an extra array:

for ($row = 1; $row <= $highestRow; $row++){ 
    $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row,NULL,TRUE,FALSE);
    foreach($rowData[0] as $var){ 
        echo "{$var}<br/>";
    }     
}

or

for ($row = 1; $row <= $highestRow; $row++){ 
    $rowData[] = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row,NULL,TRUE,FALSE);
}
foreach($rowData as $row) {
    foreach($row[0] as $var) {  
        echo "{$var}<br/>";
    }
}
niyou
  • 875
  • 1
  • 11
  • 23
  • where is my fault? - uh - i forgot [] – niyou Dec 17 '13 at 15:28
  • $rowData is just getting overwritten each time in the loop, it doesn't magically become an array.... I see you just edited your second example to make it an array, but the first is still not going to work. Unless you edit that before I finish typing this... – superphonic Dec 17 '13 at 15:29
  • take a look at the print_r output – niyou Dec 17 '13 at 15:32
  • You are correct. My apologies, It was the initial formatting, I missed the closing bracket on the second `foreach`. – superphonic Dec 17 '13 at 15:34