使用<li>和<div>过滤</div> </li>进行Mysql查询

时间:2014-01-27 11:17:37

标签: php mysql fetch

我有一个产品表,我正在寻找:

$query=mysql_query("select * from PRODUCTS left join IMAGES on IMAGES.IDI = PRODUCTS.ID"); 

 while($product = mysql_fetch_array($query))
{
echo "<li><div><img>$product['PICTURENAME']</img></div></li>";
}

问题是有些产品有1个有一个以上的图像..要将它们列为幻灯片放映,而抓取应该创建该模式;

<li><div class="jquery"><img>$product['PICTURENAME']</img><img>$product['PICTURENAME']</img><img>$product['PICTURENAME']</img></div></li>
<li><div><img>$product['PICTURENAME']</img></div></li>
<li><div class="jquery"><img>$product['PICTURENAME']</img><img>$product['PICTURENAME']</img></div></li>

这意味着只有1张图片的产品应该是这样的;

<li><div><img>$product['PICTURENAME']</img></div></li>

其他的应该是;

<li><div class="jquery"><img>$product['PICTURENAME']</img><img>$product['PICTURENAME']</img></div></li>

由于JQUERY幻灯片放映,<img><li>内有两个<div>并排{/ 1}}

问题是“while”每次都会创建<li><div><img></div></li> ..对于幻灯片显示,它必须像<li><div><img1><img2><img3></div></li>

2 个答案:

答案 0 :(得分:2)

试试这个

$query=mysql_query("select * from PRODUCTS left join IMAGES on IMAGES.IDI = PRODUCTS.ID");
while($product = mysql_fetch_array($query))
{
$query_image=mysql_query("select * from IMAGES where IDI = ".$product['ID']);
if(mysql_num_rows($query_image)>1)
{
    echo '<li><div class="jquery">';
    while($image = mysql_fetch_array($query_image))
    {
        echo "<img>$image['PICTURENAME']</img>";
    }
    echo '</div></li>';
}
else
    echo "<li><div><img>$image['PICTURENAME']</img></div></li>";
}

答案 1 :(得分:1)

尝试:

$query=mysql_query("select * from PRODUCTS left join IMAGES on IMAGES.IDI = PRODUCTS.ID"); 
$count=mysql_num_rows($query);

if($count==1){
  $product = mysql_fetch_array($query);
  echo "<li><div><img>$product['PICTURENAME']</img></div></li>";
}
elseif($count>1){
{
   echo "<li><div class='jquery'>";
   while($product = mysql_fetch_array($query))
   {
    echo "<img>$product['PICTURENAME']</img>";
   }
    echo "</div></li>";
   }
}