Jump to content

Export users to cvs


modifiedcontent
 Share

Recommended Posts

I am trying to rewrite Soma's suggestion here to export fields from the users database to a cvs file, with variations like this:

$array = $users->explode(function($item){ ...etc.

And

$array = $users->find('start=0')->explode(function($item){ ...

But keep getting 'Call to a member function ... on a non-object' errors. I guess $users is the non-object?

I am trying to use this within a function, that is called with a button click. Is $users not available within a function?

I see there are new export/import features coming up - great! - but it is all about $pages. Should $users work the exact same way?

This works as demo starting point. Now I just have to replace the $list with something from $users:

Spoiler

<?php
function export() {

$list = array(
'Peter,Griffin,Oslo,Norway',
'Glenn,Quagmire,Oslo,Norway',
);

$file = fopen('process/test.csv','w');

foreach ($list as $line) {
	fputcsv($file,explode(',',$line));
}

fclose($file);
}

if ( isset($_POST[ 'runexport' ] ) ) {
export();
}
?>

<form method=post>
<input name=runexport type=submit value='export members' >
</form>

 

 

Link to comment
Share on other sites

Thanks Alxndre'

This seems to work:

Spoiler

<?php

function export() {

$members = wire('users')->find('start=0');
$rows = array();

foreach ($members as $u){
    $rows[] = array(
        'name' => $u->name,
        'firstname' => $u->firstname
    );
}

$file = fopen('process/test.csv','w');
foreach ($rows as $fields) fputcsv($file, $fields);
fclose($file);
}

if ( isset($_POST[ 'runexport' ] ) ) {
export();
}
?>

<form method=post>
<input name=runexport type=submit value='export members' >
</form>

 

If anyone can spot mistakes or knows a better way, let me know.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...