Jump to content
Sign in to follow this  
nickngqs

Export CSV for a page

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? 

Share this post


Link to post
Share on other sites

I didn't understood everything but you can take a look at this thread :

 

 

and this module if it help :

 

 

  • Like 2

Share this post


Link to post
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.

 

.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Mithlesh
      Changing it to null
    • By EyeDentify
      I have been experimenting with the new $page->meta() method and find it useful.

      Once i figured out that the data i "save" with it is tied to the page where i called the method from.

      So this is not obvious at least not for me in the documentation:
      https://processwire.com/api/ref/page/meta/
       
      So i just wanted to share that revelation with the community so you don´t get as confused as i was.

      Happy Coding Everyone.
    • By stanoliver
      In the new page-meta-method (https://processwire.com/blog/posts/pw-3.0.133/#comments) there is an example how to ouput "world". 
      Could may someone give me a snippet of code so that the output is one of the colors (red, green, blue).
    • By Tyssen
      I have a page that contains a single ProFields table field and I want to display the contents of the table on the front end and then for logged in users, they can edit certain columns in the table.
      What I have at the moment is
      $out = '<form action="'.$page->url.'" method="post" > <table class="table"> <tbody>'; $count = 1; foreach($page->fieldName as $row) : $out .= ' <tr> <td><input type="checkbox" name="fieldName_'.$count.'_columnName"></td> </tr>'; if($input->post->submit) : $page->of(false); $page->set('fieldName_'.$count.'_columnName', $sanitizer->text($input->post->{fieldName_'.$count.'_columnName})); $page->save(); endif; $count++; endforeach; $out .= ' </tbody> </table> <button class="button" type="submit">Save</button> </form>'; The two problems I have are:
      I get an error trying from $sanitizer->text($input->post->{fieldName_'.$count.'_columnName}), not sure how to make that dynamic.  If I change the above to just a static value, e.g. $page->set('fieldName_1_columnName', 'Testing') and save the form, it's not saving the values to the database. Where am I going wrong?
    • By VeiJari
      Hello forum. I'm trying to figure out how to detect if current form has errors after saving it in pw admin page. This is so that our event location isn't added in database if we detect errors. But I can't seem to find a correct way to detect errors? I only found the wire()->errors() and it always triggers even if I don't have any errors. 
      How can I detect errors in a form?
      By error I mean $page->error('this is the red error');
×
×
  • Create New...