Jump to content

Export CSV for a page


nickngqs
 Share

Recommended Posts

Does anyone know how to do an export of a page entries to CSV using the Processwire way?

I'm trying to setup a list where the client can see a list of user's form values.

Example ->

A front end form -> Upon submit -> Adds to database -> Shows on the admin of Processwire.

From there, I can see the entries and sort stuff. And lastly I can export them in CSV.

I'm looking at the Processwire cheatsheet and see the $files, is this the one I should be looking at? 

Link to comment
Share on other sites

I use something called SimpleExcel to export / import CSV.

http://faisalman.github.io/simple-excel-php/

 

Here's an example code snippet for Export :

$AllPages is just the data source, so you can replace it with your own.
I would also ignore all the col_subcat fields and any associated functions like GetSubCategoryNames.

You can replace 'SSS-Exported' at the bottom to the filename of your choosing.

    public function ProcessXML() {

        $AllPages =  Data::PagesAllContentsNoInteraktives();

        if ($AllPages->count()==0)
            exit;

        $_colTitle  = 0; //A
        $_colCategory=1; //B
        $_colSubcat1 =2; //C
        $_colSubcat2 =3; //D
        $_colSubcat3 =4; //E
        $_colSubcat4 =5; //F
        $_colSubcat5 =6; //G
        $_colContent = 7; //H
        $_colIntro   = 8; //I
        $_colAuthor  = 9;//J
        $_colImage   = 10;//K
        $_colAlt     = 11;//L
        $_colTitleNav= 12;//M
        $_colSEOTitle= 13;//N
        $_colKeywords= 14;//O
        $_colSEOImage= 16;//Q
        $_colMeta    = 15;//P
        $_colURL     = 17;//R
        $_colURLnew  = 18;//S
        $_colCanonical=19;//T
        $_colParent  = 20;//U
        $_colAudio   = 21;//V
        $_colVideo   = 22;//W
        $worksheet = array();

        array_push($worksheet,
        array(
            'Title',
            'Category',
            'Sub-Category I',
            'Sub-Category II',
            'Sub-Category III',
            'Sub-Category IV',
            'Sub-Category V',
            'Content',
            'Intro',
            'Author',
            'Image',
            'Alt',
            'Title for Navigation',
            'SEO Title',
            'Keywords',
            'Meta Description',
            'SEO Image',
            'URL',
            'URL New',
            'Canonical',
            'Parent',
            'Audio',
            'Video URL',
            'ID'
            )
        );

        foreach ($AllPages as $page) {
            try {

                $subcats = GetSubCategoryNames($page);

                $subcat1='';
                $subcat2='';
                $subcat3='';
                $subcat4='';
                $subcat5='';

                try {
                    $subcat1 = $subcats[0];
                    $subcat2 = $subcats[1];
                    $subcat3 = $subcats[2];
                    $subcat4 = $subcats[3];
                    $subcat5 = $subcats[4];
                }
                catch (\Exception $e)
                {}

                array_push($worksheet, array(
                    $page->title,
                    GetCategoryName($page),
                    $subcat1,
                    $subcat2,
                    $subcat3,
                    $subcat4,
                    $subcat5,
                    $page->content,
                    $page->content_intro,
                    $page->author,
                    $page->image,
                    $page->image_alt,
                    $page->title_nav,
                    $page->seo_title,
                    $page->seo_keywords,
                    $page->seo_description,
                    $page->seo_image,
                    $page->url,
                    $page->url,
                    '',
                    $page->seo_canonical,
                    $page->audio,
                    $page->video_url,
                    $page->id
                ));
            }
            catch (\Exception $e) {
                WriteLog($e->getMessage() . " " . "{$page->title}" . " has errors");
            }
        }

        try {
            $excel = new SimpleExcel('xml');

            $excel->writer->setData($worksheet);
            $excel->writer->saveFile('SSS-Exported');
        } catch (\Exception $e) {
            WriteLog($e->getMessage());
        }


    }


 

P/S In case you need to switch from CSV to XML, this post could be of interest.

 

.

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...