Export Member Data Script
Posted: 26 August 2008 12:42 PM   [ Ignore ]  
Lab Assistant
Avatar
RankRank
Total Posts:  155
Joined  11-19-2007

EDIT: this script has moved

The old script below still works but has since been improved.

An interface has been added. The latest script can be found on the wiki:
Export Member Data


//// old script ////

<?php
/*
**    This script will export member data to a
**    comma or tab delimited text file.
**    
**    usage: edit $member_data and $custom_field_ids
**    with the fields to be exported
**    
**    original script by Ronny
**    modified by bkuberek
**    
*/

global $IN, $DB;

$member_data = array('member_id','username','screen_name','email'); // array('member_id','email')
$custom_field_ids = array(1,2,3,4);     // array() | array(1,2,3,4)
$delimiter = 'tab';                     // default delimiter : comma | tab
$data = '';

/*
** look for parameters
*/

if ($IN->GBL('delimiter') == 'comma' || $IN->GBL('delimiter') == 'tab')
{
    $delimiter
= $IN->GBL('delimiter');
}

switch ($delimiter)
{
    
case 'tab': $delimiter = "\t";
        break;
    default:
$delimiter = ",";
        break;
}

/*
** get custom fields' label
*/

$query = $DB->query("SELECT m_field_id, m_field_label FROM exp_member_fields");

foreach (
$query->result as $row)
{
    $custom_fields[$row[
'm_field_id']] = $row['m_field_label'];
}

/*
** execute query
*/

$DB->fetch_fields = TRUE;

$sql = "SELECT ";

if (
is_array($member_data) && count($member_data) > 0)
{
    
foreach ($member_data as $k => $v)
    
{
        $member_data[$k]
= "a." . $v;
    
}
    $sql
.= implode(', ',$member_data);
}
else
{
    $sql
.= "a.member_id, a.username, a.screen_name, a.email";
}

if (is_array($custom_field_ids) && count($custom_field_ids) > 0)
{
    
foreach ($custom_field_ids as $id)
    
{
        $sql
.= ", b.m_field_id_" . $id;
    
}
}

$sql
.= " FROM exp_members as a, exp_member_data as b";
$sql .= " WHERE a.member_id = b.member_id ORDER BY a.member_id";

$query = $DB->query($sql);

/*
** create column headers
*/

$field_names = $DB->field_names;
$count = count($field_names);

foreach(
$field_names as $k => $v)
{
    
if( preg_match("/(m_field_id_)(\d+)/", $v, $matches) )
    
{
        $field_names[$k]
= $custom_fields[$matches[2]];
    
}
}

$header
= '';

for (
$i = 0; $i < $count; $i++)
{
    $header
.= '"' . ucwords( str_replace('_',' ',$field_names[$i]) ) . '"' . $delimiter;
}

$header
= rtrim($header, $delimiter);

/*
** create data
*/

foreach ($query->result as $row)
{
    $line
= '';
    
    foreach (
$row as $value)
    
{
        
if(empty($value))
        
{
            $value
= '""' . $delimiter;
        
}
        
else
        
{
            $value
= '"' . $value . '"' . $delimiter;
        
}
        $line
.= $value;
    
}

    $line
= rtrim($line, $delimiter);
    
$data .= $line . "\r\n";
}

/*
** output HTTP Headers
*/

header("Content-type: application/x-download");
header("Content-disposition: attachment; filename=\"Export_Member_Data-".date('YmdHi', time()).".txt\"\r\n");
header("Content-transfer-encoding: binary\n");
header("Content-Type: application/csv-tab-delimited-table");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Pragma: public");

/*
** output file contents
*/

echo $header."\r\n".$data;

?>

 Signature 

http://www.bbook.com

Profile
 
 
Posted: 26 August 2008 01:03 PM   [ Ignore ]   [ # 1 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  155
Joined  11-19-2007

In the above script I left out part of the original script. I am not sure how it works and I didn’t rewrite it. If someone knows how to implement it to the above version please post it below.

here is the part I left out:

if ( isset&($_GET['createcsv']) AND $_GET['createcsv']=="true") {

    header
("Pragma: public"); // required
    
header("Expires: 0");
    
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    
header("Cache-Control: public");
    
header("Content-type: application/txt");
    
header("Content-Disposition: attachment; filename=$file;");
    
header("Content-Transfer-Encoding: binary");

    while&(
$rs=mysql_fetch_assoc&($res)){
        
echo implode(" ; ",$rs)."\n";
    
}

    
exit;

}

Thanks

 Signature 

http://www.bbook.com

Profile
 
 
Posted: 27 August 2008 01:07 PM   [ Ignore ]   [ # 2 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  247
Joined  05-24-2002

Oh wow! I’m glad you could rewrite the code with EE classes. I just don’t know enough about php to do this myself. Php is still on my to-do list.

Therefor I have no idea what that piece of code did. All I did was grab all kinds of code I found on the interwebs and somehow magically had a file that worked. Probably if you didn’t notice anything wrong with it, it can be deleted..

 Signature 

You know me better than that, love. I don’t “do” anything. Things just happen.

Profile
 
 
Posted: 29 August 2008 01:01 AM   [ Ignore ]   [ # 3 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  155
Joined  11-19-2007

Hey check it out:

Export Member Data v.0.1

Now the script has an interface. wink

 Signature 

http://www.bbook.com

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 1149, on July 16, 2007 09:33 AM
Total Registered Members: 65085 Total Logged-in Users: 37
Total Topics: 82223 Total Anonymous Users: 20
Total Replies: 441915 Total Guests: 222
Total Posts: 524138    
Members ( View Memberlist )
Newest Members:  BombermanhaimtuagocsadamVeNeaDoRHildegaardhrtrulzUNFORGIVEN IIIEmmanuelYanYanTomsB