我正在使用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>


你能帮助我吗 ?

谢谢

最佳答案

尝试这个 :

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>';
            }
}


您需要为每个产品调用查询。

关于php - 显示与每个产品相对应的所有图像,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29343986/

10-09 15:39