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

问题描述 投票:0回答:1

我想逐行导出.txt格式的电话号码。来自数据库customers的表名是customer_contact_numbers,列名是saiautocare我正在使用Laravel框架,JQuery和MySQL数据库。

enter image description here

当我单击该按钮时,它将询问我将文件保存在哪里并将数字逐行导出到文本文件中。

这里是代码:

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

我是PHP和MySQL的新手,这就是为什么如果无法提供所有必需的信息,我感到抱歉。

php jquery mysql database laravel
1个回答
0
投票
$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)
];);
© www.soinside.com 2019 - 2024. All rights reserved.