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.
LVL 8
rgranlundAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
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
rgranlundAuthor Commented:
what would the some.php look like when it queries a table that is serialized and outputs a CSV file?
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
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.
Price Your IT Services for Profit

Managed service contracts are great - when they're making you money. Yes, you’re getting paid monthly, but is it actually profitable? Learn to calculate your hourly overhead burden so you can master your IT services pricing strategy.

rgranlundAuthor 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
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
<?php

$data = 'a:1:"text1";b:2:"text2";c:3:"text3"';
$outer = explode(";",$data);
//print_r($outer);
foreach($outer as $inner){
    $x = explode(":",$inner);
    print_r($x);
    echo "<br>";
}

?>

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Chris StanyonWebDevCommented:
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.
rgranlundAuthor 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";}
Chris StanyonWebDevCommented:
Just posted a response to your other question specifically regarding grabbing a part of the array
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Databases

From novice to tech pro — start learning today.