PHP Excel Reader - for loop

I am using the following code to read an excel sheet in php. It works fine, however I am confused how to loop through it the right way. In the following code, $val outputs all the data in the sheet, but I wanna see one row at a time, not sure how to make that happen. I am not sure how to access elements within $val

[php]
require_once (ABSPATH . ‘PHPExcel_1.8.0_doc/Classes/PHPExcel.php’);

$objPHPExcel = PHPExcel_IOFactory::load(ABSPATH . “test.xlsx”);
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
$worksheetTitle = $worksheet->getTitle();
$highestRow = $worksheet->getHighestRow(); // e.g. 10
$highestColumn = $worksheet->getHighestColumn(); // e.g ‘F’
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
$nrColumns = ord($highestColumn) - 64;

for ($row = 1; $row <= $highestRow; ++ $row) {
    for ($col = 0; $col < $highestColumnIndex; ++ $col) {
        $cell = $worksheet->getCellByColumnAndRow($col, $row);
        $val = $cell->getValue();
        echo '<td>' . $val . '<br></td>';
    }
   
}

}
[/php]

Can you explain what you mean by, one row at a time? As far as you do an action and it outputs another row, or what?

Yes for example:
$val outputs the following:

Amy
Tesch
[email protected]
64732882
tom
Lee
[email protected]
489384922
John
Smith
[email protected]
84395893942
Sam
Jon
[email protected]
84928492

But I want to just access the first names, last names and assign them to different variables. Does that make sense?

[php] for ($row = 1; $row <= $highestRow; ++ $row) {
for ($col = 0; $col < $highestColumnIndex; ++ $col) {[/php]

You are looping thru the row, then the column. If you think about it, you can figure out which column belongs with which relation.

[member=72272]astonecipher[/member] Yeah I know however the goal is to access elements of $val. The way it is looping is fine.

If the goal is to access that value, your looking at the wrong problem.

Let’s say you use what is stored in $val, what logic would you use to distinguish between first name and last name? What you need to do, is designate variables that hold $column[0], $column[1], $column[2] and $column[3] respectively and not worried about $val.

[member=72272]astonecipher[/member] ok that makes sense. Which variable is the column then?

[member=72272]astonecipher[/member] I made the following changes and I am able to separate the variables.However, I still don’t know how to loop through each out. for instance, “fname” has 4 names in it, and I want to let’s say access fname[1], when I do it , it gives an error
[php]
$fname = $worksheet->getCellByColumnAndRow(0, $row);
$lname = $worksheet->getCellByColumnAndRow(1, $row);
$email = $worksheet->getCellByColumnAndRow(2, $row);
$phone = $worksheet->getCellByColumnAndRow(3, $row);
[/php]

What changes in all did you make?

You are off to a good start!

[php]$fname =[/php]
Should only hold a single value, you will still want to index it,
[php]$fname[] =[/php]

Or use it as an object.

Yes that is what I don’t understand how to index it using a loop. Please bear in mind I am a newbie to PHP. The goal is to assign them to a link as follows: “www.example.com/fname” so that four different links get opened up when I execute the code.

I also tried outputting $fname[1] but that results into this error: “Fatal error: Cannot use object of type PHPExcel_Cell as array”

Not sure what you mean by the links portion,

[php]foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
$worksheetTitle = $worksheet->getTitle();
$highestRow = $worksheet->getHighestRow(); // e.g. 10
$highestColumn = $worksheet->getHighestColumn(); // e.g ‘F’
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
$nrColumns = ord($highestColumn) - 64;

for ($row = 1; $row <= $highestRow; ++ $row) {
        $fnameCell = $worksheet->getCellByColumnAndRow(0, $row);
        $lnameCell = $worksheet->getCellByColumnAndRow(1, $row);
        $emailCell = $worksheet->getCellByColumnAndRow(2, $row);
        $phoneCell = $worksheet->getCellByColumnAndRow(3, $row);
        $fname[] = $fnameCell->getValue();
        $lname[] = $lnameCell->getValue();
        $email[] = $emailCell->getValue();
        $phone[] = $phoneCell->getValue();
}

}

echo “

There were " . count($fname) . " first names found.

”;

echo "









";
for( $index = 0; $index < count($fname); $index++){
echo "




";
}
echo “
First Name Last Name Email Phone #
{$fname[$index]} {$lname[$index]} {$email[$index]} {$phone[$index]}
”;[/php]
Sponsor our Newsletter | Privacy Policy | Terms of Service