-3

Okay so I have radio buttons for the user to pick from certain images as their image. Their image starts as the default image and can change it to 2 others. Here's the radio buttons and PHP to display the image:

    <input type="radio" id="Default" name="change_image" value="0" />
    <input type="radio" id="Unknown" name="change_image" value="1" />     
    <input type="radio" id="Gasmask" name="change_image" value="2" />     

    <?php 
if ( $user_image < 1 )
{
    echo 
    '<img src="images/user_image/Default.png" width="60" height="60" />';
} 
elseif ( $user_image = 1 )
{
    echo 
    '<img src="images/user_image/Unknown.png" width="60" height="60" />';
}
elseif ( $user_image > 1 )
{
    echo 
    '<img src="images/user_image/GasMask.png" width="60" height="60" />';
}
?>

Well, the if and first elseif work, but the 3rd elseif will not display that image? Are their any reasons why?

4

3 回答 3

4

您使用=which 是赋值语句而不是比较语句。用来==比较!

此外,您所做的似乎有点开销(对我来说,我不是专家),但请注意我看不到您如何处理$user_image. 这是我的做法:

<input type="radio" id="Default" name="change_image" value="Default" />
<input type="radio" id="Unknown" name="change_image" value="Unknown" />     
<input type="radio" id="Gasmask" name="change_image" value="Gasmask" />     

<?php 
if (isset($_POST['change_image'])) { //make sure you validate that the right stuff is being posted
    $user_image = $_POST['change_image'];
    echo "<img src='images/user_image/".$user_image.".png' width='60' height='60' />";
}

我没有测试变量是否正确连接,但是只要您遵循名称作为单选按钮值的约定,这将为您节省许多 if 语句。我希望这有帮助!

于 2012-12-15T05:21:18.847 回答
2

我相信这是由于您的第二个 elseif。

elseif ( $user_image = 1 )

在您应该测试相等性的地方,您将 1 的值分配给 $user_image。尝试将其替换为:

elseif ( $user_image == 1 )
于 2012-12-15T05:10:31.120 回答
2

如果条件人使用双相等,您正在分配

if ( $user_image < 1 )
{
    echo 
    '<img src="images/user_image/Default.png" width="60" height="60" />';
} 
elseif ( $user_image == 1 )
{
    echo 
    '<img src="images/user_image/Unknown.png" width="60" height="60" />';
}
于 2012-12-15T05:10:33.927 回答