Hi, it's me again.. I have this codes wherein I can export the data from the databse to Excel..

I get a Notice: Undefined variable: aData on one of the php files.. well two of them actually.. The weird thing is... The other page which I have modified - i only changed the sql code and the filename, is working.. it is exporting the data just perfectly fine..

so I'll share the codes.. Here is the one that is NOT WORKING:

<?php
$con=mysql_connect("localhost","root");
mysql_select_db("dbfirest",$con);

$Result=mysql_query("Select int_id as Official_Number,
                badge_number as Badge_Number,
                official_name as Official_Name,
                official_rank as Rank,
                official_designation as Designation,
                official_age as Age,
                official_bday_month as Birthday_Month,
                official_bday_day as Birthday_Day,
                official_bday_year as Birthday_Year,
                official_status as Status,
                official_contact as Contact,
                official_height as Height,
                official_weight as Weight   
                from tblofficials order by official_id asc");

//fetching each row as an array and placing it into a holder array ($aData)
while($row=mysql_fetch_assoc($Result)){
$aData[] = $row;
}
//feed the final array to our formatting function...
$contents=getExcelData($aData);

$filename="Officials_" . date('mdY') . ".xls";

//prepare to give the user a Save/Open dialog...
header ("Content-type: application/octet-stream");
header ("Content-Disposition: attachment; filename=".$filename);

//setting the cache expiration to 30 seconds ahead of current time. an IE 8 issue when opening the data directly in the browser without first saving it to a file
$expiredate = time() + 30;
$expireheader = "Expires: ".gmdate("D, d M Y G:i:s",$expiredate)." GMT";
header ($expireheader);

//output the contents
echo $contents;
exit;
?>

<?php
 function getExcelData($data){
    $retval = "";
    if (is_array($data)  && !empty($data))
    {
     $row = 0;
     foreach(array_values($data) as $_data){
      if (is_array($_data) && !empty($_data))
      {
          if ($row == 0)
          {
              // write the column headers
              $retval = implode("\t",array_keys($_data));
              $retval .= "\n";
          }
           //create a line of values for this row...
              $retval .= implode("\t",array_values($_data));
              $retval .= "\n";
              //increment the row so we don't create headers all over again
              $row++;
       }
     }
    }
  return $retval;
 }
?>

and here is the one that is perfectly working:

<?php
$con=mysql_connect("localhost","root");
mysql_select_db("dbfirest",$con);

$sql=mysql_query("Select int_id as Inventory_Number,
                int_number as Inventory_ID,
                int_image as Image,
                int_equipment as Inventory_Name,
                int_size as Size,
                int_desc as Description,
                int_quantity as Quantity    
                from tblinventory order by int_id asc");

//fetching each row as an array and placing it into a holder array ($aData)
while($row=mysql_fetch_assoc($sql)){
$aData[]=$row;
}
//feed the final array to our formatting function...
$contents=getExcelData($aData);

$filename="Inventory_" . date('mdY') . ".xls";

//prepare to give the user a Save/Open dialog...
header ("Content-type: application/octet-stream");
header ("Content-Disposition: attachment; filename=".$filename);

//setting the cache expiration to 30 seconds ahead of current time. an IE 8 issue when opening the data directly in the browser without first saving it to a file
$expiredate = time() + 30;
$expireheader = "Expires: ".gmdate("D, d M Y G:i:s",$expiredate)." GMT";
header ($expireheader);

//output the contents
echo $contents;
exit;
?>

<?php
 function getExcelData($data){
    $retval = "";
    if (is_array($data)  && !empty($data))
    {
     $row = 0;
     foreach(array_values($data) as $_data){
      if (is_array($_data) && !empty($_data))
      {
          if ($row == 0)
          {
              // write the column headers
              $retval = implode("\t",array_keys($_data));
              $retval .= "\n";
          }
           //create a line of values for this row...
              $retval .= implode("\t",array_values($_data));
              $retval .= "\n";
              //increment the row so we don't create headers all over again
              $row++;
       }
     }
    }
  return $retval;
 }
?>

hope somebody can enlighten with these THANKS!

The problem is here:

//fetching each row as an array and placing it into a holder array ($aData)
while($row=mysql_fetch_assoc($Result)){
$aData[] = $row;
}
//feed the final array to our formatting function...
$contents=getExcelData($aData);

Since you are not declaring $aData before the while loop, if the result set is empty then $aData doesn't exist. To solve add $aData = array(); before the loop and check also if you get something from the query, so you can stop the process, for example:

$aData = array();

if(mysql_num_rows($Result) > 0)
{
    //fetching each row as an array and placing it into a holder array ($aData)
    while($row=mysql_fetch_assoc($Result)){
    $aData[] = $row;
    }
}
else
{
    die('no data');
}

//feed the final array to our formatting function...
$contents=getExcelData($aData);

Thanks! I tried that actually and i'm getting a notice of undefined variable at contents

HI it's working now. THANKS A LOT!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.