从csv格式的JSON_提取中删除逗号,php
当我以csv格式从数据库中提取文件时,在每个名称之后我都会得到逗号,我不知道它们在哪里。查看屏幕截图: 代码如下:从csv格式的JSON_提取中删除逗号,php,php,json,csv,Php,Json,Csv,当我以csv格式从数据库中提取文件时,在每个名称之后我都会得到逗号,我不知道它们在哪里。查看屏幕截图: 代码如下: function expired(){ if(isset($_POST['download'])) { $db =& JFactory::getDBO(); $time = (time()); $query = "select user_id, profile_value FROM #__
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);
}
}
看起来json_extract占用了数据库中的所有用户,但没有显示这些用户,只是一个由coma分隔的空插槽,问题不在这一行:
fputcsv($output, $users, ',') ;
我必须改变所有的密码才能把它们取下来。这就是解决方案: 也许你会找到一些有用的东西。问题是,在变量$users中,i a有两个数组
$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);