如何在csv_from_result()方法codeigniter中传递muitiple $ query变量

时间:2015-08-12 09:45:07

标签: php codeigniter

我有以下查询:

$query1 = $this->db->query(" SELECT * FROM `student_registration` "); 

$query2 = $this->db->query(" SELECT * FROM `club_registration` "); 

这里我想传递codeigniter的csv_from_result()中的两个查询数据,我正在使用:

$this->load->dbutil();
$delimiter = ",";
$newline = "\r\n";
$backup=$this->dbutil->csv_from_result($query, $delimiter, $newline);
$db_name = 'Students'.'_'. date('Y-m-d') .'.csv';
$this->load->helper('download');
force_download($db_name, $backup);

但在上面csv_from_result()方法中如何传递$query1$query2变量?

1 个答案:

答案 0 :(得分:0)

这是您正在使用的实用程序功能的库代码。 您无法将多个查询传递给此函数。 $ query参数是这里的查询结果对象。

function csv_from_result($query, $delim = ",", $newline = "\n", $enclosure = '"')
{
    if ( ! is_object($query) OR ! method_exists($query, 'list_fields'))
    {
        show_error('You must submit a valid result object');
    }

    $out = '';

    // First generate the headings from the table column names
    foreach ($query->list_fields() as $name)
    {
        $out .= $enclosure.str_replace($enclosure, $enclosure.$enclosure, $name).$enclosure.$delim;
    }

    $out = rtrim($out);
    $out .= $newline;

    // Next blast through the result array and build out the rows
    foreach ($query->result_array() as $row)
    {
        foreach ($row as $item)
        {
            $out .= $enclosure.str_replace($enclosure, $enclosure.$enclosure, $item).$enclosure.$delim;
        }
        $out = rtrim($out);
        $out .= $newline;
    }

    return $out;
}

正确使用此功能将是 -

$query = $this->db->query("SELECT * FROM mytable");
echo $this->dbutil->csv_from_result($query);

参考 - https://ellislab.com/codeigniter/user-guide/database/utilities.html