-1

我知道如何将 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 "&nbsp;";
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 中输出此表?

4

1 回答 1

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 left join 操作有点熟悉。您可以显着减少查询次数。甚至可能下降到一个。

于 2013-08-18T21:24:15.083 回答