0

I am working on a PHP file that will export data from a table to an Excel file. One piece of code I need to use is in a file that I included in the current file. When I try to run the code, however, I get the following error: Fatal error: Call to undefined function buscarFaltantesPorProveedor() in /require/exportarFaltantesExcel.php on line 13.

I am guessing I get this error because I am calling a class variable, but I am not sure. Can you please help me? Thanks!

PHP excel code:

<?php
error_reporting(E_ALL | E_WARNING | E_NOTICE);
ini_set('display_errors', 1);

include('../accesodatos/consultas.php');
require_once('../excel/Classes/PHPExcel.php');
require_once('../excel/Classes/PHPExcel/IOFactory.php');    
$proveedor = $_GET['proveedor'];
echo $proveedor;

$objPHPExcel = new PHPExcel();
 // $objPHPExcel = new PHPExcel();
$query = buscarFaltantesPorProveedor($proveedor);
 $row = 1;

 while($row_data = mysql_fetch_array($query)){
    $col = 0;
    foreach($row_data as $key=>$value) {
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value);
        $col++;
    }
    $row++;
}

 header('Content-Type: application/vnd.openxmlformats-   officedocument.spreadsheetml.sheet');
 header('Content-Disposition: attachment;filename="'.$proveedor.date("Y-m-d").'".xlsx"');
 header('Cache-Control: max-age=0');

 $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
 $objWriter->save('php://output');
?>

Piece of code in consultas.php:

function buscarFaltantesPorProveedor($proveedor){
conectar();
if(strcmp($proveedor,"Todos") == 0){
    $instruccion = "SELECT * FROM articulos, estatus, ordenes, proveedor WHERE art_id_estatus = est_id AND art_id_orden = ord_id AND art_id_proveedor = pro_id_clave AND art_num_guia IS NULL";
}else{
    $instruccion = "SELECT * FROM articulos, estatus, ordenes, proveedor WHERE art_id_estatus = est_id AND art_id_orden = ord_id AND art_id_proveedor = pro_id_clave AND art_num_guia IS NULL and pro_id_clave = '$proveedor'";
}

$tabla = mysql_query($instruccion);
return $tabla;
}

Also, I tried using $this to call the method but I also got an error. Why is that? Can't I use $this on a method?

Julio Garcia
  • 393
  • 2
  • 7
  • 22

2 Answers2

2

A possible reason: maybe buscarFaltantesPorProveedor is a method of a class. Check if it is inside class AlgumaCoisa {  ... } in consultas.php, like this:

class AlgumaCoisa {
  /* Instead AlgumaCoisa, any name can be used. */ 

  /* Some other code can go here */

  function buscarFaltantesPorProveedor($proveedor){
    /* ... */
  }

  /* Some other code can go here */

}

(it won’t be named AlgumaCoisa, it will have some other name; also, additional things can be written before the opening {).

Please check consultas.php. If this is indeed a member of a class, then you have to call it like $object->buscarFaltantesPorProveedor where $object is an instance of class. You need to create an instance of the class, like this:

$object = new AlgumaCoisa(/* some AlgumaCoisa-dependent code here */);

Instead of AlgumaCoisa, write the name of the class that is used in consultas.php.

Note that the parameters inside brackets. The parameters in the brackets will be determined by another method inside the class, __construct.

Another possible reason: maybe consultas.php is in different namespace. Check if there is a namespace AlgumaCoisa; in the top of consultas.php. If there is, you need to call your function AlgumaCoisa\buscarFaltantesPorProveedor.

1

I had the same error a day before yesterday.

index.php

<?php
echo 'This works';
require 'functions.php';
echo butThis();
?>

functions.php

<?
function butThis() {return "not works";}
?>

It take me a while... but today I see the problem is in short_open_tag :-)