How to Export data from 3 tables in one .csv file php How to Export data from 3 tables in one .csv file php wordpress wordpress

How to Export data from 3 tables in one .csv file php


Well, your problem is that you can't retrieve all the data at the same time in an only MySQL query, as they are not related data. Your problem is just the output, so, you only will have to relate the 3 set of results in an only array. To do that:

  • Execute the three querys, and save them in three unrelated arrays.

  • Relate them with a key you'll share with all of them.

  • Loop over all the arrays assigning values to a main "output" one.

With that, you'll have the array which you can output to get the CSV you want. For the sake of the example, and due I can't write a valid code with your vars and queries, I wrote the following example. It has the 3 different arrays you'll have to get from your database with mock data, but you can grab the idea. Just copy and paste and you'll have the live example:

<?php$playlists  = array(    array(        'id'    => 1    ,   'data'  => 'playlist1'    ),   array(        'id'    => 2    ,   'data'  => 'playlist2'    ),   array(        'id'    => 3    ,   'data'  => 'playlist3'    ));$songs  = array(    array(        'id'    => 1    ,   'data'  => 'song1'    ),   array(        'id'    => 2    ,   'data'  => 'song2'    ),   array(        'id'    => 3    ,   'data'  => 'song3'    ),   array(        'id'    => 4    ,   'data'  => 'song4'    ),   array(        'id'    => 5    ,   'data'  => 'song5'    ));$rates  = array(    array(        'id'    => 1    ,   'data'  => 'rating1'    ),   array(        'id'    => 2    ,   'data'  => 'rating2'    ),   array(        'id'    => 3    ,   'data'  => 'rating3'    ),   array(        'id'    => 4    ,   'data'  => 'rating4'    ),   array(        'id'    => 5    ,   'data'  => 'rating5'    ),   array(        'id'    => 6    ,   'data'  => 'rating6'    ));// Count all the arrays and get the bigger:$num        = 0;$play_num   = count( $playlists );$num        = ($play_num > $num) ? $play_num : $num;$song_num   = count( $songs );$num        = ($song_num > $num) ? $song_num : $num;$rate_num   = count( $rates );$num        = ($rate_num > $num) ? $rate_num : $num;$output = array();for ( $i = 0; $i<=$num; $i++ ) {    $output[]   = array(        'id_playlist'   => !empty( $playlists[$i]['id'] )   ? $playlists[$i]['id']   : ''    ,   'data_playlist' => !empty( $playlists[$i]['data'] ) ? $playlists[$i]['data'] : ''    ,   'id_song'       => !empty( $songs[$i]['id'] )       ? $songs[$i]['id']       : ''    ,   'data_song'     => !empty( $songs[$i]['data'] )     ? $songs[$i]['data']     : ''    ,   'id_rate'       => !empty( $rates[$i]['id'] )       ? $rates[$i]['id']       : ''    ,   'data_rate'     => !empty( $rates[$i]['data'] )     ? $rates[$i]['data']     : ''    );}foreach ( $output as $out ) {    echo implode( ' - ', $out);    echo '<br>';}

Output:

1 - playlist1 - 1 - song1 - 1 - rating12 - playlist2 - 2 - song2 - 2 - rating23 - playlist3 - 3 - song3 - 3 - rating3  -           - 4 - song4 - 4 - rating4  -           - 5 - song5 - 5 - rating5  -           -   -       - 6 - rating6