1

Trying to test which row the loop is on and if it's greater than or equal to six, plug the $TESTIMAGE variable into the span element for the next iteration.

When I run the code, it plugs the variable into everything following the first row.

While($row = mysql_fetch_array($result))

{

    //assign variables
    $title = $row['title'];
    $url = $row['location'];
    $image = "/waves/files/images/games/$title.png";

    echo "
            <span class='dropt'>

                <a href=$url>$title</a>

                        <span class='$TESTIMAGE'>
                            <img src='$image'>
                        </span>

            </span>
            <br />
    ";



//Test to see which row we're on -- adjust image position
If (mysql_num_rows($result) >= 6)
{
$TESTIMAGE = "image_display_up";
}   



}
1
  • So what you're saying is that you'd like that add something to the markup if it's row number 7 ? Commented Dec 11, 2012 at 0:40

5 Answers 5

2

use an increasing index:

$i = 0;
while($row = mysql_fetch_array($result)){
    $i += 1;
}
Sign up to request clarification or add additional context in comments.

Comments

0

That is because mysql_num_rows() will return the same exact value for each iteration of the loop, as the number of rows in the result change will not change.

You would need to implement a counter to do what you are wanting to do.

Comments

0

try it like this:

$i = 1;

While($row = mysql_fetch_array($result)) {
    if(!($i%6)) {  // will enter here on the 6th try.

    //assign variables
    $title = $row['title'];
    $url = $row['location'];
    $image = "/waves/files/images/games/$title.png";

        echo "
        <span class='dropt'>

            <a href=$url>$title</a>

                    <span class='$TESTIMAGE'>
                        <img src='$image'>
                    </span>

            </span>
            <br />
       ";
  }
  if($i!=6)  // this way it remains on 6
     $i++;

}

Comments

0
$i=0;
While($row = mysql_fetch_array($result)) {
    //assign variables
    $title = $row['title'];
    $url = $row['location'];
    $image = "/waves/files/images/games/$title.png";
    $TESTIMAGE = ($i++ >= 6) ? "image_display_up" : "";

    echo "
            <span class='dropt'>
                <a href=$url>$title</a>
                        <span class='$TESTIMAGE'>
                            <img src='$image'>
                        </span>
            </span>
            <br />
    ";
}​

Comments

0

The call to mysql_num_rows($result) always returns the same number. You want to increment an index on each iteration instead:

$idx = 0
while (blah) {
    if ($idx >= 6)
    {
        $TESTIMAGE = "image_display_up";
    }   
    $idx += 1
}

Comments

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.