1

尝试测试循环在哪一行,如果它大于或等于六,请将 $TESTIMAGE 变量插入到 span 元素中以进行下一次迭代。

当我运行代码时,它会将变量插入第一行之后的所有内容中。

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



}
4

5 回答 5

2

使用递增索引:

$i = 0;
while($row = mysql_fetch_array($result)){
    $i += 1;
}
于 2012-12-11T00:41:08.580 回答
0

这是因为mysql_num_rows()对于循环的每次迭代都将返回相同的精确值,因为结果更改中的行数不会改变。

你需要实现一个计数器来做你想做的事情。

于 2012-12-11T00:42:04.197 回答
0

试试这样:

$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++;

}

于 2012-12-11T00:42:49.720 回答
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 />
    ";
}​
于 2012-12-11T00:43:29.683 回答
0

对 mysql_num_rows($result) 的调用总是返回相同的数字。您想在每次迭代时增加一个索引:

$idx = 0
while (blah) {
    if ($idx >= 6)
    {
        $TESTIMAGE = "image_display_up";
    }   
    $idx += 1
}
于 2012-12-11T00:43:32.387 回答