SelectLimit($sub_orders_query, 100); // the last 100 orders with this product should suffice hopefully $num_sub_products_ordered = $sub_orders_result->RecordCount(); if ($num_sub_products_ordered >= 1) { $list_of_order_ids = array(); while ($sub_orders = $sub_orders_result->fields) { $list_of_order_ids[] = $sub_orders['orders_id']; // Move that ADOdb pointer! $sub_orders_result->MoveNext(); } } // if there are no orders with this product the next query is pointless too if (isset($list_of_order_ids)) { $orders_productstable = $oostable['orders_products']; $orderstable = $oostable['orders']; $productstable = $oostable['products']; $orders_query = "SELECT p.products_id, p.products_image FROM $orders_productstable opb, $orderstable o, $productstable p WHERE opb.products_id != '" . intval($nProductsId) . "' AND opb.products_id = p.products_id AND opb.orders_id = o.orders_id AND o.orders_id in (" . implode(',', $list_of_order_ids) . ") AND p.products_status >= '1' GROUP BY p.products_id ORDER BY o.date_purchased DESC"; $orders_result = $dbconn->SelectLimit($orders_query, MAX_DISPLAY_ALSO_PURCHASED); $num_products_ordered = $orders_result->RecordCount(); if ($num_products_ordered >= MIN_DISPLAY_ALSO_PURCHASED) { while ($orders = $orders_result->fields) { $aPurchased[] = array('products_name' => oos_get_products_name($orders['products_id']), 'products_id' => $orders['products_id'], 'products_image' => $orders['products_image']); // Move that ADOdb pointer! $orders_result->MoveNext(); } } } }