Skip to main content
2 of 5
deleted 50 characters in body
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Check efficiency PHP code that will query over 8500 orders

I am querying all orders from a WordPress database that uses the WooCommerce Bookings plugin. I want to display Upcoming Bookings and there is a potential to have every hour within 2015 to be booked; which means that I might query over 8700 orders.

I'm new to PHP and did most of this following code myself. I'm not sure if there is a more efficient way of writing this code. I don't want my code to hinder site performance.

<?php 
                    
                    function objectToArray($d) {
                        if (is_object($d)) {
                            $d = get_object_vars($d);
                        }
                 
                        if (is_array($d)) {
                            return array_map(__FUNCTION__, $d);
                        }
                        else {
                            return $d;
                        }
                    }

                    $results = $wpdb->get_results("SELECT tzYeb_2_posts.* FROM tzYeb_2_posts INNER JOIN tzYeb_2_term_relationships ON (tzYeb_2_posts.ID = tzYeb_2_term_relationships.object_id) WHERE 1=1 AND ( tzYeb_2_term_relationships.term_taxonomy_id IN (11) ) AND tzYeb_2_posts.post_type = 'shop_order' AND ((tzYeb_2_posts.post_status = 'publish')) GROUP BY tzYeb_2_posts.ID ORDER BY tzYeb_2_posts.post_date DESC");
                    
                    $time_array = array();
                    $fname_array = array();
                    $church_array = array();
                    $city_array = array();
                    $state_array = array();
                    
                    $new_array = array();
                    
                    foreach( $results as $result ) {
                        $order_items = $wpdb->get_results("
                            SELECT  order_item_id, order_item_name, order_item_type
                            FROM        {$wpdb->prefix}woocommerce_order_items
                            WHERE   order_id = " . $result->ID . "
                            AND     order_item_type IN ('line_item')
                            ORDER BY    order_item_id
                        ");
                        
                        foreach( $order_items as $order_item ) {
                            $item_meta = $wpdb->get_results("
                                SELECT  *
                                FROM        {$wpdb->prefix}woocommerce_order_itemmeta
                                WHERE   order_item_id = " . $order_item->order_item_id . "
                            ");
                            
                            $meta_array = objectToArray( $item_meta );
                            
                            $timestamp = "";
                            $date = "";
                            $time = "";
                            $fname = "";
                            $church = "";
                            $city = "";
                            $state = "";
                            
                            foreach( $meta_array as $data ) {
                                    
                                    if( $data['meta_key'] == 'Booking Date' ) 
                                        $date = $data['meta_value'];
                                    if( $data['meta_key'] == 'Booking Time' )
                                        $time = $data['meta_value'];
                                    if( $data['meta_key'] == 'First Name - First Name' )
                                        $fname = $data['meta_value'];
                                    if( $data['meta_key'] == 'Church Information - Church Name' )
                                        $church = $data['meta_value'];
                                    if( $data['meta_key'] == 'Church Information - City' )
                                        $city = $data['meta_value'];
                                    if( $data['meta_key'] == 'Church Information - State' )
                                        $state = $data['meta_value'];
                                
                                
                            }
                            
                            $timestamp = strtotime( $time . ' ' . $date );
                            
                            array_push( $time_array, $timestamp );
                            array_push( $fname_array, $fname );
                            array_push( $church_array, $church );
                            array_push( $city_array, $city );
                            array_push( $state_array, $state );
                            
                        }
                        
                    }
                    
                    $arrays = array( $fname_array, $church_array, $city_array, $state_array );
                    
                    foreach( $time_array as $index => $key ) {
                        $t = array();
                        foreach( $arrays as $array ) {
                            $t[] = $array[$index];
                        }
                        $up[$key] = $t;
                    }
                    
                    ksort( $up );
                    
                    $i = 0;
                    foreach( $up as $tsort => $tval ) { 
                        if( $tsort > strtotime('now') ) {
                        ?>
                        
                        <div class="wc-upcoming-booking">
                            <div class="wc-upcoming-time">
                                <span class="upcoming-hour"><?php echo date( 'g:i A', $tsort ); ?></span>
                                <span class="upcoming-date"><?php echo date( 'F j', $tsort ); ?></span>
                            </div>
                            <div class="wc-upcoming-details">
                                <?php if( $tval[0] ) { echo $tval[0]; } ?>
                                <?php if( $tval[1] ) { echo ', ' . $tval[1]; } ?>
                                <?php if( $tval[2] ) { echo ', ' . $tval[2]; } ?>
                                <?php if( $tval[3] ) { echo ', ' . $tval[3]; } ?>
                            </div>
                        </div>
                        
                    <?php
                        
                        $i++;
                        if( $i >= 10 ) 
                            break;
                        }
                    }
                    
                 ?>