我正在使用一个变量($ x)来跟踪循环运行了多少次,并以此来结束表中的行。但是,每次循环运行时,$ x都设置为0。

$x=0;

function getname($gid)
{
    $query2="SELECT UID FROM gallery WHERE GID = '$gid'";
    $result2=mysql_query($query2) or die(mysql_error());
    $row2=mysql_fetch_array($result2);
    global $uid;
    $uid=$row2['UID'];

    $query3="SELECT user FROM login WHERE id='$uid'";
    $result3=mysql_query($query3) or die(mysql_error());
    $row3=mysql_fetch_array($result3);
    global $creator;
    $creator=$row3['user'];
}

$query="SELECT * FROM photos ORDER BY DATE LIMIT $offset, 20 ";
$result=mysql_query($query) or die(mysql_error());
$row=mysql_fetch_array($result);
while($row=mysql_fetch_array($result))
{
    $gid=$row['gid'];
    $pid=$row['pid'];
    $name=$row['photo'];

    getname($gid);

    $photo="photos/".$row['pid'].".".$row['type'];

    echo "<table border=1>";

    if ($x=0)
    {
        echo "<tr>";
        echo "yes";
    }

    $max_width = 100;
    $max_height = 100;
    list($width, $height) = getimagesize($photo);
    $w=$width;
    $h=$height;
    if($width > 100 or $height > 100)
    {
        $ratioh = $max_height/$height;
        $ratiow = $max_width/$width;
        $ratio = min($ratioh, $ratiow);
        // New dimensions
        $width = intval($ratio*$width);
        $height = intval($ratio*$height);
    }

    echo "<td><a href=view.php?pid=$pid> <img src=$photo width=$width height=$height /><big><font color=beige ><center>$name</center></font></big></a>";
    echo "<a href=user.php?uid=$uid><small><center>$creator</center></small></a></td>";
    echo $x;
    $x++;
    echo $x;
    if ($x=5)
    {
        echo "</tr>";
        $x=0;
    }
}
echo "</table>";


图片确实可以很好地显示并适当调整大小,但是每张照片都在不同的行上。我想做的是在每行上放置5张缩略图,然后下一行显示另外5张缩略图。但是,由于变量一直在重置,因此我无法将它们全部显示在正确的行上。任何帮助深表感谢。但是,由于变量不断重置,所以我无法

最佳答案

您需要使用相等运算符,而不是赋值运算符。

if ($x=0) // this sets x to 0, and the expression returns true
          // if the assignment succeed (it always does)

if ($x==0) // this checks if x is zero. and returns true/false based on that.

关于php - PHP while循环变量不会增加,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5463272/

10-12 12:52
查看更多