开发者

PHP while loop variable won't increment

I am using a variable($x) to keep track of how many times my loop has run, and using that to end rows on my table. However, every time the loop runs through it, $x is set to 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开发者_C百科=$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>";

the pictures do display fine,properly resized, but every photo is on a different row. What I am trying to do is put 5 thumbnails on each row, then go down to the next row and show 5 more. However, since the variable keeps resetting, I can't get them all on the right row. Any help is much appreciated. however, since the variable keeps resetting, I can't


You need to use the equality operator, not the assignment operator.

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.


You are using the wrong kind of equal signs for comparison. if ($x=0) will set the value of $x to zero. You need to use if ($x==0) instead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜