Php 如何从MySQL数据库中的表列导出.txt文件?
我想逐行导出.txt格式的电话号码。表名为Php 如何从MySQL数据库中的表列导出.txt文件?,php,jquery,mysql,database,laravel,Php,Jquery,Mysql,Database,Laravel,我想逐行导出.txt格式的电话号码。表名为customers,列名为customer\u contact\u number来自数据库saiautcare我使用的是Laravel框架、JQuery和MySQL数据库 当我点击该按钮时,它会询问我保存文件的位置,并将数字逐行导出到文本文件中 代码如下: <div class="col-sm-6 col-lg-3"> <div class="card text-white bg-primary">
customers
,列名为customer\u contact\u number
来自数据库saiautcare
我使用的是Laravel框架、JQuery和MySQL数据库
当我点击该按钮时,它会询问我保存文件的位置,并将数字逐行导出到文本文件中
代码如下:
<div class="col-sm-6 col-lg-3">
<div class="card text-white bg-primary">
<div class="card-body pb-0">
<div class="btn-group float-right">
<button type="button" class="btn btn-transparent dropdown-toggle p-0" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<i class="icon-settings"></i>
</button>
<div class="dropdown-menu dropdown-menu-right">
<a class="dropdown-item" href="#">Export to TXT</a>
</div>
</div>
<h4 class="mb-0">{{ $TotalCustomers }}</h4>
<p>Marketing Numbers</p>
</div>
<div class="chart-wrapper px-3" style="height:70px;">
<canvas id="card-chart1" class="chart" height="70"></canvas>
</div>
</div>
</div>
{{$TotalCustomers}}
营销数字
我不熟悉PHP和MySQL,因此,如果我无法提供所有必需的信息,我很抱歉。还有一个问题,我是否应该添加一个新的PHP文件并将其添加到href“#”中。然后将代码粘贴到新创建的PHP文件中。我有没有办法不用创建新页面就可以做到这一点?你只需要将此代码放到你的控制器上,设置一些路由,然后通过此路由进入href。在用户点击它之后,它会调用你的控制器方法并开始下载一个txt文件。如果你能详细说明,我将非常感谢兄弟。我曾经使用过这个GitHub项目:我是PHP新手,如果它打扰了您,我很抱歉:)
$customers = Customer::all();
$phoneNumbers = "Phone numbers \n";
foreach ($customers as $customer) {
$content .= $customer->customer_contact_numbers;
$content .= "\n";
}
// file name to download
$fileName = "contact_numbers.txt";
// make a response, with the content, a 200 response code and the headers
return Response::make($content, 200, [
'Content-type' => 'text/plain',
'Content-Disposition' => sprintf('attachment; filename="%s"', $fileName),
'Content-Length' => sizeof($content)
];);