我正在使用以下命令从 WordPress 数据库中的表中检索记录...
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>';
这非常有效,并且返回我想要的数据并将其回显到表格中。
我现在想将数据放入数组中,这样我最终会得到......
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',
),
);
有没有人有一个例子可以为我指明实现类似目标的正确方向?
最佳答案
您可以在 foreach 循环中使用 array_push 函数
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>';
关于sql - 将 SQL 查询输出为数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23174817/