0

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.

4
  • $row['0']...? Where'd this come from...? And what does "does not work" mean exactly? Commented Oct 14, 2012 at 10:56
  • if($rimg == $rimg['0'])...? How does this condition make any sense? Commented Oct 14, 2012 at 11:02
  • What exactly are you trying to accomplish because deceze is right that if statement makes no sense at all. Commented Oct 14, 2012 at 11:04
  • I want to get the first record do something, otherwise do something else Commented Oct 14, 2012 at 11:21

1 Answer 1

2

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

?>
Sign up to request clarification or add additional context in comments.

3 Comments

Ahhh I was considering a loop counter too +1 ... how would I have done the MySQL count FYI
How do I return a specific Row ?
mysql_query() gives you resultset so if it have one row then you have directly use mysql_fetch_array without loop or more rows, you have to iterate it through loop

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.