导出学生导致php excel

时间:2016-10-10 06:11:10

标签: php html excel web export

我做了一个学生成绩页面。我想将结果导出为ex​​cel。为此,我制作了excel.php。当我点击导出按钮时,它显示没有结果。但结果显示在网页上。请找到附带的结果截图,并帮助我解决问题。

Screenshot of Results in webpage

rsltmng.php

<?php
error_reporting(0);
session_start();
include_once '../oesdb.php';
/************************** Step 1 *************************/
if(!isset($_SESSION['admname'])) {
    $_GLOBALS['message']="Session Timeout.Click here to <a href=\"index.php\">Re-LogIn</a>";
}
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');    
            }    
?>  


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="css/style.css"/>
<title>Manage Results - OES</title>
</head>

<body>
<div id="logmsk" style="display: block;">
<?php include("header.php"); ?>
<div id="page3">
<form name="rsltmng" action="rsltmng.php" method="post">

<div id="navigationbar">
<?php if(isset($_SESSION['admname'])) {
                        // Navigations

                            ?>
                        <li><input type="submit" value="Logout" name="logout" class="aclass2" title="Log Out"/></li>
                            <?php  if(isset($_REQUEST['testid'])) { ?>
                        <li><input type="submit" value="Back" name="back" class="aclass2" title="Manage Results"/></li>
                            <?php }else { ?>
                        <li><input type="submit" value="DashBoard" name="dashboard" class="aclass3" title="Dash Board"/></li>
                            <?php } ?>
</div>
<div><p style="font-weight:bold; color:#F00; text-align:center"><?php if($_GLOBALS['message']){ echo ($_GLOBALS['message']);}?></p></div>

<?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);
                                ?>
                    <table cellpadding="5" cellspacing="10" border="0" align="center" style="background:#ffffff url(../images/page.gif);text-align:left;line-height:20px;">
                        <tr>
                            <td colspan="2"><h3 style="color:#0000cc;text-align:center;"><u>Test Summary</u></h3></td>
                        </tr>
                       <!-- <tr>
                            <td colspan="2" ><hr style="color:#ff0000;border-width:4px;"/></td>
                        </tr>-->
                        <tr>
                            <td>Test Name:</td>
                            <td><b><?php echo htmlspecialchars_decode($r['testname'],ENT_QUOTES); ?></b></td>
                        </tr>
                        <tr>
                            <td>Subject Name:</td>
                            <td><b><?php echo htmlspecialchars_decode($r['subname'],ENT_QUOTES); ?></b></td>
                        </tr>
                        <tr>
                            <td>Validity:</td>
                            <td><b><?php echo $r['fromdate']." To ".$r['todate']; ?></b></td>
                        </tr>
                        <tr>
                            <td>Max. Marks:</td>
                            <td><b><?php echo $r['maxmarks']; ?></b></td>
                        </tr>
                        <tr><td colspan="2"><hr style="color:#ff0000;border-width:2px;"/></td></tr>
                        <!--<tr>
                            <td colspan="2"><h3 style="color:#0000cc;text-align:center;">Attempted Students</h3></td>
                        </tr>
                        <tr>
                            <td colspan="2" ><hr style="color:#ff0000;border-width:4px;"/></td>
                        </tr>-->

                    </table>
                    <table><tr><td><h3 style="color:#0000cc;text-align:center;"><u>Attempted Students</u></h3></td></tr></table>                 

                                <?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"<h3 style=\"color:#0000cc;text-align:center;\">No Students Yet Attempted this Test!</h3>";
                                }
                                else {
                                    ?>

                    <table cellpadding="5" cellspacing="10" class="datatable">
                        <tr>
                            <th>Student Name</th>
                            <th>Contact Number</th>
                            <th>Email-ID</th>
                            <th>Obtained Marks</th>
                            <th>Result(%)</th>

                        </tr>
                                        <?php
                                        while($r1=mysql_fetch_array($result1)) {

                                            ?>
                        <tr>
                            <td><?php echo htmlspecialchars_decode($r1['fname'],ENT_QUOTES); ?></td>
                            <td><?php echo htmlspecialchars_decode($r1['contactno'],ENT_QUOTES); ?></td>
                            <td><?php echo htmlspecialchars_decode($r1['emailid'],ENT_QUOTES); ?></td>
                            <td><?php echo $r1['om']; ?></td>
                            <td><?php echo ($r1['om']/$r['maxmarks']*100)." %"; ?></td>


                        </tr>

                        <form method="post" action="excel.php"><input type="submit" name="export_excel" class="aclass3" value="Export Result" /></form>
                                        <?php

                                        }    
                                    }
                                }
                                else {
                                    echo"<h3 style=\"color:#0000cc;text-align:center;\">Something went wrong. Please logout and Try again.</h3>";
                                }
                                ?>
                    </table>       
                        <?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 "<h3 style=\"color:#0000cc;text-align:center;\">No Tests Yet...!</h3>";
                            }
                            else {
                                $i=0;

                                ?>
                    <table cellpadding="10" cellspacing="10" class="datatable">
                        <tr>
                            <th>Test Name</th>
                            <th>Validity</th>
                            <th>Subject Name</th>
                            <th>Attempted Students</th>
                            <th>Details</th>
                        </tr>
            <?php
                                    while($r=mysql_fetch_array($result)) {
                                        $i=$i+1;
                                        if($i%2==0) {
                                            echo "<tr class=\"alt\">";
                                        }
                                        else { echo "<tr>";}
                                        echo "<td>".htmlspecialchars_decode($r['testname'],ENT_QUOTES)."</td><td>".$r['fromdate']." To ".$r['todate']." PM </td>"
                                            ."<td>".htmlspecialchars_decode($r['subname'],ENT_QUOTES)."</td><td>".$r['attemptedstudents']."</td>"
                                            ."<td class=\"tddata\"><a title=\"Details\" href=\"rsltmng.php?testid=".$r['testid']."\"><img src=\"../images/detail.png\" height=\"30\" width=\"40\" alt=\"Details\" /></a></td></tr>";
                                    }
                                    ?>
                    </table>
        <?php
                            }
                        }
                        closedb();
                    }

                    ?>


</form>
</div>
<div  style="position: relative; clear:both; z-index:10; height:3em; margin-top:-3em"><?php include("footer.php"); ?></div>
</div>
</body>
</html>

excel.php

<?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)>0) 
    {
        $output .= '
        <table cellpadding="5" cellspacing="10" class="datatable">
                        <tr>
                            <th>Student Name</th>
                            <th>Contact Number</th>
                            <th>Email-ID</th>
                            <th>Obtained Marks</th>
                            <th>Result(%)</th>

                        </tr>
                        ';
                        while ($row=mysqli_fetch_array($result))
                        {
                        $output .= '
                        <tr>
                            <td>'.$row['fname'].'</td>
                            <td>'.$row['contactno'].'</td>
                            <td>'.$row['emailid'].'</td>
                            <td>'.$row['om'].'</td>
                            <td>'.$row['om'].'/'.$row['maxmarks'].'*100."%"</td>
                        </tr>
                        ';  
                        }
                        $output .= '</table>';
                        header("Content-Type: application/xls");
                        header("Content-Disposition: attachment; filename=download.xls");
                        echo $output;       
    }
    else
    {echo "NO RESULT"; }
}
?>

1 个答案:

答案 0 :(得分:0)

None

这一行实际上使$_REQUEST['testid']=isset($_REQUEST['testid']); 变量为1或0。

因此查询被执行但没有显示结果,因为表中没有这样的行。