Skip to main content
edited tags
Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Check efficiency of 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 Bookingsupcoming bookings, and there is a potential to have every hour within 2015 to be booked; which means that I might query over 8700 orders.

<?php
    function objectToArray($d) {
        if (is_object($d)) {
            $d = get_object_vars($d);
        }

function objectToArray($d) {
      if (is_objectis_array($d)) {
        $d = get_object_vars  return array_map(__FUNCTION__, $d);
        } 
        else {
            return $d;
        }
    }

    if (is_array($d))$results {
= $wpdb->get_results("SELECT tzYeb_2_posts.* FROM tzYeb_2_posts INNER JOIN tzYeb_2_term_relationships returnON array_map(__FUNCTION__,tzYeb_2_posts.ID $d= tzYeb_2_term_relationships.object_id);
 WHERE 1=1 AND }( elsetzYeb_2_term_relationships.term_taxonomy_id {
IN (11) ) AND tzYeb_2_posts.post_type = 'shop_order' AND return((tzYeb_2_posts.post_status $d;
= 'publish')) GROUP BY }
}tzYeb_2_posts.ID ORDER BY tzYeb_2_posts.post_date DESC");

$results = $wpdb->get_results("SELECT tzYeb_2_posts.* FROM$time_array tzYeb_2_posts= INNERarray();
 JOIN tzYeb_2_term_relationships ON (tzYeb_2_posts.ID$fname_array = tzYeb_2_term_relationships.object_idarray() WHERE;
 1=1 AND ( tzYeb_2_term_relationships.term_taxonomy_id$church_array IN= array(11) ) AND tzYeb_2_posts.post_type;
 = 'shop_order' AND ((tzYeb_2_posts.post_status$city_array = 'publish')array() GROUP;
 BY tzYeb_2_posts.ID ORDER BY$state_array tzYeb_2_posts.post_date= DESC"array();

$time_array = array();
$fname_array = array();
$church_array = array();
$city_array = array();
$state_array$new_array = array();

$new_array    foreach ($results as $result) {
        $order_items = array$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 ($results$order_items as $result$order_item) {
    $order_items        $item_meta = $wpdb->get_results("SELECT"
   order_item_id, order_item_name, order_item_type                                   SELECT    *
                                        FROM        {$wpdb->prefix}woocommerce_order_itemswoocommerce_order_itemmeta
                    WHERE   order_id = " . $result->ID . "
           WHERE    order_item_id = " . $order_item->order_item_id . AND"
     order_item_type IN ('line_item')
                    ORDER BY    order_item_id"    ");

    foreach ($order_items as $order_item) {
        $item_meta$meta_array = $wpdb->get_resultsobjectToArray("
                                    SELECT  *
                                    FROM        {$wpdb->prefix}woocommerce_order_itemmeta
                                    WHERE   order_item_id = " . $order_item->order_item_id . "
                                "$item_meta);

        $meta_array    $timestamp = objectToArray($item_meta);"";
            $date = "";
            $time = "";
            $fname = "";
            $church = "";
            $city = "";
            $state = "";

        $timestamp = "";  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'];
            }

        foreach    $timestamp = strtotime($meta_array$time as. $data)' {' . $date);

            if ($data['meta_key'] == 'Booking Date')
                $date = $data['meta_value'];
            if array_push($data['meta_key'] == 'Booking$time_array, Time'$timestamp)
                $time = $data['meta_value'];;
            if array_push($data['meta_key'] == 'First Name - First Name')
               $fname_array, $fname = $data['meta_value'];
            if ($data['meta_key'] == 'Church Information - Church Name')
                $church = $data['meta_value'];;
            if array_push($data['meta_key'] == 'Church Information -$church_array, City'$church)
                $city = $data['meta_value'];;
            if array_push($data['meta_key'] == 'Church Information -$city_array, State'$city);
               array_push($state_array, $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;
    }
    $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>
        </div>

            <?php
            $i++;
            if ($i >= 10)
                break;
        }
    }
}
?>

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.

<?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;
    }
}
?>

Check efficiency of 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.

<?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;
        }
    }
?>
Tweeted twitter.com/#!/StackCodeReview/status/487334456039317505
added 480 characters in body
Source Link
Alex L
  • 5.7k
  • 2
  • 25
  • 68
<?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;
                        }
                    }
                    
                 ?>
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;
    }
}
?>
<?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;
                        }
                    }
                    
                 ?>
<?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;
    }
}
?>
deleted 50 characters in body
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237
Loading
Source Link
Loading