title: export student result in php to excel
body: <p>I made a Student Result page. I wanted to export the results to excel. For that I made excel.php. When I click on the export button it is showing that there is no results. But results are showing in the webpage. Please find the attached screenshot of results and help me to solve the issue.</p>
  
  <p><img src="https://i.stack.imgur.com/W8Wnr.jpg" alt="Screenshot of Results in webpage"></p>
  
  <p>rsltmng.php</p>
  
  <pre><code>&lt;?php
  error_reporting(0);
  session_start();
  include_once '../oesdb.php';
  /************************** Step 1 *************************/
  if(!isset($_SESSION['admname'])) {
      $_GLOBALS['message']="Session Timeout.Click here to &lt;a href=\"index.php\"&gt;Re-LogIn&lt;/a&gt;";
  }
  else if(isset($_REQUEST['logout'])) {
      /************************** Step 2 - Case 1 *************************/
      //Log out and redirect login page
          unset($_SESSION['admname']);
          header('Location: index.php');
  
      }
      else if(isset($_REQUEST['dashboard'])) {
      /************************** Step 2 - Case 2 *************************/
          //redirect to dashboard
              header('Location: admwelcome.php');
  
          }
          else if(isset($_REQUEST['back'])) {
      /************************** Step 2 - Case 3s *************************/
              //redirect to Result Management
                  header('Location: rsltmng.php');    
              }    
  ?&gt;  
  
  
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
  &lt;link rel="stylesheet" type="text/css" href="css/style.css"/&gt;
  &lt;title&gt;Manage Results - OES&lt;/title&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
  &lt;div id="logmsk" style="display: block;"&gt;
  &lt;?php include("header.php"); ?&gt;
  &lt;div id="page3"&gt;
  &lt;form name="rsltmng" action="rsltmng.php" method="post"&gt;
  
  &lt;div id="navigationbar"&gt;
  &lt;?php if(isset($_SESSION['admname'])) {
                          // Navigations
  
                              ?&gt;
                          &lt;li&gt;&lt;input type="submit" value="Logout" name="logout" class="aclass2" title="Log Out"/&gt;&lt;/li&gt;
                              &lt;?php  if(isset($_REQUEST['testid'])) { ?&gt;
                          &lt;li&gt;&lt;input type="submit" value="Back" name="back" class="aclass2" title="Manage Results"/&gt;&lt;/li&gt;
                              &lt;?php }else { ?&gt;
                          &lt;li&gt;&lt;input type="submit" value="DashBoard" name="dashboard" class="aclass3" title="Dash Board"/&gt;&lt;/li&gt;
                              &lt;?php } ?&gt;
  &lt;/div&gt;
  &lt;div&gt;&lt;p style="font-weight:bold; color:#F00; text-align:center"&gt;&lt;?php if($_GLOBALS['message']){ echo ($_GLOBALS['message']);}?&gt;&lt;/p&gt;&lt;/div&gt;
  
  &lt;?php
                          if(isset($_REQUEST['testid'])) {
   /************************** Step 3 - Case 1 *************************/
   // Defualt Mode: Displays the Detailed Test Results.
                              $result=executeQuery("select t.testname,DATE_FORMAT(t.testfrom,'%d %M %Y') as fromdate,DATE_FORMAT(t.testto,'%d %M %Y %H:%i:%S') as todate,sub.subname,IFNULL((select sum(marks) from question where testid=".$_REQUEST['testid']."),0) as maxmarks from test as t, subject as sub where sub.subid=t.subid and t.testid=".$_REQUEST['testid'].";") ;
                              if(mysql_num_rows($result)!=0) {
  
                                  $r=mysql_fetch_array($result);
                                  ?&gt;
                      &lt;table cellpadding="5" cellspacing="10" border="0" align="center" style="background:#ffffff url(../images/page.gif);text-align:left;line-height:20px;"&gt;
                          &lt;tr&gt;
                              &lt;td colspan="2"&gt;&lt;h3 style="color:#0000cc;text-align:center;"&gt;&lt;u&gt;Test Summary&lt;/u&gt;&lt;/h3&gt;&lt;/td&gt;
                          &lt;/tr&gt;
                         &lt;!-- &lt;tr&gt;
                              &lt;td colspan="2" &gt;&lt;hr style="color:#ff0000;border-width:4px;"/&gt;&lt;/td&gt;
                          &lt;/tr&gt;--&gt;
                          &lt;tr&gt;
                              &lt;td&gt;Test Name:&lt;/td&gt;
                              &lt;td&gt;&lt;b&gt;&lt;?php echo htmlspecialchars_decode($r['testname'],ENT_QUOTES); ?&gt;&lt;/b&gt;&lt;/td&gt;
                          &lt;/tr&gt;
                          &lt;tr&gt;
                              &lt;td&gt;Subject Name:&lt;/td&gt;
                              &lt;td&gt;&lt;b&gt;&lt;?php echo htmlspecialchars_decode($r['subname'],ENT_QUOTES); ?&gt;&lt;/b&gt;&lt;/td&gt;
                          &lt;/tr&gt;
                          &lt;tr&gt;
                              &lt;td&gt;Validity:&lt;/td&gt;
                              &lt;td&gt;&lt;b&gt;&lt;?php echo $r['fromdate']." To ".$r['todate']; ?&gt;&lt;/b&gt;&lt;/td&gt;
                          &lt;/tr&gt;
                          &lt;tr&gt;
                              &lt;td&gt;Max. Marks:&lt;/td&gt;
                              &lt;td&gt;&lt;b&gt;&lt;?php echo $r['maxmarks']; ?&gt;&lt;/b&gt;&lt;/td&gt;
                          &lt;/tr&gt;
                          &lt;tr&gt;&lt;td colspan="2"&gt;&lt;hr style="color:#ff0000;border-width:2px;"/&gt;&lt;/td&gt;&lt;/tr&gt;
                          &lt;!--&lt;tr&gt;
                              &lt;td colspan="2"&gt;&lt;h3 style="color:#0000cc;text-align:center;"&gt;Attempted Students&lt;/h3&gt;&lt;/td&gt;
                          &lt;/tr&gt;
                          &lt;tr&gt;
                              &lt;td colspan="2" &gt;&lt;hr style="color:#ff0000;border-width:4px;"/&gt;&lt;/td&gt;
                          &lt;/tr&gt;--&gt;
  
                      &lt;/table&gt;
                      &lt;table&gt;&lt;tr&gt;&lt;td&gt;&lt;h3 style="color:#0000cc;text-align:center;"&gt;&lt;u&gt;Attempted Students&lt;/u&gt;&lt;/h3&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;                 
  
                                  &lt;?php
                                  $result1=executeQuery("select s.fname,s.contactno,s.emailid,IFNULL((select sum(q.marks) as om from studentquestion as sq, question as q where sq.testid=q.testid and sq.qnid=q.qnid and sq.answered='answered' and sq.stdanswer=q.correctanswer and sq.stdid=st.stdid and sq.testid=".$_REQUEST['testid']." order by sq.testid),0) as om from studenttest as st, student as s where s.stdid=st.stdid and st.testid=".$_REQUEST['testid']."  order by om DESC;" );
  
  
                                  if(mysql_num_rows($result1)==0) {
                                      echo"&lt;h3 style=\"color:#0000cc;text-align:center;\"&gt;No Students Yet Attempted this Test!&lt;/h3&gt;";
                                  }
                                  else {
                                      ?&gt;
  
                      &lt;table cellpadding="5" cellspacing="10" class="datatable"&gt;
                          &lt;tr&gt;
                              &lt;th&gt;Student Name&lt;/th&gt;
                              &lt;th&gt;Contact Number&lt;/th&gt;
                              &lt;th&gt;Email-ID&lt;/th&gt;
                              &lt;th&gt;Obtained Marks&lt;/th&gt;
                              &lt;th&gt;Result(%)&lt;/th&gt;
  
                          &lt;/tr&gt;
                                          &lt;?php
                                          while($r1=mysql_fetch_array($result1)) {
  
                                              ?&gt;
                          &lt;tr&gt;
                              &lt;td&gt;&lt;?php echo htmlspecialchars_decode($r1['fname'],ENT_QUOTES); ?&gt;&lt;/td&gt;
                              &lt;td&gt;&lt;?php echo htmlspecialchars_decode($r1['contactno'],ENT_QUOTES); ?&gt;&lt;/td&gt;
                              &lt;td&gt;&lt;?php echo htmlspecialchars_decode($r1['emailid'],ENT_QUOTES); ?&gt;&lt;/td&gt;
                              &lt;td&gt;&lt;?php echo $r1['om']; ?&gt;&lt;/td&gt;
                              &lt;td&gt;&lt;?php echo ($r1['om']/$r['maxmarks']*100)." %"; ?&gt;&lt;/td&gt;
  
  
                          &lt;/tr&gt;
  
                          &lt;form method="post" action="excel.php"&gt;&lt;input type="submit" name="export_excel" class="aclass3" value="Export Result" /&gt;&lt;/form&gt;
                                          &lt;?php
  
                                          }    
                                      }
                                  }
                                  else {
                                      echo"&lt;h3 style=\"color:#0000cc;text-align:center;\"&gt;Something went wrong. Please logout and Try again.&lt;/h3&gt;";
                                  }
                                  ?&gt;
                      &lt;/table&gt;       
                          &lt;?php    
                          }
                          else {
  
                          /************************** Step 3 - Case 2 *************************/
                          // Defualt Mode: Displays the Test Results.
                              $result=executeQuery("select t.testid,t.testname,DATE_FORMAT(t.testfrom,'%d %M %Y') as fromdate,DATE_FORMAT(t.testto,'%d %M %Y %H:%i:%S') as todate,sub.subname,(select count(stdid) from studenttest where testid=t.testid) as attemptedstudents from test as t, subject as sub where sub.subid=t.subid;");
                              if(mysql_num_rows($result)==0) {
                                  echo "&lt;h3 style=\"color:#0000cc;text-align:center;\"&gt;No Tests Yet...!&lt;/h3&gt;";
                              }
                              else {
                                  $i=0;
  
                                  ?&gt;
                      &lt;table cellpadding="10" cellspacing="10" class="datatable"&gt;
                          &lt;tr&gt;
                              &lt;th&gt;Test Name&lt;/th&gt;
                              &lt;th&gt;Validity&lt;/th&gt;
                              &lt;th&gt;Subject Name&lt;/th&gt;
                              &lt;th&gt;Attempted Students&lt;/th&gt;
                              &lt;th&gt;Details&lt;/th&gt;
                          &lt;/tr&gt;
              &lt;?php
                                      while($r=mysql_fetch_array($result)) {
                                          $i=$i+1;
                                          if($i%2==0) {
                                              echo "&lt;tr class=\"alt\"&gt;";
                                          }
                                          else { echo "&lt;tr&gt;";}
                                          echo "&lt;td&gt;".htmlspecialchars_decode($r['testname'],ENT_QUOTES)."&lt;/td&gt;&lt;td&gt;".$r['fromdate']." To ".$r['todate']." PM &lt;/td&gt;"
                                              ."&lt;td&gt;".htmlspecialchars_decode($r['subname'],ENT_QUOTES)."&lt;/td&gt;&lt;td&gt;".$r['attemptedstudents']."&lt;/td&gt;"
                                              ."&lt;td class=\"tddata\"&gt;&lt;a title=\"Details\" href=\"rsltmng.php?testid=".$r['testid']."\"&gt;&lt;img src=\"../images/detail.png\" height=\"30\" width=\"40\" alt=\"Details\" /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;";
                                      }
                                      ?&gt;
                      &lt;/table&gt;
          &lt;?php
                              }
                          }
                          closedb();
                      }
  
                      ?&gt;
  
  
  &lt;/form&gt;
  &lt;/div&gt;
  &lt;div  style="position: relative; clear:both; z-index:10; height:3em; margin-top:-3em"&gt;&lt;?php include("footer.php"); ?&gt;&lt;/div&gt;
  &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>excel.php</p>
  
  <pre><code>&lt;?php
  
  $connect = mysqli_connect("localhost", "root", "", "ssmc_ae_drsunny");
  $output = '';
  $_REQUEST['testid']=isset($_REQUEST['testid']);
  
  if(isset($_POST["export_excel"]))
  {
  
  $sql=executeQuery("select s.fname,s.contactno,s.emailid,IFNULL((select sum(q.marks) as om from studentquestion as sq, question as q where sq.testid=q.testid and sq.qnid=q.qnid and sq.answered='answered' and sq.stdanswer=q.correctanswer and sq.stdid=st.stdid and sq.testid=".$_REQUEST['testid']." order by sq.testid),0) as om from studenttest as st, student as s where s.stdid=st.stdid and st.testid=".$_REQUEST['testid']."  order by om DESC;" );   
      $result=mysqli_query($connect, $sql);
      if(mysqli_num_rows($result)&gt;0) 
      {
          $output .= '
          &lt;table cellpadding="5" cellspacing="10" class="datatable"&gt;
                          &lt;tr&gt;
                              &lt;th&gt;Student Name&lt;/th&gt;
                              &lt;th&gt;Contact Number&lt;/th&gt;
                              &lt;th&gt;Email-ID&lt;/th&gt;
                              &lt;th&gt;Obtained Marks&lt;/th&gt;
                              &lt;th&gt;Result(%)&lt;/th&gt;
  
                          &lt;/tr&gt;
                          ';
                          while ($row=mysqli_fetch_array($result))
                          {
                          $output .= '
                          &lt;tr&gt;
                              &lt;td&gt;'.$row['fname'].'&lt;/td&gt;
                              &lt;td&gt;'.$row['contactno'].'&lt;/td&gt;
                              &lt;td&gt;'.$row['emailid'].'&lt;/td&gt;
                              &lt;td&gt;'.$row['om'].'&lt;/td&gt;
                              &lt;td&gt;'.$row['om'].'/'.$row['maxmarks'].'*100."%"&lt;/td&gt;
                          &lt;/tr&gt;
                          ';  
                          }
                          $output .= '&lt;/table&gt;';
                          header("Content-Type: application/xls");
                          header("Content-Disposition: attachment; filename=download.xls");
                          echo $output;       
      }
      else
      {echo "NO RESULT"; }
  }
  ?&gt;
  </code></pre>