function wpfeatured_product_cta_randomizer($product_array) { $number_of_prods = get_option('product_cta_number_of_prods', 3); $products_in_array = count($product_array); //make sure we have enough products, or else just display however many are available... if ($products_in_array <= $number_of_prods) { $limit = $products_in_array; } else { $limit = $number_of_prods; } for ($i = 1; $i <= $limit; $i++) { ${'key_' . $i} = array_rand($product_array); ${'product_' . $i} = $product_array[${'key_' . $i}]; ${'key_' . $i}; //remove the key to prevent repeats unset($product_array[${'key_' . $i}]); //run echo function wpfeatured_product_cta_echo(${'product_' . $i}); } }
<h2 class="current_products">Current Products</h2> <?php global $wpdb; $table_name = $wpdb->prefix . 'featured_cta_plugin'; $product_array = $wpdb->get_results("SELECT * FROM " . $table_name, ARRAY_A); foreach ($product_array as $product) { ?> <div class="current_products_option_page"> <?php wpfeatured_product_cta_echo($product); //Also, for the options page, display the category... echo '<p class="options_category"><strong>Category: ' . get_the_category_by_ID($product['category']) . '</strong></p>'; $id = $product['id']; echo '<form action="" method="post" enctype="multipart/form-data"> <input class="option_delete" type="submit" name="delete_button" value="Delete Product" /> <input type="hidden" name="delete_product[]" value="' . $id . '" /> </form> <a href="' . admin_url('admin.php?page=wpfeatured-product-cta-update&id=' . $id) . '">Edit Product</a>'; echo '</div>'; }