简体   繁体   中英

I'm getting what seems to be an infinite loop and can't figure out why - PHP/WordPress

I'm running through some distinct user ID's (about 147 of them, retrieved from a table with many duplicates) with a foreach loop and then when retrieved, using them to retrieve more user details. I then place these details within an array in order to push them to my JavaScript.

For some reason the result that I get from the array is what seems to be an infinite loop that has quit somewhere in the process.

Here's an example:

在此处输入图片说明

This is the code that I am currently running:

public function payments_rt_search() {
    global $wpdb;

    $date1 = $_POST['date1'];
    $date2 = $_POST['date2'];
    $threshold = intval($_POST['threshold']);

    $results = $wpdb->get_results( "SELECT DISTINCT vendor_id FROM {$wpdb->prefix}wcpv_commissions WHERE order_date BETWEEN '".$date1."'  AND '".$date2."'");

    $past_threshold_users = [];
    // echo json_encode($results);
    // wp_die();
    foreach ($results as $user_R) {
        $total_commissions = $wpdb->get_results( "SELECT SUM(product_commission_amount) AS TotalCommissions FROM {$wpdb->prefix}wcpv_commissions WHERE vendor_id = ".$user_R->vendor_id);
        $total_commission = 0;
        foreach($total_commissions as $total_commission_res)
        {
            $total_commission = $total_commission_res->TotalCommissions;
        }
        if ($total_commission >= $threshold)
        {
            $res2 = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}wcpv_commissions WHERE vendor_id = ".$user_R->vendor_id." LIMIT 1");
            // echo json_encode($res2[0]);
            // wp_die();
            //Add user details to array
            $user_deets = $res2[0];
            $user_arr = array(
                'vendor_id' => $user_deets->vendor_id,
                'vendor_name' => $user_deets->vendor_name,
                'paypal_email' => '',
                'amount' => $total_commission,
                'currency' => '$',
                'commission_status' => $user_deets->commission_status
            );
            $past_threshold_users[] = $user_arr;
            // echo json_encode($user_arr);
            // wp_die();                
        }
        else
        {
            continue;
        }
        echo json_encode($past_threshold_users);
    }

    //echo json_encode($results);

    wp_die();
}

Try this code

I have move this echo json_encode($past_threshold_users); code after foreach loop.

    public function payments_rt_search() {
        global $wpdb;

        $date1 = $_POST['date1'];
        $date2 = $_POST['date2'];
        $threshold = intval($_POST['threshold']);

        $results = $wpdb->get_results( "SELECT DISTINCT vendor_id FROM {$wpdb->prefix}wcpv_commissions WHERE order_date BETWEEN '".$date1."'  AND '".$date2."'");

        $past_threshold_users = [];
        // echo json_encode($results);
        // wp_die();
        foreach ($results as $user_R) {
            $total_commissions = $wpdb->get_results( "SELECT SUM(product_commission_amount) AS TotalCommissions FROM {$wpdb->prefix}wcpv_commissions WHERE vendor_id = ".$user_R->vendor_id);
            $total_commission = 0;
            foreach($total_commissions as $total_commission_res)
            {
                $total_commission = $total_commission_res->TotalCommissions;
            }
            if ($total_commission >= $threshold)
            {
                $res2 = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}wcpv_commissions WHERE vendor_id = ".$user_R->vendor_id." LIMIT 1");
                // echo json_encode($res2[0]);
                // wp_die();
                //Add user details to array
                $user_deets = $res2[0];
                $user_arr = array(
                    'vendor_id' => $user_deets->vendor_id,
                    'vendor_name' => $user_deets->vendor_name,
                    'paypal_email' => '',
                    'amount' => $total_commission,
                    'currency' => '$',
                    'commission_status' => $user_deets->commission_status
                );
                $past_threshold_users[] = $user_arr;
                // echo json_encode($user_arr);
                // wp_die();                
            }
            /*else
            {
                continue;
            }   */     
        }

        echo json_encode($past_threshold_users);
        //echo json_encode($results);

        wp_die();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM