We help IT Professionals succeed at work.

PHP Output to CSV from serialized data

416 Views
Last Modified: 2019-01-31
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.
Comment
Watch Question

Scott FellDeveloper
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2013

Commented:
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

Author

Commented:
what would the some.php look like when it queries a table that is serialized and outputs a CSV file?
Scott FellDeveloper
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2013

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

Author

Commented:
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
Developer
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2013
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
CERTIFIED EXPERT
Most Valuable Expert 2018
Distinguished Expert 2019

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

Author

Commented:
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";}
CERTIFIED EXPERT
Most Valuable Expert 2018
Distinguished Expert 2019

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