【问题标题】:How to export a .txt file from a column of a table in MySQL database?如何从 MySQL 数据库中的表的列中导出 .txt 文件?
【发布时间】:2023-03-17 09:05:02
【问题描述】:

我想逐行导出 .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 的新手,如果我不能提供所有必需的信息,我很抱歉。

【问题讨论】:

    标签: php jquery mysql database laravel


    【解决方案1】:
    $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)
    ];);
    

    【讨论】:

    • 还有一个问题,我是否应该添加一个新的 PHP 文件并将其添加到 href "#" 中。然后将该代码粘贴到新创建的 PHP 文件中。有什么方法可以在不创建新页面的情况下做到这一点?
    • 您只需将此代码放入您的控制器并设置一些路由,然后通过此路由进入href。用户点击后它会调用你的控制器方法并开始下载一个txt文件
    • 如果您能详细说明,我将非常感谢兄弟。我已经使用了这个 GitHub 项目:github.com/AshutoshChoubey/SaiAutoCare 我是 PHP 新手,如果打扰到您,请见谅 :)
    猜你喜欢
    • 1970-01-01
    • 2013-08-05
    • 1970-01-01
    • 2012-10-23
    • 2018-05-07
    • 2012-11-22
    • 2017-10-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多