2017-03-24 2 views
0

dbからcsv形式でファイルを展開するときに、各名前の後にカンマが得られます。スクリーンショットをチェックしてください:how the csv file looksJSON_EXTRACTからJSON_EXTRACTのカンマをCSV形式で取り除きます。

は、これはコードです:json_extractは、DB内にあるすべてのユーザーを取っているが、それらが表示されていない、昏睡状態で区切ってちょうど空のスロットと同じよう

function expired(){ 
    if(isset($_POST['download'])) 
     { 

      $db =& JFactory::getDBO(); 
      $time = (time()); 
      $query = "select user_id, profile_value FROM #__user_metadata WHERE JSON_EXTRACT(profile_value, '$.expiry_date') > '$time'"; 
      $db->setQuery($query); 

      $rows = $db->loadObjectList(); 

       header('Content-type: text/csv'); 
       ob_end_clean(); // is cleaning the space before the list 

       header('Content-Disposition: attachment; filename=UserDatabase.csv'); 
       ob_start(); 
       $output = fopen("php://output", "w"); 

      foreach ($rows as $row) { 

       $user_id = json_decode($row->user_id); 

       $db =& JFactory::getDBO(); 
       $query = "select user_id, profile_value FROM #__user_metadata WHERE user_id = '$user_id'" ; 
       $db->setQuery($query); 

       $rows = $db->loadObjectList(); 


       foreach ($rows as $row) { 

       $profile_value = json_decode($row->profile_value); 


       $first_name=$profile_value->first_name; 

       $last_name=$profile_value->last_name; 
       $email =$profile_value->email; 

       $users = array($first_name . " " . $last_name, $email); 
       fputcsv($output, $users, ',') ; 

        } 

     } 
     fclose($output); 
      ob_end_flush(); 
      exit(0); 

} 

} 

に見えます問題はその行から来ていません:

fputcsv($output, $users, ',') ; 

答えて

0

私はそれらを取り除くためにすべてのコードを変更する必要がありました。これは解決策です: 多分あなたのために何か役に立つでしょう。問題は、変数$ユーザーの中で私はダブル配列を持っていたということでした。

$db =& JFactory::getDBO(); 
    $app = JFactory::getApplication(); // 
    $user_type= $app->input->getString('user_type'); 


     switch($user_type){ 

     case 'Export expired danish users': 

     $query = "select user_id, profile_value FROM #__user_metadata WHERE profile_key = 'profile20.subscription_shared' AND JSON_EXTRACT(profile_value, '$.expiry_date') > '$time'"; 
       $rows = $db->loadObjectList(); 




     break; 

     case 'foreign': 
      $query = "select user_id, profile_value FROM #__user_metadata WHERE profile_key = 'profile20.subscription_shared' AND JSON_EXTRACT(profile_value, '$.expiry_date') > '$time'"; 
       $rows = $db->loadObjectList(); 
       break; 
      default: 

      $db =& JFactory::getDBO(); 
      $query = "select user_id, profile_value FROM #__user_metadata WHERE profile_key = 'profile20.subscription_shared' AND JSON_EXTRACT(profile_value, '$.expiry_date') > '$time'"; 
      $db->setQuery($query); 

      break; 

    } 

     header('Content-type: text/csv'); 
     ob_end_clean(); // is cleaning the space before the list 

     header('Content-Disposition: attachment; filename=UsersWithExpiredDate.csv'); 
     ob_start(); 
     $output = fopen("php://output", "w"); 

     foreach ($rows as $row) { 

      $user_id = $row->user_id; 

      $query = "SELECT JSON_unquote(JSON_EXTRACT(profile_value, '$.email')) AS email, JSON_unquote(JSON_EXTRACT(profile_value, '$.first_name')) AS first_name, JSON_unquote(JSON_EXTRACT(profile_value, '$.last_name')) AS last_name FROM nagwm_user_metadata WHERE user_id = '$user_id ' AND profile_key = 'profile20.profile_data'"; 

      $db->setQuery($query); 

      $rows = $db->loadRow(); 

      $first_name=$rows[1]; 


      $last_name=$rows[2]; 
      $email =$rows[0]; 

      $users = array($first_name . " " . $last_name, $email); 
      fputcsv($output, $users) ; 

     } 
      fclose($output); 
      ob_end_flush(); 
      exit(0); 
関連する問題