Ahtsham Ul Haq
Ahtsham Ul Haq

Reputation: 51

How to get value after subtraction in PHP & MYSQL

I am creating a bakery software using PHP and MYSQL (mysqli version).I want to know how I get the subtracted value from two columns eg. "Price - Pay = Remaining". My problem is just fetching the subtracted data to the table from those two columns.

<?php
$minus = mysqli_query($connection, "SELECT * FROM managment WHERE price - pay");
$sub = mysqli_fetch_assoc($minus);
?>

<tbody>
          <?php while ($row = mysqli_fetch_assoc($result)) { ?>
          <tr>
            <td></td>
            <td><?php echo $row['product']; ?></td>
            <td><?php echo $row['measure']; ?></td>
            <td><?php echo $row['value']; ?></td>
            <td><?php echo $row['cost']; ?></td>
            <td><?php echo $row['price']; ?></td>
            <td><?php echo $row['pay']; ?></td>
            <td><?php echo $sub['remaining']; ?></td>
          </tr>
         <?php } ?>

        </tbody>

What should i do...?

Upvotes: 1

Views: 5040

Answers (3)

Ajay
Ajay

Reputation: 41

Please simple use code like this :-

<?php
$results = mysqli_query($connection, "SELECT *, (price - pay) AS remaining FROM managment");
?>    
<tbody>
          <?php while ($row = mysqli_fetch_assoc($results)) { ?>
          <tr>
            <td></td>
            <td><?php echo $row['product']; ?></td>
            <td><?php echo $row['measure']; ?></td>
            <td><?php echo $row['value']; ?></td>
            <td><?php echo $row['cost']; ?></td>
            <td><?php echo $row['price']; ?></td>
            <td><?php echo $row['pay']; ?></td>
            <td><?php echo $row['remaining']; ?></td>
          </tr>
         <?php } ?>

        </tbody>

Upvotes: 0

pr1nc3
pr1nc3

Reputation: 8338

<td><?php echo ($row['price'] - $row['pay]); ?></td>

Why don't you go for a simple solution like this? I guess price and pay are numbers so this will work fine for your problem.

In my sql you can do this like :

SELECT *, price-pay AS total From managment;

You will see that a new column will be "created" with the name total and it's values will be the subtract you are asking.

After that you just call it inside html like:

<td><?php echo $row['total']; ?></td>

Upvotes: 1

ThataL
ThataL

Reputation: 165

SELECT *, (price - pay) as remaining FROM managment

you need to change the query. try this query and fetch data remaining data as below.

<?php echo $row['remaining']; ?>

or you can also subtract remaining as @pr1nc3 posted. above.

Upvotes: 0

Related Questions