我知道如何将MySQL TABLE导出为Excel。但我的问题是我有一个复杂的场景,我可以从多个MySQL表中获取数据并将它们排列到HTML表输出中。我的问题是:如何将此数据输出作为Excel?
这是我的代码
<?php
include(".../dbconnect.txt");
$result = mysql_query("select * from main_table where jobid='".$_GET['jobid']."'") or die(mysql_error());
?>
<table width='1450'>
<tr>
<td>S/N</td>
<td>Job Title</td>
<td>Applicant</td>
<td>Date</td>
<td>Expe</td>
<td>Course</td>
<td>Email</td>
<td>Quali</td>
<td>Age</td>
<td>Gender</td>
</tr>
<?php
$cnt =1;
while($row = mysql_fetch_array($result)){
$count = $cnt; $cnt++;
?>
<tr>
<td>
<?php echo $count; ?>
</td>
<td>
<?php
$re = mysql_query("select * from table1 where id='".$_GET['jobid']."'") or die(mysql_error());
$rw = mysql_fetch_array($re);
echo $rw['title'];
?>
</td>
<td>
<?php
$re1 = mysql_query("select * from table2 where email='".$row['email']."'") or die(mysql_error());
$rw1 = mysql_fetch_array($re1);
echo ucwords($rw1['fname']);
echo " ";
echo ucwords($rw1['lname']);
?>
</td>
<td><?php echo $row['date']; ?></td>
<td>
<?php
$re2 = mysql_query("select * from table3 where email='".$row['email']."'") or die(mysql_error());
$rw2 = mysql_fetch_array($re2);
echo $rw2['years'];
?>
</td>
<td>
<?php
$re3 = mysql_query("select * from table4 where email='".$row['email']."'") or die(mysql_error());
$rw3 = mysql_fetch_array($re3);
echo ucwords($rw3['course']);
?>
</td>
<td>
<?php echo $row['email']; ?>
</td>
<td>
<?php
echo $rw3['quali'];
?>
</td>
<td>
<?php echo $rw1['age']; ?>
</td>
<td>
<?php echo $rw1['gender']; ?>
</td>
</tr>
<?php } ?>
</table>
HTML结果如下:
S/N Job Title Applicant Date Expe Course Email Quali Age Gender
1 Program Officer - Clinical Services Username Surname Wed 31, Jul, 2013 1 Agricultural Engineering useremail@yahoo.com BEng 24 Male
2 Program Officer - Clinical Services Username Surname Wed 31, Jul, 2013 3 Political Science useremail@gmail.com BSc 33 Male
3 Program Officer - Clinical Services Username Surname Wed 31, Jul, 2013 1 Microbiology useremail@yahoo.com HND 25 Female
如何在Excel中输出此表?
答案 0 :(得分:0)
这是一个如何将所有查询转换为可以传递给html或excel的漂亮数组的示例。一段时间后我感到无聊所以我只输了几个查询。但是你应该可以从这里开始。
$rows = mysql_query("select * from main_table where jobid='".$_GET['jobid']."'") or die(mysql_error());
$items = array();
while($row = mysql_fetch_array($result))
{
$item = array();
// Master stuff
$item['date'] = $row['date'];
// Title
$re = mysql_query("select * from table1 where id='".$_GET['jobid']."'") or die(mysql_error());
$rw = mysql_fetch_array($re);
$item['title'] = $rw['title'];
// Names
$re1 = mysql_query("select * from table2 where email='".$row['email']."'") or die(mysql_error());
$rw1 = mysql_fetch_array($re1);
$item['name'] = ucwords($rw1['fname']) . ' ' . ucwords($rw1['lname']);
// ... rest of queries
// Add to list
$items[] = $item;
}
我建议对sql左连接操作稍微熟悉一下。您可以大大减少查询次数。甚至可能只有一个。