数据值需要存储在CSV文件中

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

我不能将数据csv存储在文件中

我可以加载数据,但我不能将数据存储在CSV文件中并将文件存储在特定文件夹中我的要求是加载Web表单详细信息并将电子邮件发送给特定用户。我可以下载csv文件,但我无法将详细信息存储在CSV文件中

    function webform_email_menu() {
        $items = array();
         $items['webform_email'] = array(
            'title'             =>  'Webform Email',  //page title
            'description'       =>  'Webform Information',  //description show when mouse hover on link
            'page callback'     =>  'webform_list',  //callback function which is invoked when menu item is called.
            'access callback'   =>  true,  //any user can access this page
           );
         return $items;
    }



function webform_list() {
    module_load_include('inc', 'webform', 'includes/webform.export');
    module_load_include('inc', 'webform', 'includes/webform.components');
    module_load_include('inc', 'webform', 'includes/webform.submissions');  
    $submissions = webform_get_submissions(125);
echo "installation Type,Date of Purchase,Date of Install    ,Outdoor Unit Model,Outdoor Unit Serial,Indoor Unit 1 Model,Indoor Unit 1 Serial,Indoor Unit 2 Model,Indoor Unit 2 Serial,Indoor Unit 3 Model,Indoor Unit 3 Serial,Indoor Unit 4 Model,Indoor Unit 4 Serial,Indoor Unit 5 Model,Indoor Unit 5 Serial,Indoor Unit 6 Model,Indoor Unit 6 Serial,Indoor Unit 7 Model,Indoor Unit 7 Serial,Indoor Unit 8 Model,Indoor Unit 8 Serial,Indoor Unit 9 Model,Indoor Unit 9 Serial,Owner First Name,Owner Last Name,Installation Address 1,Installation Address 2,City,State/Province,Zip/Postal Code,Country ,Owner Phone Number,Owner Email,Contractor Information,Contractor Name,Contractor Address 1 ,Contractor Address 2,City,State/Province,Zip/Postal Code,Country,Contractor Phone Number,Contractor Email,Distributor Name,Distributor Address 1,Distributor Address 2,City,State/Province,Zip/Postal Code,Country,TAC,Privacy Statement\n";
   $surveycomponents = db_select('webform_component')
     ->fields('webform_component')
     ->condition('nid', 125)
     ->orderBy('weight')
     ->orderBy('name')
     ->execute()
     ->fetchAllAssoc('cid', PDO::FETCH_ASSOC);
$count =0;
$i=2;
   foreach ($submissions as &$submission) {
       $submission_value=$submission->data;
       $i=0;
       foreach($submission_value as $submissionvalue){
            $numItems = count($submission_value);
            if(++$i === $numItems) {
                echo $csv_export="\n";
            }else{
                echo $csv_export=$submissionvalue[0].',';
            }
       }
    }

        $file_open = fopen('sites/default/files/csv/csvfile.csv', "a");
        file_put_contents("sites/default/files/csv/csvfile.csv", $csv_export);
    $csv_handler = fopen ('sites/default/files/csv/csvfile.csv','w');
    fwrite ($csv_handler,$csv_export);
    fclose ($csv_handler);

}

我错了

php drupal drupal-7 drupal-modules
3个回答
0
投票

您可以使用这样的数组将数据存储在CSV文件中

$data = [[1,2,3][11,22,33]];
$fp = fopen('test.csv', 'w');

foreach ($data as $fields) {
    fputcsv($fp, $fields);
}
fclose($fp);

0
投票

检查文件夹权限。即sites / default / files / csv。它应该是可写的,以便将某些东西写入文件。


0
投票

我不确定你的预期,但是你需要使用drupal函数来获取使用的righet路径并准备它被修改:

$path = drupal_realpath('public://').DIRECTORY_SEPARATOR.'csv';
file_prepare_directory($path);
$file_open = fopen($path.'/csvfile.csv', "a");
file_put_contents($path.'/csvfile.csv', $csv_export);

[...]

https://api.drupal.org/api/drupal/includes%21file.inc/function/file_prepare_directory/7.x

https://api.drupal.org/api/drupal/includes%21file.inc/function/drupal_realpath/7.x

© www.soinside.com 2019 - 2024. All rights reserved.