php基于$ answer回显图像

时间:2014-05-01 13:44:16

标签: php

这是一个大学项目,我有一个页面将是一个php计算器,用户可以在其中输入$ valuea'可能会在0-550之间,然后他们会点击提交,减去' $ valueb'这是250,给他们' $回答'。

我需要帮助的是,一旦用户点击提交,我就会有一个特定的图像回显,这取决于$ answer的数字。

这是我到目前为止所做的,但它不起作用;

<?php
if (isset($_POST['valuea'])) $valuea = $_POST['valuea'];
if (isset($_POST['valueb'])) $valueb = '250';
$answer = $valuea - $valueb;

echo <<<_END
<form method='post' action='calculator.php'>
<table border='0' width='500px' cellpadding='3' cellspacing='1' class="table">
<tr class="calcheading"><td colspan="2"><strong>Work out how much you could be     saving</strong></td></tr>
<tr class="calcrow"><td>How much do you spend a year?</td><td align="center"><input     type='text' name='valuea' value="$valuea"/></td></tr>
<tr class="calcrow"><td>Minus the average price of an n-power student tarrif* Leave     Blank:</td><td align="center"><input type='text' name='valueb' value="$valueb"/></td></tr>
<tr class="submit"><td colspan="2"><input type='submit' value='Calculate'/></td></tr>
_END;
?>

<tr class="calcrow">
<td><i>You could be saving:</td>
<td align="center"><input type="text" value="<?php echo round($answer)?>"></td></i>
</tr>
</table>
</form>

<?php
if($db_server){

$image1 = mysqli_query("SELECT URL FROM images WHERE imagename='image1');"

$image2 = mysqli_query("SELECT URL FROM images WHERE imagename='image2');"

$image3 = mysqli_query("SELECT URL FROM images WHERE imagename='image3');"

$image4 = mysqli_query("SELECT URL FROM images WHERE imagename='image4');"

$image5 = mysqli_query("SELECT URL FROM images WHERE imagename='image5');"

if( $answer > 0 and $answer < 150 )?>
 <img src="<?php echo $image1;?>"/>

else if( $answer < 250)
   <img src="<?php  echo $image2;?>"/>
else if( $answer < 350)
   <img src="<?php  echo $image3;?>"/>
else if( $answer < 450)
   <img src="<?php  echo $image4;?>"/>
else if( $answer < 550)
   <img src="<?php  echo $image5;?>"/>
   <?php
else
   echo 'error';

}
?>

1 个答案:

答案 0 :(得分:2)

你的代码真是一团糟。我已经清理了一下。

<?php
$valuea = (isset($_POST['valuea']) && is_numeric($_POST['valuea'])) ? $_POST['valuea'] : 0;
$valueb = 250;

$answer = $valuea - $valueb;

?>
<form method='post' action='calculator.php'>
<table border='0' width='500px' cellpadding='3' cellspacing='1' class="table">
    <tr class="calcheading">
        <td colspan="2"><strong>Work out how much you could be     saving</strong></td>
    </tr>
    <tr class="calcrow">
        <td>How much do you spend a year?</td>
        <td align="center"><input     type='text' name='valuea' value="$valuea"/></td>
    </tr>
    <tr class="calcrow">
        <td>Minus the average price of an n-power student tarrif* Leave     Blank:</td>
        <td align="center"><input type='text' name='valueb' value="$valueb"/></td>
    </tr>
    <tr class="submit">
        <td colspan="2"><input type='submit' value='Calculate'/></td>
    </tr>
    <tr class="calcrow">
        <td><i>You could be saving:</td>
        <td align="center"><input type="text" value="<?php echo round($answer)?>"></td></i>
    </tr>
</table>
</form>

<?php
if($db_server){

    switch( $answer ){
        case $answer > 0 and $answer < 150 : $image = mysqli_query("SELECT URL FROM images WHERE imagename = 'image1'");
        break;

        case $answer < 250 : $image = mysqli_query("SELECT URL FROM images WHERE imagename = 'image2'");
        break;

        case $answer < 350 : $image = mysqli_query("SELECT URL FROM images WHERE imagename = 'image3'");
        break;

        case $answer < 450 : $image = mysqli_query("SELECT URL FROM images WHERE imagename = 'image4'");
        break;

        case $answer < 550 : $image = mysqli_query("SELECT URL FROM images WHERE imagename = 'image5'");
        break;
    }

    echo "<img src='$image' alt='' />";

}
?>