开发者

Is there a way to make these 3 queries into one

I have this code which is really not the prettiest and the most efficient. The code works but i feel like there is way to avoid all the looping and combine these queries and make possibly one array. I was just wondering if anyone had any pointers on how to improve this

$sql = "SELECT p.image, group_concat(pi.image) as additional_images 
FROM product as p 
JOIN product_image as pi on pi.product_id=p.product_id 
WHERE p.product_id = '{$product['standard']['product_id']}'";
$result = mysql_query($sql);
$images_before = mysql_fetch_assoc($result);
$images = array();
if($images_before['additional_images'] != ""){
$images = explode(",", $images_before['additional_images']);
}
array_push($images, $images_before['image']);
$sql2 = "SELECT p.image, group_concat(pi.image) as additional_images 
FROM product as p 
JOIN product_image as pi on pi.product_id=p.product_id 
WHERE p.product_id = '{$product['professional']['product_id']}'";
$result2 = mysql_query($sql2);
$images_before2 = mysql_fetch_assoc($result2);
$images2 = array();
if($images_before2['additional_images'] != ""){
$images2 = explode(",", $images_before2['additional_images']);
}
array_push($images2, $images_before2['image']);
$sql3 = "SELECT p.image, group_concat(pi.image) as additional_images 
FROM product as p 
JOIN product_image as pi on pi.product_id=p.product_id 
WHERE p.product_id = '{$product['premium']['product_id']}'";
$result3 = mysql_query($sql3);
$images_before3 = mysql_fetch_assoc($result3);
$images3 = array();
if($images_before3['additional开发者_运维知识库_images'] != ""){
$images3 = explode(",", $images_before3['additional_images']);
}
array_push($images3, $images_before3['image']);
$counter = 0;
foreach($images as $image) {
      if ($counter == 0) {
          echo "<a id='show_{$product['standard']['product_id']}' style='display:none;' href='http://somesite.com/shop_possystems/image/{$image}' rel='prettyPhoto[show_{$product['standard']['product_id']}]'></a>";
                $counter++;
      }else{
        echo "<a style='display:none;' href='http://somesite.com/shop_possystems/image/{$image}' rel='prettyPhoto[show_{$product['standard']['product_id']}]'></a>";
            $counter++;
        }
}

$counter2 = 0;
  foreach($images2 as $image) {
        if ($counter2 == 0) {
            echo "<a id='show_{$product['professional']['product_id']}' style='display:none;' href='http://somesite.com/shop_possystems/image/{$image}' rel='prettyPhoto[show_{$product['professional']['product_id']}]'></a>";
                    $counter2++;
        }else{
          echo "<a style='display:none;' href='http://somesite.com/shop_possystems/image/{$image}' rel='prettyPhoto[show_{$product['professional']['product_id']}]'></a>";
                $counter2++;
            }
}
$counter3 = 0;
    foreach($images3 as $image) {
          if ($counter3 == 0) {
              echo "<a id='show_{$product['premium']['product_id']}' style='display:none;' href='http://somesite.com/shop_possystems/image/{$image}' rel='prettyPhoto[show_{$product['premium']['product_id']}]'></a>";
                        $counter3++;
          }else{
            echo "<a style='display:none;' href='http://somesite.com/shop_possystems/image/{$image}' rel='prettyPhoto[show_{$product['premium']['product_id']}]'></a>";
                    $counter3++;
                }
    }
?>


Instead of using where p.product_id = <value>, use WHERE IN syntax:

$sql = "SELECT  ...
WHERE p.product_id IN (
  {$product['standard']['product_id']},
  {$product['professional']['product_id']}, 
  {$product['premium']['product_id']}
)";


You code is very bad by itself. You need to study application architecture and design along with the language itself.

Here is a very good article to start: http://symfony.com/doc/current/book/from_flat_php_to_symfony2.html

As for the tips on code:

Don't use group_concat and the explode. Better to do two queries without JOIN:

  1. get all the images using IN()
  2. get all the additional_images using IN()
  3. loop over additional_images and bind them to images in PHP loop


$sql = "
    SELECT p.product_id, p.image, group_concat(pi.image) as additional_images 
    FROM product as p 
        JOIN product_image as pi on pi.product_id=p.product_id 
    WHERE p.product_id IN ({$product['standard']['product_id']}, {$product['professional']['product_id']}, {$product['premium']['product_id']})";

$result = mysql_query($sql);
while($product_result = mysql_fetch_assoc($result)) {
    $images = explode(',', $product_result['additional_images']);
    $first = true;
    foreach($images as $image) {
        if($first) {
            echo "<a id='show_{$product_result['product_id']}' style='display:none;' href='http://posnation.com/shop_possystems/image/{$image}' rel='prettyPhoto[show_{$product_result['product_id']}]'></a>";
            $first = false;
        }
        else {
            echo "<a style='display:none;' href='http://posnation.com/shop_possystems/image/{$image}' rel='prettyPhoto[show_{$product['standard']['product_id']}]'></a>";
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜