user636096
user636096

Reputation: 19

Pulling in a number from MySQL database & WordPress custom field and multiplying/dividing them in PHP

I'm currently trying to pull in two numbers (1 from a MySQL Database and another from a WordPress custom field) and multiply them.

E.g: <?php echo ($data['price'] / 2) * NUMBER-FROM-DATABASE ;?>

Can I pull in from the Database in a normal way and place the code in this formula or do I have to do something else with it?

Thanks in advance,

Barry

Upvotes: 1

Views: 259

Answers (1)

Shad
Shad

Reputation: 15461

What you wrote would work~ I would recommend:

<?php echo '$'.number_format(($data['price']/2) * $row['number_from_db'],2)?>

If you are concerned the value from DB might not be a int/float and could cause an error, type cast it:

<?php echo '$'.number_format(($data['price']/2) * (int)$row['number_from_db'],2)?>

Upvotes: 1

Related Questions