简体   繁体   English

将MYSQL结果导出到CSV

[英]Export MYSQL Result to CSV

I've been building a database to calculate termination fees. 我一直在建立一个数据库来计算终止费。

The below code (termination.php) takes multiple rows selected from check boxes on a previous page, processes the query, and outputs in a loop to a simple HTML table. 下面的代码(termination.php)接受从上一页上的复选框中选择的多行,处理查询,并循环输出到简单的HTML表。 This is working as required. 这是按要求工作的。

<?php
require_once '.\Includes\dbcon.php';

$rowCount = count($_POST["select"]);

for ($i = 0; $i < $rowCount; $i++)
    {
    $sql = "UPDATE `{$_POST['customer']}` SET `DateOfCancellation`='{$_POST['dateofcancellation']} WHERE ServiceID={$_POST['select'][$i]}'";
    if ($con->query($sql) === TRUE)
        {
        echo "";
        }
      else
        {
        echo "Error: " . $sql . "<br />" . $con->error;
        }
    }

?>
<a href = ".\index.php">Back to Index</a></p>
<strong>Termination for <?php echo ucwords($_POST['customer']) ?> Based on Cancellation Date <?php echo $_POST['dateofcancellation'] ?></strong></p>
<table>
    <tr>
        <th>Service Name</th>
        <th>License Start Date</th>
        <th>License End Date</th>
        <th>Cost Per Calendar Month</th>
        <th>Balance on Termination</th>
<?php
$rowCount = count($_POST["select"]);

for ($i = 0; $i < $rowCount; $i++)
    {
    $sql = mysqli_query($con, "$select FROM `{$_POST['customer']}` WHERE ServiceID={$_POST['select'][$i]}");
    $row[$i] = mysqli_fetch_array($sql); ?>
<tr>
<td><?php
    echo $row[$i]['ServiceName']; ?></td>
<td><?php
    echo $row[$i]['LicenseStart']; ?></td>
<td><?php
    echo $row[$i]['LicenseEND']; ?></td>
<td>£<?php
    echo $row[$i]['CostPCM']; ?></td>
<td>£<?php
    echo $row[$i]['CostOfTermination']; ?></td>
<?php
    }

$sql = "UPDATE `{$_POST['customer']}` SET `DateOfCancellation`='0000-00-00'";

if ($con->query($sql) === TRUE)
    {
    echo "";
    }
  else
    {
    echo "Error: " . $sql . "<br />" . $con->error;
    }

$sum = 0;

for ($i = 0; $i < $rowCount; $i++)
    {
    $sum = $sum + $row[$i]['CostOfTermination'];
    }

echo "<strong>The Total Balance of Termination is: </strong>£" . $sum;
echo "<p>";
$sum = 0;

for ($i = 0; $i < $rowCount; $i++)
    {
    $sum = $sum + $row[$i]['CostPCM'];
    }

echo "<strong>Balance of Termination per Month: </strong>£" . $sum;
echo "<p>";
?>
</tr>

What do I need to do to export the data in the table to an Excel file (ideally) or a CSV file. 我需要做什么才能将表中的数据导出到Excel文件(理想情况下)或CSV文件。 I've tried a few different methods and can get the headers but not the actual data. 我尝试了几种不同的方法,但可以获取标头,但不能获取实际数据。 I think the for loop is what's throwing me off. 我认为for循环是让我失望的原因。

Try something like this: 尝试这样的事情:

<?php

$titles = "Service Name,License Start Date,License End Date,Cost Per Calendar Month,Balance on Termination";

$rowCount = count($_POST["select"]);
echo $titles;

for ($i = 0; $i < $rowCount; $i++)
    {
    $sql = mysqli_query($con, "$select FROM `{$_POST['customer']}` WHERE ServiceID={$_POST['select'][$i]}");

    $row[$i] = mysqli_fetch_array($sql); 

    echo 
    $row[$i]['ServiceName']  . "," . 
    $row[$i]['LicenseStart'] . "," .
    $row[$i]['LicenseEND']   . "," .
    $row[$i]['CostPCM']      . "," . 
    $row[$i]['CostOfTermination'] ."<br>"; 

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM