Question

I keep getting this error:

Fatal error: Unsupported operand types on line 34

And I'm not sure how to fix it, i am trying to make it so that the number that is entered into the form such as 10, will be the discount and it will echo the discounted price, can anyone help me fix this, here is the code

<!DOCTYPE html>
<html>
<head>
<title> Car Shop Cars! </title>
<link rel="stylesheet" type="css/text" href="style.css" 
</head>

<body>

<div id="header"> 
</div>

<div id="carpic">
</div>

<div id="price">
<center> This is the Nissan 350z and costs 8,999,999 <br>
please enter your promo code </center>
</div>

<div id="form">
<form action="index.php" method="post">

   <center> <input type="text" name="percent" id="percent" />
  <input type="submit"  /> </center>

</form> 
<?php
        $percent=$_POST['percent'];
    $total=['8,999,999'];

    /*calculation for discounted price */ 

    $discount_value= ($total / 100) *$percent;

    $final_price = $total - $discount_value;

    echo $final_price;

?> 
</div>

</body> 
</html>
Was it helpful?

Solution

This is invalid

$total=['8,999,999'];

I guess you mean something like this

$total = 8999999;

OTHER TIPS

Look at your code:

$discount_value = ($total / 100) * $percent;
                          ^
                          |

So more specifically here:

$total / 100
       ^
       |

If you put in there the value of $total you just have set the lines before;

$total = ['8,999,999'];

It will create this operation:

['8,999,999'] / 100

Which means you want to divide an array by 100. PHP does not support dividing arrays by integers, hence it gives the error. So when you try to divide an array by an integer, I have to tell you the truth and say that this is not possible with PHP.

All operators PHP support for arrays are on this website:

If on the other hand you're concerned to first convert the value in the array into an integer number, then you need to add a convertion function to the operation:

$converter($total) / 100  # 89999.99

Where $converter is a function that parses the array:

$total = ['8,999,999'];

$converter = function (array $input) {
    $string = reset($input);
    $formatter = new NumberFormatter('en_GB', NumberFormatter::DECIMAL);
    return $formatter->parse($string, NumberFormatter::TYPE_INT32);
};

var_dump($converter($total) / 100); # double(89999.99)

Hope this helps.

all the above are right but try also this :

if (isset($_POST['percent'])) {
    $percent=$_POST['percent'];
    $total='8999999';

    /*calculation for discounted price */ 

    $discount_value= ($total / 100) *$percent;

    $final_price = $total - $discount_value;

    echo $final_price;
}

not only the $total was wrong but you should verify is the form was submited before doing the calculations.

Why are you using this : $total=['8,999,999']; ? This is an error

Use it This way : $total= 8999999;

Solution To Discount is : Just an example:

   <?php
        $percent = 10;
        $total   = 1000;

        $discount_value= ($total / 100) *$percent;

        echo  $final_price = $total - $discount_value;
   ?>

NOTE: This version is only a suggestion to check for a numerical value.

If a numerical value is entered in the form field, it will show the new price afterwards.

Yet, if the field contains anything other than numbers, it will revert back to the original price.

I also noticed your <link rel="stylesheet" type="css/text" href="style.css" in your original code was incomplete.

It should have read as <link rel="stylesheet" type="css/text" href="style.css"> You left out the closing "greater than symbol" => >

Added the following code:

if (is_numeric ($_POST['percent'])) 
 {

 echo "<center>";
 echo "Your new price is: $" . $final_price;
 echo "</center>";
 } else {
 echo "<center>";
 echo "Please enter your promo code. <br>The current price is: $" . $final_price;
 echo "</center>";
 }

Please feel free to use it at your disposal. (I used a more realistic number for the price of the car).

<!DOCTYPE html>
<html>
<head>
<title> Car Shop Cars! </title>
<link rel="stylesheet" type="css/text" href="style.css" />
</head>

<body>

<div id="header"> 
</div>

<div id="carpic">
</div>

<div id="price">
<center> This is the Nissan 350z and costs $35,000.00<br>
</center>
</div>

<div id="form">
<form action="index.php" method="post">

<center> <input type="text" name="percent" id="percent" />
<input type="submit"  /> </center>

</form> 
<?php

$percent=$_POST['percent'];
$total = 35000;

    /*calculation for discounted price */ 

    $discount_value= ($total / 100) *$percent;

    // I added this below
    $final_price = $total - $discount_value;

    $final_price = number_format($final_price);

    number_format($final_price, 2, '.', '');


if (is_numeric ($_POST['percent'])) 
 {

 echo "<center>";
 echo "Your new price is: $" . $final_price;
 echo "</center>";
 } else {
 echo "<center>";
 echo "Please enter your promo code. <br>The current price is: $" . $final_price;
 echo "</center>";
 }

?> 
</div>

</body> 
</html>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top