Home > database >  IF statement in span style PHP
IF statement in span style PHP

Time:06-27

I'm trying to call a field of type int and display it on my website, but why when I enter the IF process into style span is not show?

this is code I'm trying to display a price field of type integer and display it with an icon

<a href="#"><i ></i>
     <span>
     (Rp.<?php if($count_balance->rowCount()>0) $count_balance->fetch(PDO::FETCH_ASSOC); ?>)
     </span>
     </a>

variable $count_balance is obtained from here

 $count_balance = $conn->prepare("SELECT * FROM `balance` WHERE user_id = ?");
 $count_balance->execute([$user_id]);

so what I'm expecting is, the price appears from database like converting an integer to a string so that it can be displayed, but when I use strval() to convert it still doesn't work

CodePudding user response:

This line of your code tests if row count is bigger then 0 and do nothing.

(Rp.<?php if($count_balance->rowCount()>0) $count_balance->fetch(PDO::FETCH_ASSOC); ?>)

You should echo/print the value as below:

(Rp.
<?php 
if($count_balance->rowCount()>0){
 $RpValue=$count_balance->fetch(PDO::FETCH_ASSOC);
 echo $RpValue['your_desired_field'];
}
?>)

I recommend you to have a look on number_format() function also, it helps you to handle decimal houses/currency.

  • Related