0

before start i wanna say, i have seen already all of the similar question but i didn't get answer for my problem and sorry if my question is duplicate

I want to calculate discount of product price in product details page but I've got title problem, this website is developed base on PHP mvc

this is my function in main model:

function caluclateDiscount($price,$discount){
$price_discount=((100-$discount)*$price)/100;
$total_price=$price-$price_discount;
return [$price_discount,$total_price];
}

in product model class:

function productInfo($id){
$sql="select * from tbl_product where id=:product";
$stmt=self::$conn->prepare($sql);
$stmt->bindParam(':product',$id);
$stmt->execute();
$result=$stmt->fetch();
$price=$result['price'];
$discount=$result['discount'];
$price_calculate=$this->caluclateDiscount($price,$discount);
$result['price_discount']=$price_calculate[0];
$result['total_price']=$price_calculate[1];
return $result;
}

in the product Controller:

function index($id){
    $productInfo=$this->model->productInfo($id);
    $data=['p_info'=>$productInfo];
    $this->view('product/index',$data);
}

and finally in price field in product page:

<span> <?= $special=$data['p_info']; $special['price_discount']; ?> </span>
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Mehrdad Hosseini
  • 357
  • 1
  • 4
  • 17
  • If `$discount` is in percentage in `caluclateDiscount($price,$discount)` function then formula to calculate `$price_discount` should be `$price*$discount/100` – lkdhruw Apr 13 '20 at 05:32
  • `$special=$data['p_info'];` is an array, you can't just print an array like that (with your `= ... ?>`), see the linked duplicate question. You have to print a specific array element instead. – Progman Apr 13 '20 at 09:21

0 Answers0