0

The aim

  1. The user enters inserts a number into a text field and hits calculate

  2. That number is then subtracted by 250

  3. Depending on what the $result is, a particular image is shown.

The problem

When the page is run I get the following error message;

Catchable fatal error: Object of class mysqli_result could not be converted to string in /home/cs12jcw/public_html/n-power/includes/calculator.php on line 95

Line 95 is;

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

The full code

<?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($db_server, "SELECT URL     FROM images WHERE imagename = 'image1'");
    break;

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

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

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

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

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

}
?>
Dharman
  • 30,962
  • 25
  • 85
  • 135

2 Answers2

1

You've forgot to fetch your result :

$row = mysqli_fetch_array($image);
echo "<img src='$row[URL]' alt='' />";
potashin
  • 44,205
  • 11
  • 83
  • 107
1

You are setting your $image variable to a mysql result object. You still need to fetch the data from the result. Try the below

$result = mysqli_query($db_server, "SELECT URL FROM images WHERE imagename = 'image1'");
$obj = mysqli_fetch_object($result);
$image = $obj->URL;

Also, you could modify your switch statement to the below, so you only have one spot issuing the query

$imagename = '';
switch( $answer ){
    case $answer > 0 and $answer < 150 : $imagename = 'image1';
    break;

    case $answer < 250 : $imagename = 'image2';
    break;

    case $answer < 350 : $imagename = 'image3';
    break;

    case $answer < 450 : $imagename = 'image4';
    break;

    case $answer < 550 : $imagename = 'image5';
    break;
}

if ($imagename) {
    $result = mysqli_query($db_server, "SELECT URL FROM images WHERE imagename = '$imagename'");
    $obj = mysqli_fetch_object($result);
    $image = $obj->URL;

    echo "<img src='$image' alt='' />";
}
poops
  • 186
  • 2
  • 9