簡體   English   中英

如何從 MySQL 數據庫中的表的列中導出 a.txt 文件?

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

我想逐行導出.txt格式的電話號碼。 表名是customers ,列名是customer_contact_numbers來自數據庫saiautocare我使用的是 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>

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