Hi all,

I am having a problem with the code below. When I run it I get the error:

Warning: Invalid argument supplied for foreach() in /var/www/vhosts/baggagepin.com/httpdocs/sales_report.php on line 85
FPDF error: Some data has already been output, can't send PDF file

I am trying to run the QUERY and extract the data that matches the variable "$ecndi". The code I am working on is:

$ecnid = 1;
mysql_select_db($database_pin, $pin);
$query_comm = sprintf("SELECT c.code, c.begin_date,c.expire_date,c.discount,c.member_id,c.product_id,c.used_count,
c.saleprice,c.coupon_id,c.paid,p.payment_id,p.member_id,p.coupon_id,p.completed,p.time,p.amount, p.product_id, 
p.p_discount, p.comm_paid, p.d_paid, m.member_id, m.agentrate 
FROM coupon c ,payments p, members m  
WHERE c.coupon_id = " . $ecnid . "
AND c.coupon_id = p.coupon_id
AND m.member_id = %s
AND p.completed = 1 AND p.comm_paid = 0 ORDER BY p.payment_id"); $comm = mysql_query($query_limit_comm, $pin) or die(mysql_error()); $row_comm = mysql_fetch_assoc($comm); define('FPDF_FONTPATH','/var/www/vhosts/mydomain/httpdocs/fonts/'); require('fpdf.php'); $vals1 = ""; class PDF extends FPDF { //Load data function LoadData($file) { //Read file lines $row_comm=file($query_comm); $data=array(); foreach($row_comm as $line) $data[]=explode(';',chop($line)); return $data; }

Can anyone see where I am going wrong.

    $query_comm does not exist inside LoadData(). Also, it is an associative array, not a filename.

    You seem to be doing two different things in the same piece of code. The part before the define() does one thing, the part after the define() does another.

      laserlight wrote:

      $query_comm does not exist inside LoadData(). Also, it is an associative array, not a filename.

      You seem to be doing two different things in the same piece of code. The part before the define() does one thing, the part after the define() does another.

      Maybe this is what you want to do.
      Instead of passing $file to your function.

      /halojoy
      🙂

      //Load data
      function LoadData( $query_comm )
      {
          //Read file lines
          $row_comm = file( $query_comm );
      
      $data = array();
      foreach( $row_comm as $line )
      {
          $data[] = explode( ';', chop( $line ));
      }
      return $data; // an array of arrays, rows
      
      } //end function
      
      

        Hi Laserlight,

        Many thanks for your reply.

        What I am trying to do is:

        Run the query and produce the result into an array which I can use to extract the row of data.

        The query works butits the extraction of the data that I am having errors with.

        The data that is extracted into row is to be used in the creation of a PDF using FPDF.

        The function:

        function LoadData($file) 
        { 
            //Read file lines 
            $row_comm=file($query_comm); 
            $data=array(); 
            foreach($row_comm as $line) 
                $data[]=explode(';',chop($line)); 
            return $data; 
        } 
        

        I have change to:

        function LoadData($data)
        {
            //Read file lines
            $row_comm=array($comm);
            foreach($row_comm as $line)
                $row_comm[]=explode(';',chop($line));
            return $row_comm;
        }
        

        and now the ouput is 2 line with the word "ARRAY".

        Can you advise please.

          did you see my code
          and question about your LoadData()

            Hi, halojoy, many thansk for the reply.

            I have the first part working, many thanks. But now I have a similar problem with the next part of the script.

            This works great:

            //Load data
            function LoadData( $query_comm )
            {
                //Read file lines
                $row_comm = array( $query_comm );
            
            $data = array($query_comm);
            foreach( $row_comm as $line )
            {
                $data[] = explode( ';', chop( $line ));
            }
            return $data; // an array of arrays, rows
            
            } //end function 
            

            and the this does not work. Can you advise please.

            //Simple table
            function BasicTable($data)
            {
            
            //Data
            foreach($data as $row)
            {
                foreach($row as $col)
                    $this->Cell(40,6,$col,1);
                $this->Ln();
            }
            }
            

              How does it not work?

              It would be good to post the smallest and simplest code that demonstrates the problem.

                Hi LaserLight, sorry for that I should have post more than just "it don't work".

                I am getting an error

                Warning: Invalid argument supplied for foreach() 
                

                This error is on line 102 of the script.

                //Data
                foreach($data as $row) // Line 102
                {
                foreach($row as $col)
                $this->Cell(40,6,$col,1);
                        $this->Ln();
                }
                } //end function 
                

                So what I have so far is

                $ecnid = 1;
                mysql_select_db($database_pin, $pin);
                $query_comm = sprintf("SELECT c.code, c.begin_date,c.expire_date,c.discount,c.member_id,c.product_id,c.used_count,
                c.saleprice,c.coupon_id,c.paid,p.payment_id,p.member_id,p.coupon_id,p.completed,p.time,p.amount, p.product_id, 
                p.p_discount, p.comm_paid, p.d_paid, m.member_id, m.agentrate 
                FROM coupon c ,payments p, members m  
                WHERE c.coupon_id = " . $ecnid . "
                AND c.coupon_id = p.coupon_id
                AND m.member_id = %s
                AND p.completed = 1 AND p.comm_paid = 0 ORDER BY p.payment_id"); $comm = mysql_query($query_limit_comm, $pin) or die(mysql_error()); $row_comm = mysql_fetch_assoc($comm); define('FPDF_FONTPATH','/var/www/vhosts/mydomain/httpdocs/fonts/'); require('fpdf.php'); $vals1 = ""; class PDF extends FPDF { //Load data function LoadData( $query_comm ) { //Read file lines $row_comm = array( $query_comm ); $data = array($query_comm); foreach( $row_comm as $line ) { $data[] = explode( ';', chop( $line )); } return $data; // an array of arrays, rows } //end function //Simple table function BasicTable($data) { //Data foreach($data as $row) { foreach($row as $col) // line 102 $this->Cell(40,6,$col,1); $this->Ln(); } } //end function } $pdf=new PDF(); $pdf->AliasNbPages(); $data=$pdf->LoadData($row_comm); $pdf->AddPage(); //Set font $pdf->SetFont('Arial','B',36); $pdf->SetTextColor(0,0,0); $pdf->SetXY(5,130); $pdf->BasicTable($data); $pdf->Output();

                Many thanks

                  First define pdf.php:

                  <?php
                  require_once 'fpdf.php';
                  define('FPDF_FONTPATH','/var/www/vhosts/mydomain/httpdocs/fonts/');
                  
                  class PDF extends FPDF
                  {
                      private $db;
                      private $data;
                  
                  function __construct($db)
                  {
                      $this->db = $db;
                  }
                  
                  function LoadData($ecnid, $member_id)
                  {
                      $query = sprintf("SELECT c.code, c.begin_date, c.expire_date,
                                               c.discount, c.member_id, c.product_id,
                                               c.used_count, c.saleprice, c.coupon_id,
                                               c.paid, p.payment_id, p.member_id,
                                               p.coupon_id, p.completed, p.time,p.amount,
                                               p.product_id, p.p_discount, p.comm_paid,
                                               p.d_paid, m.member_id, m.agentrate
                                        FROM coupon c, payments p, members m
                                        WHERE c.coupon_id = %d
                                            AND c.coupon_id = p.coupon_id
                                            AND m.member_id = %d
                                            AND p.completed = 1
                                            AND p.comm_paid = 0
                                        ORDER BY p.payment_id",
                                       $ecnid,
                                       $member_id);
                  
                      $result = mysql_query($query, $this->db) or die(mysql_error());
                      $this->data = array();
                      while ($row = mysql_fetch_assoc($result))
                      {
                          $this->data[] = $row;
                      }
                  }
                  
                  //Simple table
                  function PrintBasicTable()
                  {
                      foreach($this->data as $row)
                      {
                          foreach($row as $col)
                          {
                              $this->Cell(40,6,$col,1);
                          }
                          $this->Ln();
                      }
                  }
                  }
                  ?>

                  Then use your PDF class:

                  <?php
                  require 'pdf.php';
                  // ...
                  $ecnid = 1;
                  mysql_select_db($database_pin, $pin);
                  
                  $pdf = new PDF($pin);
                  $pdf->AliasNbPages();
                  $data = $pdf->LoadData($ecnid, $member_id);
                  
                  $pdf->AddPage();
                  //Set font
                  $pdf->SetFont('Arial','B',36);
                  $pdf->SetTextColor(0,0,0);
                  $pdf->SetXY(5,130);
                  $pdf->PrintBasicTable();
                  $pdf->Output();
                  
                  ?>

                  One important change that I made was to turn:

                  $row_comm = mysql_fetch_assoc($comm);

                  into:

                  $this->data = array();
                  while ($row = mysql_fetch_assoc($result))
                  {
                      $this->data[] = $row;
                  }

                  The reason is that mysql_fetch_assoc() only returns one row of the result set, but you want all the rows. Note also my correction to the sprintf(); but I made up $member_id as it was not given.

                  Originally, LoadData() was intended to load from a file, but since you changed your mind and wanted it to load from a database, it should be changed accordingly, as per my example.

                    Hi LaserLight.

                    Sorry to be a pain. I have produced the script and made it fit the style I write in (hope you don't mind). But when I run it I am getting an error of which I cannot find any information about. can you please advise.

                    I change this part becaue I did not understand how it worked (sorry) and used the database connection process I normally use.

                    private $db;
                    private $data;

                    function __construct($db)
                    {
                        $this->db = $db;
                    }

                    FPDF error: Incorrect orientation:

                    The script now looks like this:

                    session_cache_limiter('private');
                    session_start();
                    //Connect to your database
                    require_once('../../Connections/pin.php');
                    require('fpdf.php');
                    define('FPDF_FONTPATH','/var/www/vhosts/mydomain/httpdocs/fonts/');
                    if (!function_exists("GetSQLValueString")) {
                    function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
                    {
                      $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
                    
                      $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
                    
                      switch ($theType) {
                        case "text":
                          $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
                          break;    
                    case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } } $colname_comm = "-1"; if (isset($_SESSION['ecnid'])) { $colname_comm = $_SESSION['ecnid']; } $colname1_comm = "-1"; if (isset($_SESSION['m_id'])) { $colname1_comm = $_SESSION['m_id']; } class PDF extends FPDF { private $data; //Select the Products you want to show in your PDF file function LoadData($ecnid, $member_id) { mysql_select_db($database_pin, $pin); $query = sprintf("SELECT c.coupon_id, c.code, c.discount, c.used_count, c.paid, p.member_id, p.amount FROM coupon c, payments p WHERE c.coupon_id = %s AND p.member_id = %s", GetSQLValueString($colname_comm, "int"),GetSQLValueString($colname1_comm, "int")); $result = mysql_query($query, $pin) or die(mysql_error()); $this->data = array(); while ($row = mysql_fetch_assoc($result)) { $this->data[] = $row; } } //Simple table function PrintBasicTable() { foreach($this->data as $row) { foreach($row as $col) { $this->Cell(40,6,$col,1); } $this->Ln(); } } } // ... $pdf = new PDF($pin); $pdf->AliasNbPages(); $data = $pdf->LoadData($ecnid, $member_id); $pdf->AddPage(); //Set font $pdf->SetFont('Arial','B',36); $pdf->SetTextColor(0,0,0); $pdf->SetXY(5,130); $pdf->PrintBasicTable(); $pdf->Output();
                      Write a Reply...