0

I have a php file that creates a table for me using data fetched from a mysql database.

<?php

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
      echo "<tr>";
      echo "<td>" . $row['ID'] . "</td>";
      echo "<td>" . $row['Name'] . "</td>";
      echo "<td>" . $row['CountryCode'] . "</td>";
      echo "<td>" . $row['District'] . "</td>";
      echo "<td>" . $row['Population'] . "</td>";
      echo "<td>" . $row['Price'] . "</td>";
      echo "<td><input type=\"number\" name="quantity" id="quantity"></td>";
      echo "</tr>";
    }
} else {
    echo "0 results";
}
$conn->close();

?>

At the end of each row, I have an input number so that I can calculate the amount of each product(countries in this case). How could I get the amount of each country, multiply it by the price and return a total at the end of my table?

6
  • 2
    please explain this sentence:- I have an input number so that I can calculate the amount of each product(countries in this case). How could I get the amount of each country, multiply it by the price and return a total at the end of my table? unable to understand Commented May 21, 2019 at 12:40
  • 3
    You can;t do that in php you will need to have JavaScript functions to get the values, store to totals and update the page totals. Commented May 21, 2019 at 12:41
  • 1
    @AlivetoDie At the end of my table, I have an input. The user can chose how many of each product they want. I then need to multiply the quantity by the price to get the total price of each product and then add all of the prices of the product to get the grand total at the end of my table Commented May 21, 2019 at 12:44
  • 1
    You need jquery/javascript for that my friend. Can't do that in php Commented May 21, 2019 at 12:48
  • 1
    @Louis you are already doing that by printing the data out to the client side. You may add some id or class properties to make the place where you printed out the values easier to access from js. Commented May 21, 2019 at 12:55

2 Answers 2

1

If you want to do it with PHP, you have to do it in two steps, because the PHP is processed in the server. So you would enter all the quantities you want and when you press the "Compute" button, the page would send all the values to the server who would compute everything you want and give you the final result.

A problem that I see with this solution is that once you have get all the quantities you want, you would have to get the prices once more. I don't its very efficient because you already have them on the first page. I see two solutions here :

  1. When you display the values, keep track of the prices in a $_SESSION field, thus you will be able to access them after leaving the page, without having to get them from the database.
  2. When you display the values, display the prices in an input field (I would recommend as readonly so that the user won't change this value here), and thus they would be included in the $_POST (or $_GET, if you use this) variable that your browser would send to the server.

If you're only interested to display the value, without using it after, you can use javascript to compute it, it might be easier.

Even though it's not the point here, you should be careful when displaying inputs via PHP on a while loop, because here all of your inputs have the same id, which would make thing harder to compute what you want to.

Hope it helps!

Sign up to request clarification or add additional context in comments.

Comments

1

You must add a form tag around the table and then sum record price * qty on every row

What I did here is I checked if the quantity is set in the POST variable or not. if it exists I put data in quantity value. after that, you must make the name of quantity capable of taking array inside with the key of record Id.

after that, if the form submitted the quantity will send back to the page as a query string. then I multiply the price with quantity and add it to sum varaible. after finishing the loop and echo out the sum variable at the end of the table.

$html = '';
if ($result->num_rows > 0) {
    $html .= '<form method="GET" action="" target="_self>';

    $row = $result->fetch_assoc();

    if(isset($_GET['quantity'][$row['ID']]))
        $qty = $_GET['quantity'][$row['ID']];
    else
        $qty = 0;

    $sum = 0;
    // output data of each row
    while ($row) {
        $html .= '<tr>';
        $html .= '<td>' . $row['ID'] . '</td>';
        $html .= '<td>' . $row['Name'] . '</td>';
        $html .= '<td>' . $row['CountryCode'] . '</td>';
        $html .= '<td>' . $row['District'] . '</td>';
        $html .= '<td>' . $row['Population'] . '</td>';
        $html .= '<td>' . $row['Price'] . '</td>';
        $html .= '<td>
                    <input type="number" name="quantity[' . $row['ID'] . ']"  value="' . $qty . '">
                  </td>';
        $html .= '<td>' . ($sum += $row['Price'] * $qty) . '</td>';
        $html .= '</tr>';
    }
    $html .= '<input name="submitForm" value="submitForm">';
    $html .= '</form>';
    $html .= '<tr>';
    $html .= '<td>Total = '.$sum.'</td>';
    $html .= '</tr>';
} else {
    $html .= "0 results";
}
$conn->close();

echo htmlspecialchars($html);

remember to use htmlspecialchars to echo out the result.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.