Hey all,
need some advice. which would you say was more efficient i've checked the execution times of both scripts and they average out about the same tbh, just wondered if one way was a 'better' way of doing it.
Code 1
<?php
$url_home = 'url';
$top_10 = "SELECT p.products_id, p.products_status, p.products_ordered, pd.products_id, pd.products_name, p.manufacturers_id
FROM products AS p, products_description AS pd
WHERE p.products_id = pd.products_id
AND p.manufacturers_id = '".$brID."'
AND p.products_status = '1'
ORDER BY p.products_ordered DESC
LIMIT 10";
$result = mysql_query($top_10) or die(mysql_error());
$manufacturer = "SELECT manufacturers_name
FROM manufacturers
WHERE manufacturers_id = '".$brID."'";
$man_q = mysql_query($manufacturer) or die(mysql_error());
$man_r = mysql_fetch_array($man_q);
$b_name = $man_r['manufacturers_name'];
echo '<div class="blah">'.$b_name.' Sellers</div>
<ul>';
while($top_10_r = mysql_fetch_array($result)){
$prod_id = $top_10_r['products_id'];
$product_buy_link = $url_home .'/index.php?main_page=product_info&products_id='. $prod_id;
$name_sml = str_replace($b_name,"",$top_10_r['products_name']);
$top_10_r = str_replace("&","&",$top_10_r);
if (strlen($name_sml) > 45 ) {
$name_sml = substr($name_sml, 0, 42) . "...";
}
echo'<li> <a href="'.$product_buy_link.'">' .$name_sml. '</a></li>';
}
echo '</ul>';
?>
Code 2
<?php
$url_home = 'url';
$top_10 = "SELECT p.products_id, p.products_status, p.products_ordered, pd.products_id, pd.products_name, p.manufacturers_id, m.manufacturers_id, m.manufacturers_name
FROM products AS p, products_description AS pd, manufacturers AS m
WHERE p.products_id = pd.products_id
AND p.manufacturers_id = '".$brID."'
AND p.manufacturers_id = m.manufacturers_id
AND p.products_status = '1'
ORDER BY p.products_ordered DESC
LIMIT 10";
$result = mysql_query($top_10) or die(mysql_error());
$first_grabbed = false;
while($top_10_r = mysql_fetch_array($result)){
$b_name = $top_10_r['manufacturers_name'];
if ($first_grabbed == false){
echo '<div class="blah">'.$b_name.' Sellers</div><ul>';
$first_grabbed = true;
}
$prod_id = $top_10_r['products_id'];
$product_buy_link = $url_home .'/index.php?main_page=product_info&products_id='. $prod_id;
$name_sml = str_replace($b_name,"",$top_10_r['products_name']);
$top_10_r = str_replace("&","&",$top_10_r);
if (strlen($name_sml) > 45 ) {
$name_sml = substr($name_sml, 0, 42) . "...";
}
echo'<li> <a href="'.$product_buy_link.'">' .$name_sml. '</a></li>';
}
echo '</ul>';
?>
basically is it better to call 2 queries or is it better to do 1 query with an extra loop? or neither :O
any feedback greatly recieved
Andy.