Advertisement

09.08.2006 at 06:40PM PDT, ID: 21983266
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.4

Exporting a table to Excel

Asked by horalia in PHP Scripting Language

Tags: , , ,

I have a page that displays the query results in a table. I also added some code to open the table in an Excel spreadsheet. The problem that I'm having is that if I add a where clause to the query, the data will not open in the excel spreadsheet. The parameters for the where clause come from a previous table. Here's the code, it's quite long, so please bear with me:

<?php
include '../mysql_connect.php';
//PERIOD NAME
if (isset($_GET["periodname"]))
  $periodName = $_GET["periodname"];
//PERIOD START DATE
if (isset($_GET["pstartdate"]))
  $pstartDate = $_GET["pstartdate"];
//PERIOD END DATE
if (isset($_GET["penddate"]))
  $pendDate = $_GET["penddate"];

$data="
<html>
<head>
</head>
<body>
<table class=\"rpt\" width=\"100%\" border=\"0\">
<tr>
      <th class=\"rpt\">&nbsp</th>
      <th colspan=\"14\" class=\"rpt\">NV & West</th>
      <th colspan=\"5\" class=\"rpt\">Total Remakes</th>
</tr>
<tr>
      <th width=\"6%\" class=\"rpt\">Date</th>
      <th width=\"5%\" class=\"rpt\">Printed</th>
      <th class=\"rpt\">Total Pages</th>
      <th class=\"rpt\">Last Copy</th>
      <th class=\"rpt\">Avg Min Late</th>
      <th class=\"rpt\">Last Plate</th>
      <th class=\"rpt\">Avg Min Late</th>
      <th class=\"rpt\">Press Start</th>
      <th class=\"rpt\">Avg Min Late</th>
      <th class=\"rpt\">First Bndl</th>
      <th class=\"rpt\">Avg Min Late</th>
      <th class=\"rpt\">Press Stop</th>
      <th class=\"rpt\">Avg Min Late</th>
      <th class=\"rpt\">Last Bndl</th>
      <th class=\"rpt\">Avg Min Late</th>
      <th class=\"rpt\">Prod</th>
      <th class=\"rpt\">New News</th>
      <th class=\"rpt\">Cosmetic</th>
      <th class=\"rpt\">Total</th>
      <th class=\"rpt\">% of Run</th>
</tr>";

$query="SELECT date_format(rpt_date,'%a %e-%b'), sorc, cmechctrstp, cmechctrstr, pages, g_clstcpy, clstcpy, ".
         "g_clstpte, clstpte, g_cprsstr, cprsstr, g_cfstbndl, cfstbndl, g_cprsstp, cprsstp, g_clstbndl, clstbndl, ".
         "fprd, fedn, fedc, date_format(rpt_date,'%w') ".
         "FROM production_rpt ".
         //"WHERE DATE(rpt_date)>='$pstartDate' AND DATE(rpt_date)<='$pendDate' ".
         "ORDER BY rpt_date;";

$result = mysql_query($query);
while(($row = mysql_fetch_array($result)) != false)
{
      $rpt_date=$row[0];
        $sorc=$row[1];
        $cmechctrstp=$row[2];
        $cmechctrstr=$row[3];
        $pages=$row[4];
      $g_clstcpy=$row[5];
      $clstcpy=$row[6];
      $g_clstpte=$row[7];
      $clstpte=$row[8];
      $g_cprsstr=$row[9];
      $cprsstr=$row[10];
      $g_cfstbndl=$row[11];
      $cfstbndl=$row[12];
      $g_cprsstp=$row[13];
      $cprsstp=$row[14];
      $g_clstbndl=$row[15];
      $clstbndl=$row[16];
      $fprd=$row[17];
      $fedn=$row[18];
      $fedc=$row[19];
      $day_num=$row[20];
        
        if($sorc='C')
      {
          $printed = number_format(($cmechctrstp - $cmechctrstr)/2,0,'.',',');
      } else {
              $printed = number_format($cmechctrstp - $cmechctrstr,0,'.',',');
      }
      if ($clstcpy>$g_clstcpy)
      {
          $mv_clstcpy=get_time_diff($g_clstcpy, $clstcpy);
      } else {
              $mv_clstcpy='';
      }
      if ($clstpte>$g_clstpte)
      {
          $mv_clstpte=get_time_diff($g_clstpte, $clstpte);
      } else {
              $mv_clstpte='';
      }
        if ($cprsstr>$g_cprsstr)
      {
          $mv_cprsstr=get_time_diff($g_cprsstr, $cprsstr);
      } else {
              $mv_cprsstr='';
      }
        if ($cfstbndl>$g_cfstbndl)
      {
          $mv_cfstbndl=get_time_diff($g_cfstbndl, $cfstbndl);
      } else {
              $mv_cfstbndl='';
      }
        if ($cprsstp>$g_cprsstp)
      {
          $mv_cprsstp=get_time_diff($g_cprsstp, $cprsstp);
      } else {
              $mv_cprsstp='';
      }
      if ($clstbndl>$g_clstbndl)
      {
          $mv_clstbndl=get_time_diff($g_clstbndl, $clstbndl);
      } else {
              $mv_clstbndl='';
      }
      $t_r=$fprd+$fedn+$fedc;
      $tr_p=number_format($t_r/$pages*100,2,'.','').'%';
      
      $data.="<tr class=\"d".($i & 1)."\">";
      $data.="<td>$rpt_date</td>";
      $data.="<td>$printed</td>";
      $data.="<td>$pages</td>";
      $data.="<td>$clstcpy</td>";
      $data.="<td>$mv_clstcpy</td>";
      $data.="<td>$clstpte</td>";
      $data.="<td>$mv_clstpte</td>";
      $data.="<td>$cprsstr</td>";
      $data.="<td>$mv_cprsstr</td>";
      $data.="<td>$cfstbndl</td>";
      $data.="<td>$mv_cfstbndl</td>";
      $data.="<td>$cprsstp</td>";
      $data.="<td>$mv_cprsstp</td>";
      $data.="<td>$clstbndl</td>";
      $data.="<td>$mv_clstbndl</td>";
      $data.="<td>$fprd</td>";
      $data.="<td>$fedn</td>";
      $data.="<td>$fedc</td>";
      $data.="<td>$t_r</td>";
      $data.="<td>$tr_p</td>";
      
      $data.="</tr>";
      $i++;
        if ($day_num!=0) {
            $total_weekdays++;
      } elseif ($day_num==0) {
            $total_sun++;
      }
} //Closes the while loop
$data.="</table>
{DOWNLOADLINK}
</body>
</html>";

function get_time_diff($time1, $time2)
{
      $time_diff = strtotime($time2) - strtotime($time1);
    $hours = floor($time_diff / 3600);
    $minutes = floor(($time_diff % 3600) / 60);

    return(sprintf("%02d:%02d", $hours, $minutes));
}

//Following is the place to decide to output as HTML or create the file instead.
//If requested parameter has been sent by the link to this php pave
//then it will set the headers so it will send it as "file" as in my sample
if (isset($_GET['xldownload']))
{
  //Also checking xldownload parameter value to make sure it is set correctly
  //Not necessary but good habit to expect a known value instead only checking for the existence
  if (intval($_GET['xldownload'])==1) {
    //removing the {DOWNLOADLINK}, because this will not be shown in the HTML page
    $data = str_replace("{DOWNLOADLINK}","",$data);
    header('Content-type: application/vnd.ms-excel');
    header('Content-Disposition: attachment; filename=tabledata.xls');
  }
}
else
{
  //Sending output as HTML to the client browser
  //So we need to put a link for download
  //We use the same script name however with xldownload=1 value (check out the href property of the anchor
  $data = str_replace("{DOWNLOADLINK}","<a href=\"prod_mon_rpt_rstls.php?xldownload=1\">File</a>",$data);
}
  echo $data;
?>Start Free Trial
[+][-]09.08.2006 at 06:47PM PDT, ID: 17484080

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09.08.2006 at 11:55PM PDT, ID: 17484819

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: PHP Scripting Language
Tags: php, excel, table, export
Sign Up Now!
Solution Provided By: TeRReF
Participating Experts: 2
Solution Grade: A
 
 
[+][-]09.09.2006 at 12:32AM PDT, ID: 17484921

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.09.2006 at 11:10PM PDT, ID: 17488428

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.11.2006 at 07:31AM PDT, ID: 17494836

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09.11.2006 at 07:34AM PDT, ID: 17494865

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.11.2006 at 07:39AM PDT, ID: 17494912

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09.14.2006 at 02:39PM PDT, ID: 17524647

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.15.2006 at 09:11AM PDT, ID: 17530567

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09.15.2006 at 09:12AM PDT, ID: 17530574

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09.15.2006 at 11:55AM PDT, ID: 17531772

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32