doupafu6980 2015-07-11 06:17
浏览 619
已采纳

PHPExcel循环遍历行和列

Need help identifying weird problem that i'm facing. I did tried searching in stack overflow but didn't find any possible answer.

Here is sample program that works displaying all rows and columns on UI

<?php

date_default_timezone_set('America/Los_Angeles');
require_once 'PHPExcel-1.8/Classes/PHPExcel.php';
include 'PHPExcel-1.8/Classes/PHPExcel/IOFactory.php';

$path = 'demo.xlsx';

$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow();
$highestColumn = $sheet->getHighestColumn();
 $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
for ($row = 2; $row <= $highestRow; ++ $row) {
    $val=array();
for ($col = 0; $col < $highestColumnIndex; ++ $col) {
   $cell = $worksheet->getCellByColumnAndRow($col, $row);
   $val[] = $cell->getValue();
 //End of For loop   
}

$Col1 = $val[0] ;
$Col2 = $val[1] ;
$Col3 = $val[2];

echo $Col1;
echo $Col2;
echo $Col3;
echo "<br>";

//End of for loop
}
?>

This program works perfectly fine printing all columns and rows for n-lenght

Problem - Now our requirement is to get values of Col1, Col2, Col3 and using mysql_query compare into database and do further action.

Minute we add anything above //End of for loop. It only iterates once and stops without throwing any php errors.

e.g.

.....

echo $Col1;
echo $Col2;
echo $Col3;
echo "<br>";

**$sql = mysql_query("select COALESCE(MAX(SrNo), 0)  AS Max_No from TABLEA where ColumnA = 1 and ColumnB = '$Col3'");
    $row = mysql_fetch_array($sql);


    echo $row["Max_No"];**

//End of for loop
}
?>

If we add above SQL the same program only iterates once and stops? It doesn't show any errors in logs or on screen.

Thanks in advance for your help!.

  • 写回答

2条回答 默认 最新

  • doukoumi3389 2015-07-11 10:39
    关注

    As you're using the same variable $row for the row number in the Excel iteration and for the result of your select query, it's not surprising that you're running into problems.....

    The integer value that holds the Excel row number is being overwritten by the array that you get from your SQL query, and then you're trying to use that result array as the next Excel row number

    Solution: Use a different variable for these two elements.

    $rowData = mysql_fetch_array($sql);
    echo $rowData["Max_No"];**
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?