Avatar of Robert Granlund
Robert Granlund
Flag for United States of America asked on

PHP Output to CSV from serialized data

I have a database that has serialized data in it.  I need to export it to a CSV via a one click button.  Where do I start?  I'm not really sure where to start to ask the right question.
DatabasesPHPAJAXMicrosoft ExcelMicrosoft Office

Avatar of undefined
Last Comment
Chris Stanyon

8/22/2022 - Mon
Scott Fell

Based on your questions topics, should we assume you want to have a button that you can click.
<button class="download_button">Download</button>

Open in new window

Then use JS/Ajax to detect the click
$(function(){
   $('.download_button').on('click',function(){
        // Do Something 
    });
});

Open in new window


and access a php page that outputs the data
https://api.jquery.com/jQuery.ajax/
$(function(){
   $('.download_button').on('click',function(){
       $.ajax({
            method: "POST",
             url: "some.php",
             data: { name: "John", location: "Boston" }
             })
             .done(function( msg ) {
             alert( "Data Saved: " + msg );
  });
    });
});

Open in new window

On your remote php page you can use http://php.net/manual/en/function.fputcsv.php
Robert Granlund

ASKER
what would the some.php look like when it queries a table that is serialized and outputs a CSV file?
Scott Fell

That depends on what you mean by serialized.   If the data in a field looks something like "foo:bar,bat:baz" you can explode the text string http://php.net/manual/en/function.explode.php to create an array then use http://php.net/manual/en/function.fputcsv.php to output your csv.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Robert Granlund

ASKER
How would I un-serialize and run a for-each for this table that has one field that is serialized?  I think if I can un-serialize the one field and then run a for each statement, I believe I can output to a CSV.
query.JPG
ASKER CERTIFIED SOLUTION
Scott Fell

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Chris Stanyon

In PHP, you use the serialize() and unserialize() functions. So something like this:

$data = 'a:2:{s:9:"firstname";s:5:"chris";s:8:"lastname";s:7:"stanyon";}';

$newData = unserialize($data);

Open in new window

That will convert your serialized string back into a PHP variable that you can use. In the example above it will give you an array which you can use to write out your CSV. Depending on your data, you may also get back an object, a string, a bool etc.
Robert Granlund

ASKER
I need to just grab the ii_email:

Array ( [data] => a:86:{s:9:"ii_seller";s:0:"";s:2:"id";s:3:"117";s:12:"ii_firstname";s:13:"Evander Fogle";s:10:"ii_address";s:59:" 5671 STREETAtlanta, GA 30342";s:8:"ii_email";s:26:"MY-EMAIL.COM";s:8:"ii_phone";s:12:"404-444-4444";s:10:"ii_pi_name";a:1:{i:0;s:0:"";}s:11:"ii_pi_phone";a:1:{i:0;s:0:"";}s:11:"ii_pi_email";a:1:{i:0;s:0:"";}s:11:"ii_pi_name2";s:0:"";s:12:"ii_pi_phone2";s:0:"";s:12:"ii_pi_email2";s:0:"";s:11:"ii_pi_name3";s:0:"";s:12:"ii_pi_phone3";s:0:"";s:12:"ii_pi_email3";s:0:"";s:15:"oi_hospitallist";a:4:{i:0;s:38:"Total Joint Replacement Prehab / Rehab";i:1;s:30:"Sports Medicine Prehab / Rehab";i:3;s:38:"Non-Surgical Orthopedic Prehab / Rehab";i:5;s:5:"Other";}
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Chris Stanyon

Just posted a response to your other question specifically regarding grabbing a part of the array