简体   繁体   English

如何从 MySQL 数据库中的表的列中导出 a.txt 文件?

[英]How to export a .txt file from a column of a table in MySQL database?

I want to export the phone numbers in.txt format line by line.我想逐行导出.txt格式的电话号码。 Table name is customers and the column name is customer_contact_numbers from the database saiautocare I am using the Laravel framework, JQuery and MySQL database.表名是customers ,列名是customer_contact_numbers来自数据库saiautocare我使用的是 Laravel 框架、JQuery 和 MySQL 数据库。

在此处输入图像描述

When I click that button it will ask me where to save the file and export the numbers line by line into the text file.当我单击该按钮时,它会询问我将文件保存在哪里并将数字逐行导出到文本文件中。

Here's the code:这是代码:

      <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>

I'm new to PHP and MySQL, that's why I'm sorry if I am not able to provide all the required information.我是 PHP 和 MySQL 的新手,如果我无法提供所有必需的信息,我很抱歉。

$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)
];);

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

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