export PHP to excel file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
newbieken
Forum Newbie
Posts: 1
Joined: Sun Oct 31, 2010 4:17 am

export PHP to excel file

Post by newbieken »

Hi,
I have a page where i can export i to excel file. I can export it out but somehow the file extension is worng it gives me a webpage extension instead of xls.
Here is the code
<?php
//post from attendance
$courseID = $_POST['courseID'];
$classID = $_POST['classID'];
$venueID = $_POST['venueID'];

//query for name, nric, telp
$link = mysqli_connect('localhost', 'root', '', 'sjas') or die(mysqli_connect_error());
$query = "SELECT * FROM attendance a, member m WHERE Class_idClass = '".$classID."' AND Venue_idVenue = '".$venueID."' AND a.Member_idMember = m.idMember";
$result = mysqli_query($link, $query);
while($row=mysqli_fetch_array($result))
{
$store[]=$row;
}

//query for course name
$query2 = "SELECT * FROM course WHERE idCourse = '".$courseID."'";
$result2 = mysqli_query($link, $query2);
while($row2=mysqli_fetch_array($result2))
{
$courseName = $row2['name'];
$dayDuration = $row2['day_duration'];
}

//query for date
$query3 = "SELECT * FROM class c, time_slot ts WHERE idClass = '".$classID."'";
$result3 = mysqli_query($link, $query3);
$row3=mysqli_fetch_assoc($result3);

for($k=1; $k<13; $k++)
{
if($k<10)
{
if($row3['day0'.$k] != null)
{
$totalDuration+=1;
}

}
else
{
if($row3['day'.$k] != null)
{
$totalDuration+=1;
}
}
}

for ($t=1; $t<=$totalDuration; $t++)
{
if ($t=1)
{
$day = date("l", strtotime($row3['day01']));
$day1 = date("d M Y", strtotime($row3['day01']));
$examDate = date("d M Y", strtotime($row3['exam_date']));
break;
}
}

//query for time
$query4 = "SELECT * FROM class c, time_slot ts WHERE idclass = '".$classID."' AND c.Time_Slot_idTime_Slot = ts.idTime_slot";
$result4 = mysqli_query($link, $query4);
while($row4=mysqli_fetch_array($result4))
{
$startTime = $row4['start_time'];
}

//query for lecturer
$query5 = "SELECT * FROM lecturer_expertise le, lecturer l WHERE Course_idCourse = '".$courseID."' AND l.idLecturer = le.Lecturer_idLecturer";
$result5 = mysqli_query($link, $query5);
while($row5=mysqli_fetch_array($result5))
{
$lecturerName = $row5['name'];
}
$filename = $courseName."_".$day1." (".$courseID.", ".$classID.", ".$venueID.", ".date('d M Y').")";
header("Content-disposition: xls" . date("d M Y") . ".xls");
header("Content-Disposition: attachment; filename=".$filename.".xls");
header("Content-Type: application/vnd.ms-excel");
header("Content-Transfer-Encoding: binary");
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>SJAS</title>
</head>
<body>

<h1>ATTENDANCE REGISTER - CLASSROOM TRAINING</h1>

<form id="form" name="form" enctype='multipart/form-data' method='post'>
<table cellpadding="10" cellspacing="20">
<tr></tr>
<tr></tr>
<tr>
<td>NAME OF TRAINING PROVIDER : </td>
<td>ST.JOHN AMBULANCE SINGAPORE</td>
</tr>

<tr></tr>
<tr>
<td>COURSE TITLE : </td>
<td><?php echo $courseName ?></td>
<td></td>
<td>CLASS REGN. NUMBER :</td>
<td><?php echo $classID ?></td>
<td></td>
</tr>

<tr></tr>
<tr>
<td>COMMENCEMENT DATE : </td>
<td><?php echo $day1 ?></td>
<td></td>
<td>COMPLETION DATE :</td>
<td>
<?php
for ($t=1; $t<=$totalDuration; $t++)
{
if($totalDuration<10)
{
$lastDay = date("d M Y", strtotime($row3['day0'.$totalDuration]));
echo $lastDay;
break;
}
else
{
$lastDay = date("d M Y", strtotime($row3['day'.$totalDuration]));
echo $lastDay;
break;
}
}
?>
</td>
</tr>

<tr></tr>
<tr>
<td>DAYS AND TIME : </td>
<td><?php echo $day?>, <?php echo $startTime?></td>
<td></td>
<td>EXAMINATION DATE :</td>
<td><?php echo $examDate ?></td>
</tr>
<tr></tr>
<table border="1" cellpadding="5" cellspacing="0">
<tr>
<th rowspan="2"> S/N</th>
<th rowspan="2"> Name of trainee</th>
<th rowspan="2"> NRIC no.</th>
<th rowspan="2"> Name of company</th>
<th rowspan="2"> Telephone no.</th>
<th rowspan="2"> Invoice/Receipt</th>
<?php
for ($t=1; $t<=$totalDuration; $t++)
{
if($totalDuration<10)
{

$day = date("d M Y", strtotime($row3['day0'.$t]));
echo "<th colspan='2'>Date : ".$day."</th>";
}
else
{

$day = date("d M Y", strtotime($row3['day'.$t]));
echo "<th colspan='2'>Date : ".$day."</th>";
}
}
?>
<th rowspan="2"> Exam Result</th>
<th rowspan="2"> Remarks</th>
</tr>
<tr>
<?php
for ($t=1; $t<=$totalDuration; $t++)
{
?>
anyone can help me??
Post Reply