我正在使用php 我想显示每个产品的第一张照片。 并显示产品的所有图像。 这是我的表格: https://drive.google.com/file/d/0B3OFq9-wT2HGLXVrV2NJUmtQdjg/view?usp=sharing 这是我的代码:
<?php
// Connects to Database
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("testdb") or die(mysql_error());
$result1 = mysql_query("SELECT photo_path, min(photo_id), photo_prodid FROM mp_photos GROUP BY photo_prodid"); // displays the first photo for each product
$result2 = mysql_query("SELECT photo_path FROM mp_photos order by photo_path asc");
// display all photos foreach product
while($row1 = mysql_fetch_assoc($result1))
{
echo ' <li class="portfolio_item_li">
<div class="portfolio_item ">
<figure class="thumbnail thumbnail__pf">
<a id="'.$row1['photo_path'].'" style="width:370px; height:240px;" href="'.$row1['photo_path'].'" class="image-ap" title="galleru" rel="Gall[gallery5]">
<img src="'.$row1['photo_path'].'" alt=""/>
<span class="dsp-icon"></span> </a>
</figure>';
while($row2 = mysql_fetch_assoc($result2)) {
echo '<a href="' . $row2['photo_path'] . '" class="image-wrap" title="Gallery Format" style="display:none" rel="prettyPhoto[gallery6]"></a>';
}
echo '</li>';
}
?>
</ul>
你能帮助我吗?
谢谢
答案 0 :(得分:0)
试试这个:
while($row1 = mysql_fetch_assoc($result1))
{
echo ' <li class="portfolio_item_li">
<div class="portfolio_item ">
<figure class="thumbnail thumbnail__pf">
<a id="'.$row1['photo_path'].'" style="width:370px; height:240px;" href="'.$row1['photo_path'].'" class="image-ap" title="galleru" rel="Gall[gallery5]">
<img src="'.$row1['photo_path'].'" alt=""/>
<span class="dsp-icon"></span> </a>
</figure>';
$result2 = mysql_query("SELECT photo_path FROM mp_photos WHERE `photo_proid`=".$row1['product_id']." LIMIT 1 ORDER BY `photo_id` ASC");
if(mysql_num_rows($result2)>0) {
while($row2 = mysql_fetch_assoc($result2)) {
echo '<a href="' . $row2['photo_path'] . '" class="image-wrap" title="Gallery Format" style="display:none" rel="prettyPhoto[gallery6]"></a>';
}
}
您需要为每个产品调用查询。
答案 1 :(得分:0)
我建议你在照片的id上使用增量,当它们对应同一产品时。
photo_id : 1 , 2 , 1 , 1
photo_proid : 1 , 1 , 2 , 3
从产品X中选择第一张照片的路径:
SELECT photo_path FROM mp_photos WHERE photo_proid = X AND photo_id = 1
从产品X中选择所有照片:
SELECT photo_path FROM mp_photos WHERE photo_proid = X