Note that there are some explanatory texts on larger screens.

plurals
  1. POLimiting queries to a specific category
    text
    copied!<p>Im trying to do a mysql query and keep the results of the answer within a specific category, but I am lost on the correctness of the last line.</p> <pre><code>$category_in_now = get_products_category_id($_GET['products_id']); $similar_products = "SELECT p.products_type, p.products_id, pd.products_name, p.products_image, p.products_price, p.products_tax_class_id, p.products_date_added, m.manufacturers_name, p.products_model, p.products_quantity, p.products_weight, p.product_is_call, p.product_is_always_free_shipping, p.products_qty_box_status, p.master_categories_id FROM " . TABLE_PRODUCTS . " p LEFT JOIN " . TABLE_MANUFACTURERS . " m ON (p.manufacturers_id = m.manufacturers_id), " . TABLE_PRODUCTS_DESCRIPTION . " pd WHERE p.products_status = 1 AND p.products_id = pd.products_id AND p.master_categories_id == " . $category_in_now; </code></pre> <p>DEC 29: I ended up doing this, but am left with some questions on on the RAND LIMIT part the need for looping, am I doing this correctly?</p> <pre><code>&lt;?php echo '&lt;div&gt;'; $category_in_now = zen_get_products_category_id($_GET['products_id']); for($i=0;$i&lt;4;$i++) { $products_query_raw = "SELECT p.products_id, p.products_type, pd.products_name, p.products_image, p.products_price, p.master_categories_id FROM " . TABLE_PRODUCTS . " p LEFT JOIN " . TABLE_MANUFACTURERS . " m ON (p.manufacturers_id = m.manufacturers_id), " . TABLE_PRODUCTS_DESCRIPTION . " pd WHERE p.products_status = 1 AND p.products_id = pd.products_id AND p.master_categories_id = :catInNow ORDER BY rand() LIMIT 0,4"; $products_query_raw = $db-&gt;bindVars($products_query_raw, ':catInNow', $category_in_now, 'integer'); $result = $db-&gt;Execute($products_query_raw); echo '&lt;li&gt;'; echo '&lt;div id="item"&gt;'; if ($result-&gt;RecordCount() &gt; 0) { echo '&lt;a href="' . zen_href_link('product_info', 'cPath=' . $cPath . '&amp;products_id=' . $result-&gt;fields['products_id']) . '"&gt;'; echo zen_image(DIR_WS_IMAGES . $result-&gt;fields['products_image'], $result-&gt;fields['products_name'], SMALL_IMAGE_WIDTH, SMALL_IMAGE_HEIGHT); echo '&lt;/a&gt;'; echo '&lt;/div&gt;'; echo '&lt;/li&gt;'; } } echo '&lt;/div&gt;'; ?&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload