我有這樣的代碼,它確實不是最漂亮和最有效的代碼。代碼的作品,但我覺得有辦法避免所有的循環,並結合這些查詢,並可能創建一個數組。我只是想知道如果任何人有關於如何提高這個有沒有辦法將這3個查詢合併爲一個
$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++;
}
}
?>
如果您不介意,我已將代碼粘貼到問題主體中。 – 2011-06-16 13:47:12
沒問題......謝謝 – Trace 2011-06-16 14:00:11
這段代碼讓我的眼睛流血:) – 2011-06-16 14:12:29