Link to home
Get AccessLog in
Avatar of Robert Granlund
Robert GranlundFlag 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.
Avatar of Scott Fell
Scott Fell
Flag of United States of America image

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
Avatar of Robert Granlund

ASKER

what would the some.php look like when it queries a table that is serialized and outputs a CSV file?
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.
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
Avatar of Scott Fell
Scott Fell
Flag of United States of America image

Link to home
membership
This content is only available to members.
To access this content, you must be a member of Experts Exchange.
Get Access
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.
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";}
Just posted a response to your other question specifically regarding grabbing a part of the array