1

Getting error while Printing dynamic data in phpexcel - Error Invalid Argument while printing data from my MYSQL Database

$labels = array();
$i = 0;
while ($row1 = mysql_fetch_array($query_result1, MYSQL_ASSOC)) {
    $labels[$i++] = $row1["label"];
} 
  foreach($labels[0] as $ind=>$label){ //error invalid argument
        $letter = range('A', 'Z')[$ind];
        $tmp = explode('>',$label); 
        $col_names[] = $tmp[0];
        echo $letter.'1'."\r\n";

        echo "Column -> $tmp[0] \r\n"; 
}  

    foreach ($labels as $ind=>$item){ //Error invalid Argument

    $index = $ind + 2;

foreach($item as $ind2=>$data){

      $letter = range('A', 'Z')[$ind2];

    $val = explode('>',$data);

    $objPHPExcel->getActiveSheet()->setCellValue("$letter$index",$val[1]);

}

} 

My Var_dump($labels) output is

  array(15) {
  [0]=>
  string(2) "EY"
  [1]=>
  string(3) "PWC"
  [2]=>
  string(8) "Deloitte"
  [3]=>
  string(4) "KPMG"
  [4]=>
  string(14) "Grant Thornton"
  [5]=>

I Added my Whole Code Please Once Please

it is giving invalid argument error

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 1
    $labels[0] is not an array. – Markus Zeller Feb 11 '20 at 09:22
  • 3
    Every time you use [the `mysql_`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) database extension in new code **[this happens](https://media.giphy.com/media/kg9t6wEQKV7u8/giphy.gif)** it is deprecated and has been for years and is gone for ever in PHP7.0+. If you are just learning PHP, spend your energies learning the `PDO` or `mysqli` database extensions and prepared statements. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly Feb 11 '20 at 09:22
  • you could do `foreach($labels...` thats an array but `$labels[0]` is a scalar value – RiggsFolly Feb 11 '20 at 09:24
  • ` foreach ($labels as $ind=>$item){ $index = $ind + 2; foreach($item as $ind2=>$data){ $letter = range('A', 'Z')[$ind2]; echo "$letter$index \r\n"; $val = explode('>',$data); } echo "\r\n\r\n"; } ` also this code have same error? why – erroranderr Feb 11 '20 at 09:47

1 Answers1

1

As mentioned in the comments, $labels[0] is not an array, $labels however is.

Change:

foreach($labels[0] as $ind=>$label){

To:

foreach($labels as $ind => $label) {

Further reading.


Expanding based on your comments:

foreach($labels as $ind => $label) {

    $index = $ind + 2;

    $letter = range('A', 'Z')[$ind2];
    $val = explode('>', $data);

    $objPHPExcel->getActiveSheet()->setCellValue($letter . $index, $val[1]);

}
Mark
  • 1,852
  • 3
  • 18
  • 31
  • ` foreach ($labels as $ind=>$item){ $index = $ind + 2; foreach($item as $ind2=>$data){ $letter = range('A', 'Z')[$ind2]; echo "$letter$index \r\n"; $val = explode('>',$data); } echo "\r\n\r\n"; } ` it happens with this code too – erroranderr Feb 11 '20 at 09:47
  • That's because `$item` is not an array, you are trying to iterate through a string. – Mark Feb 11 '20 at 09:58
  • well i appreciate if you could help me to do it in better way! – erroranderr Feb 11 '20 at 10:04
  • If you do a `var_dump($labels);` just before you do `foreach($labels` and post the result in your question, I should be able to fix this for you. – Mark Feb 11 '20 at 10:06
  • I changed please check @MarkOverton – erroranderr Feb 11 '20 at 10:14
  • I've updated my answer but I need more information, what're you trying to achieve. – Mark Feb 11 '20 at 10:22
  • I am trying to print row and column dynamically in phpexcel through array but i am trying to print array[0] always see this is my old question which is unsolved [link](https://stackoverflow.com/questions/60164051/phpexcel-dynamic-row-and-column-incrementing-is-not-working-for-array-in-phpexc) – erroranderr Feb 11 '20 at 10:29
  • Please see revised answer – Mark Feb 11 '20 at 11:06