Output SQL query as an array

I am retrieving records from a tables in a WordPress database using the following….

global $wpdb;
echo '<table>';
$sellers = get_users('blog_id=1&orderby=nicename&role=sellers');
foreach ($sellers as $seller) 
    {
    echo '<tr>';
    echo '<td>' . $seller->user_login . '</td>';

    $count1 = $wpdb->get_results("SELECT COUNT(*) AS count FROM wp_mymeta2 a JOIN wp_mymeta1 b ON b.id = a.my_id WHERE a.value = '$seller->user_login' AND b.date_posted LIKE '%2014-04-19%'" );
    echo '<td>' . $count1[0]->count . '</td>'; 

    $count2 = $wpdb->get_results("SELECT COUNT(*) AS count FROM wp_mymeta2 a JOIN wp_mymeta1 b ON b.id = a.my_id WHERE a.value = '$seller->user_login' AND b.date_posted LIKE '%$2014-04-18%'" );
    echo '<td>' . $count2[0]->count . '</td>';

    $count3 = $wpdb->get_results("SELECT COUNT(*) AS count FROM wp_mymeta2 a JOIN wp_mymeta1 b ON b.id = a.my_id WHERE a.value = '$seller->user_login' AND b.date_posted LIKE '%2014-05-17%'" );
    echo '<td>' . $count3[0]->count . '</td>'; 

    echo '</tr>';
    }
echo '</table>';

This works great and is returning the data I want and echoing it into a table.

Read More

I now want to have the data placed into an array instead so I end up with…

var $example_data = array(
            array(
                'seller'    => '$seller',
            'date'      => '$date1',
                'count'     => '$count_result',
            ),
        array(
                'seller'    => '$seller',
            'date'      => '$date2',
                'count'     => '$count_result',
            ),
        array(
                'seller'    => '$seller',
            'date'      => '$date3',
                'count'     => '$count_result',
            ),
        );

Does anyone have an example they can point me in the right direction of something similar being achieved?

Related posts

Leave a Reply

1 comment

  1. You could use array_push function inside your foreach loop

    global $wpdb;
    echo '<table>';
    $sellers = get_users('blog_id=1&orderby=nicename&role=sellers');
    $arrSellers = array();
    
    foreach ($sellers as $seller) 
    {
    echo '<tr>';
    echo '<td>' . $seller->user_login . '</td>';
    
    $count1 = $wpdb->get_results("SELECT COUNT(*) AS count FROM wp_mymeta2 a JOIN wp_mymeta1 b ON b.id = a.my_id WHERE a.value = '$seller->user_login' AND b.date_posted LIKE '%2014-04-19%'" );
    echo '<td>' . $count1[0]->count . '</td>'; 
    
    $count2 = $wpdb->get_results("SELECT COUNT(*) AS count FROM wp_mymeta2 a JOIN wp_mymeta1 b ON b.id = a.my_id WHERE a.value = '$seller->user_login' AND b.date_posted LIKE '%$2014-04-18%'" );
    echo '<td>' . $count2[0]->count . '</td>';
    
    $count3 = $wpdb->get_results("SELECT COUNT(*) AS count FROM wp_mymeta2 a JOIN wp_mymeta1 b ON b.id = a.my_id WHERE a.value = '$seller->user_login' AND b.date_posted LIKE '%2014-05-17%'" );
    echo '<td>' . $count3[0]->count . '</td>'; 
    
    echo '</tr>';
    
     //add whatever you want to $arrSellers using array_push
    
    }
    echo '</table>';