添加另一个JOIN以查询和获取表列标题

时间:2015-05-26 14:46:42

标签: php mysql csv mysqli

我正在处理数据到CSV功能,但我已经卡住了。

到目前为止,它从两张表中获取所有数据,并为发票数据和客户数据的每个订单在一行中放入一个CSV文件。

我还需要通过ID获取invoice_items(我想这些将需要循环,因为可能有多个项目,但不确定如何将invoice_items添加到查询中,我还需要设置标题以及列但不确定如何)。

如果可能是这样的话会很棒:

  

|发票详细说明客户详细信息|发票项目

                               LIST ITEM

                               LIST ITEM

                               LIST ITEM

而不是

  

发票明细|客户详情|发票项目列出项目列表项目   列表项目

真实数据和标题看起来像

  

id |发票号码|金额|名字|地址|发票项目(和   这部分下面列出)

PHP

header("Content-type: text/csv"); 

    // output any connection error
    if ($mysqli->connect_error) {
        die('Error : ('.$mysqli->connect_errno .') '. $mysqli->connect_error);
    }

    $file_name = 'invoice-export-'.date('d-m-Y').'.csv';   // file name
    $file_path = 'downloads/'.$file_name; // file path

    $file = fopen($file_path, "w"); // open a file in write mode
    chmod($file_path, 0777);    // set the file permission

    $query_table_columns_data = "SELECT * 
                                    FROM invoices i
                                    JOIN customers c
                                    ON c.invoice = i.invoice
                                    WHERE i.invoice = c.invoice
                                    ORDER BY i.invoice";

    if ($result_column_data = mysqli_query($mysqli, $query_table_columns_data)) {

        // fetch table fields data
        while ($column_data = $result_column_data->fetch_row()) {
            $table_column_data = array();
            foreach($column_data as $data) {
                $table_column_data[] = $data;
            }

            // Format array as CSV and write to file pointer
            fputcsv($file, $table_column_data, ",", '"');
        }

    }

1 个答案:

答案 0 :(得分:1)

如果我理解正确,您需要加入另一张表以获取发票项目。

我还怀疑您当前的示例查询是否错误地加入了表,因为您似乎是根据发票加入客户和发票表。我预计客户可能会有很多发票,但发票只有一个客户,我希望发票表能够存储相关的客户ID,然后根据它进行连接。

因此,您可以通过以下方式获取详细信息: -

SELECT i.id,
    i.invoice_number,
    i.amount,
    c.name,
    c.address,
    ii.invoice_item
FROM invoices i
INNER JOIN customers c
ON c.id = i.customer_id
INNER JOIN invoice_items ii
ON ii.invoice_id = i.id
ORDER BY i.invoice

注意我已经从SELECT *切换到选择列名(我猜的是),因为SELECT *由于各种原因而不受欢迎。如果您想要更具可读性的东西,可以为列名添加别名。

但是你想要一个从列名中获取的标题列表。假设您不想只是硬编码,我建议您从使用mysqli_fetch_row切换到mysqli_fetch_assoc,这将返回列名。

然后,您可以处理第一行的列名称并输出它们。这样的事情: -

<?php
header("Content-type: text/csv"); 

    // output any connection error
    if ($mysqli->connect_error) {
        die('Error : ('.$mysqli->connect_errno .') '. $mysqli->connect_error);
    }

    $file_name = 'invoice-export-'.date('d-m-Y').'.csv';   // file name
    $file_path = 'downloads/'.$file_name; // file path

    $file = fopen($file_path, "w"); // open a file in write mode
    chmod($file_path, 0777);    // set the file permission

    $query_table_columns_data = "SELECT i.id,
                                    i.invoice_number,
                                    i.amount,
                                    c.name,
                                    c.address,
                                    ii.invoice_item
                                FROM invoices i
                                INNER JOIN customers c
                                ON c.id = i.customer_id
                                INNER JOIN invoice_items ii
                                ON ii.invoice_id = i.id
                                ORDER BY i.invoice";

    if ($result_column_data = mysqli_query($mysqli, $query_table_columns_data)) 
    {
        // fetch table fields data
        if ($column_data = $result_column_data->fetch_assoc()) 
        {
            $table_column_head = array();
            $table_column_data = array();
            foreach($column_data as $field_name->$data) 
            {
                $table_column_head[] = $field_name;
                $table_column_data[] = $data;
            }
            // Format array as CSV and write to file pointer
            fputcsv($file, $table_column_head, ",", '"');
            fputcsv($file, $table_column_data, ",", '"');
            while ($column_data = $result_column_data->fetch_assoc()) 
            {
                $table_column_data = array();
                foreach($column_data as $data) 
                {
                    $table_column_data[] = $data;
                }
                // Format array as CSV and write to file pointer
                fputcsv($file, $table_column_data, ",", '"');
            }
        }
    }

稍微扩展一下,只发出发票第一行的发票明细

<?php
header("Content-type: text/csv"); 

    // output any connection error
    if ($mysqli->connect_error) {
        die('Error : ('.$mysqli->connect_errno .') '. $mysqli->connect_error);
    }

    $file_name = 'invoice-export-'.date('d-m-Y').'.csv';   // file name
    $file_path = 'downloads/'.$file_name; // file path

    $file = fopen($file_path, "w"); // open a file in write mode
    chmod($file_path, 0777);    // set the file permission

    $query_table_columns_data = "SELECT i.id,
                                    i.invoice_number,
                                    i.amount,
                                    c.name,
                                    c.address,
                                    ii.invoice_item
                                FROM invoices i
                                INNER JOIN customers c
                                ON c.id = i.customer_id
                                INNER JOIN invoice_items ii
                                ON ii.invoice_id = i.id
                                ORDER BY i.invoice";

    if ($result_column_data = mysqli_query($mysqli, $query_table_columns_data)) 
    {
        // fetch table fields data
        if ($column_data = $result_column_data->fetch_assoc()) 
        {
            $table_column_head = array();
            $table_column_data = array();
            foreach($column_data as $field_name->$data) 
            {
                $table_column_head[] = $field_name;
                $table_column_data[] = $data;
            }
            $prev_id = $column_data['id'];
            $prev_invoice_number = $column_data['invoice_number'];
            $prev_amount = $column_data['amount'];
            $prev_name = $column_data['name'];
            $prev_address = $column_data['address'];

            // Format array as CSV and write to file pointer
            fputcsv($file, $table_column_head, ",", '"');
            fputcsv($file, $table_column_data, ",", '"');
            while ($column_data = $result_column_data->fetch_assoc()) 
            {
                if ($prev_id == $column_data['id'] AND $prev_invoice_number == $column_data['invoice_number'] AND $prev_amount == $column_data['amount'] AND $prev_name == $column_data['name'] AND $prev_address == $column_data['address'])
                {
                    $different_invoice = true;
                }
                else
                {
                    $different_invoice = false;
                    $prev_id = $column_data['id'];
                    $prev_invoice_number = $column_data['invoice_number'];
                    $prev_amount = $column_data['amount'];
                    $prev_name = $column_data['name'];
                    $prev_address = $column_data['address'];
                }
                $table_column_data = array();
                foreach($column_data as $field_name->$data) 
                {
                    $table_column_data[] = (($different_invoice or $field_name == 'invoice_item') ? $data : '');
                }
                // Format array as CSV and write to file pointer
                fputcsv($file, $table_column_data, ",", '"');
            }
        }
    }