我一直在研究这个问题,并且终于使我的工作成功了。我意识到我的代码效率非常低,并且这显示了加载数据需要很长时间。
我正在使用SQL查询创建一个数组,如下所示......
function table_data()
{
global $wpdb;
$blogusers = get_users('blog_id=1&orderby=nicename&role=client');
$form = GFFormsModel::get_form_meta( '337' );
foreach( $form['fields'] as $field ) {
if ( $field['adminLabel'] == 'I_BD' )
$clientid_field = $field['id'];
}
$example_data = array();
foreach ($blogusers as $user)
{
$todaysdate = date("Y-m-d");
$count0 = $wpdb->get_results("SELECT COUNT(*) AS count FROM l_detail a JOIN g_lead b ON b.id = a.l_id WHERE a.value = '$user->user_login' AND b.date_created LIKE '%$todaysdate%' AND a.field_number = '$clientid_field'" );
$onedayago = date("Y-m-d", strtotime( '-1 days' ) );
$count1 = $wpdb->get_results("SELECT COUNT(*) AS count FROM l_detail a JOIN g_lead b ON b.id = a.l_id WHERE a.value = '$user->user_login' AND b.date_created LIKE '%$onedayago%' AND a.field_number = '$clientid_field'" );
$twodaysago = date("Y-m-d", strtotime( '-2 days' ) );
$count2 = $wpdb->get_results("SELECT COUNT(*) AS count FROM l_detail a JOIN g_lead b ON b.id = a.l_id WHERE a.value = '$user->user_login' AND b.date_created LIKE '%$twodaysago%' AND a.field_number = '$clientid_field'" );
$threedaysago = date("Y-m-d", strtotime( '-3 days' ) );
$count3 = $wpdb->get_results("SELECT COUNT(*) AS count FROM l_detail a JOIN g_lead b ON b.id = a.l_id WHERE a.value = '$user->user_login' AND b.date_created LIKE '%$threedaysago%' AND a.field_number = '$clientid_field'" );
$fourdaysago = date("Y-m-d", strtotime( '-4 days' ) );
$count4 = $wpdb->get_results("SELECT COUNT(*) AS count FROM l_detail a JOIN g_lead b ON b.id = a.l_id WHERE a.value = '$user->user_login' AND b.date_created LIKE '%$fourdaysago%' AND a.field_number = '$clientid_field'" );
$fivedaysago = date("Y-m-d", strtotime( '-5 days' ) );
$count5 = $wpdb->get_results("SELECT COUNT(*) AS count FROM l_detail a JOIN g_lead b ON b.id = a.l_id WHERE a.value = '$user->user_login' AND b.date_created LIKE '%$fivedaysago%' AND a.field_number = '$clientid_field'" );
$sixdaysago = date("Y-m-d", strtotime( '-6 days' ) );
$count6 = $wpdb->get_results("SELECT COUNT(*) AS count FROM l_detail a JOIN g_lead b ON b.id = a.l_id WHERE a.value = '$user->user_login' AND b.date_created LIKE '%$sixdaysago%' AND a.field_number = '$clientid_field'" );
$arrData=array("client"=>"$user->user_login","sixdaysago"=>$count6[0]->count,"fivedaysago"=>$count5[0]->count,"fourdaysago"=>$count4[0]->count,"threedaysago"=>$count3[0]->count,"twodaysago"=>$count2[0]->count,"yesterday"=>$count1[0]->count,"today"=>$count0[0]->count,"total"=>$count[0]->count);
array_push($example_data, $arrData);
}
return $example_data;
}
正如我所提到的,这可以正常工作,但需要很长时间来处理有时服务器崩溃的数据。
如何才能提高效率?我意识到我可能以完全错误的方式写了这个,所以我希望受到教育!
答案 0 :(得分:0)
您可以使用单个查询获取所有这些数据。只需选择少于7天的所有行,然后按用户ID和DAY(date_created)分组。然后,您可以根据输出的需要将该数据读入任何适当的多维数组。确保连接,过滤和分组中使用的所有列都有索引。