TheBlackBenzKid
TheBlackBenzKid

Reputation: 27087

How to return a specific row in PHP and MySQL?

I am trying to add a mouseover hover which basically works on the first MySQL returned query. However it will not work. It does not recognise my IF ELSE statement and it just returns the ELSE command.

My data is like

gallery
----------------------
id      sku         img                     types
1       454_red     front.jpg                   F
2       454_red     back.jpg                    F
3       452_red     front.jpg                   F
4       452_red     back.jpg                    F
5       452_red     a1.jpg                      S   
6       452_red     a2.jpg                      S

My PHP

<?
$imgsql=mysql_query("SELECT * FROM `gallery` WHERE `gallery`.`sku` = '".$r['sku']."' ORDER BY `gallery`.`type` ASC");
while($rimg=mysql_fetch_array($imgsql)){ 
?>
<? if($rimg == $rimg['0']){ ?>
        <div>
            <a href="product.php?prodref=<?=$r['sku']?>"><img src="//super.cdn.com/<?=$r['sku']?>/<?=$rimg['img']?>.jpg" onmouseover="this.src='//super.cdn.com/<?=$r['sku']?>/back.jpg'" onmouseout="this.src='//super.cdn.com/<?=$r['sku']?>/<?=$rimg['img']?>.jpg'"/></a>
        </div>
    <? } else { ?>
        <div>
            <a href="product.php?prodref=<?=$r['sku']?>"><img src="//super.cdn.com/<?=$r['sku']?>/<?=$rimg['img']?>.jpg"/></a>
        </div>
    <? } ?>
<? } ?>

The $r["sku"] is called at the top of the code, this sits inside a product listing loop.

Upvotes: 0

Views: 131

Answers (1)

GBD
GBD

Reputation: 15981

To make it simple, then take another variable say counter or flag as below

<?
$count = 1;

$imgsql=mysql_query("SELECT * FROM `gallery` WHERE `gallery`.`sku` = '".$r['sku']."' ORDER BY `gallery`.`type` ASC");


while($rimg=mysql_fetch_array($imgsql)){ 
?>
<? if($count==1){ ?>
        <div>
            <a href="product.php?prodref=<?=$r['sku']?>"><img src="//super.cdn.com/<?=$r['sku']?>/<?=$rimg['img']?>.jpg" onmouseover="this.src='//super.cdn.com/<?=$r['sku']?>/back.jpg'" onmouseout="this.src='//super.cdn.com/<?=$r['sku']?>/<?=$rimg['img']?>.jpg'"/></a>
        </div>
    <? } else { ?>
        <div>
            <a href="product.php?prodref=<?=$r['sku']?>"><img src="//super.cdn.com/<?=$r['sku']?>/<?=$rimg['img']?>.jpg"/></a>
        </div>
    <? } ?>
<? 
$count = 0;
} 

?>

Upvotes: 2

Related Questions