Click here to Skip to main content
15,847,263 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to allow users to insert a number/quantity in the available input box that will then show/calculate a value/ answer based on data values from the database.

Eg: If they choose corn in the checkbox they will receive the nutritional value of that chosen food. They will then input their amount/quantity of that product and it will spit out it has x amount of fat, sodium, etc.

This is the output.php:
require 'includes/';
include_once 'header.php';
require 'includes/';


<div class="container-fluid">
    <div class="row justify-content-center">
        <div class="col-md-8">


            if (isset($_SESSION['status'])) {
                echo "<h4>" . $_SESSION['status'] . "</h4>";
            <div class="card mt-5">
                <div class="card-header">
                    <h4>High Energy, Low Fiber Sources:</h4>
                <div class="card-body">

                    <form action="includes/" method="POST">

                        $ingredient_query = "SELECT * FROM high_energy";
                        $query_run = mysqli_query($conn, $ingredient_query);

                        if (mysqli_num_rows($query_run) > 0) {
                            foreach ($query_run as $row) {
                                <input type="checkbox" name="ingredientlist[]" value="<?= $row['ingredients_name']; ?>" />
                                <?= $row['ingredients_name']; ?> <br />
                        <div class="form-group mt-3">
                            <button name="save_multicheckbox" class="btn btn-primary">Submit</button>


This is the includes for the output.php
require  '';

// session

<table class="table table-bordered table-sm">
    <thead class="">

            <td>Dry Matter</td>
            <td>Crude Protein</td>
            <td>Crude Fiber</td>
            <td>Ether Extract Fat</td>
            <td>MEn growing pig</td>
            <td>Energy digestibility, growing pig</td>
            <td>Nitrogen digestibility, growing pig</td>


    if (isset($_POST['save_multicheckbox'])) {
        $ingredientlist = $_POST['ingredientlist'];

        foreach ($ingredientlist as $ingredientitem) {
            $query = "SELECT * FROM high_energy WHERE ingredients_name='$ingredientitem';";
            $query_run = mysqli_query($conn, $query);

            // $query_run = mysqli_query($conn, $query);

            if (mysqli_num_rows($query_run) > 0) {
                foreach ($query_run as $row) {

                            <!-- <td><?= $row['ingredients_id']; ?></td> -->
                            <td><?= $row['ingredients_name']; ?></td>
                            <td><?= $row['dry_matter']; ?></td>
                            <td><?= $row['crude_protein']; ?></td>
                            <td><?= $row['crude_fibre']; ?></td>
                            <td><?= $row['ether_extract_Fat']; ?></td>
                            <td><?= $row['calcium']; ?></td>
                            <td><?= $row['phosphorus']; ?></td>
                            <td><?= $row['lysine']; ?></td>
                            <td><?= $row['methionine']; ?></td>
                            <td><?= $row['cystine']; ?></td>
                            <td><?= $row['tryptophan']; ?></td>
                            <td><?= $row['MEn_growing_pig']; ?></td>
                            <td><?= $row['energy_digestibility_gp']; ?></td>
                            <td><?= $row['nitrogen_digestibility_gp']; ?></td>



What I have tried:

I have used switch statements but since I am new to PHP it is very hard to even find anyone who has done something like this.

<pre>$number = $_POST['number'];
    // $Number2 = $_POST['Number2'];
    $operator = $_POST['operator'];
    $Result = '';
    if (is_numeric($number) && is_numeric($query_run)) {
        switch ($operator) {
            case "Add":
                $ResultByT4Tutorials = $number + $query_run;
            case "Subtract":
                $ResultByT4Tutorials = $number - $query_run;
            case "Multiply":
                $ResultByT4Tutorials = $number * $query_run;
            case "Divide":
                $ResultByT4Tutorials = $number / $query_run;

I dont know what to do.
Richard MacCutchan 23-Nov-22 11:20am    
What exactly is the problem?
Cassandra Royalty 23-Nov-22 11:55am    
I want to be able to have an input box where you enter a number/quantity then that number is added/dived/multiplied/subtracted against the database number that was selected. However, I don't even know where to start with it. What would be a good function to use? Are there any built-in functions? I have been searching through documents and watching videos since last week Wednesday and have found nothing.
Richard MacCutchan 23-Nov-22 12:00pm    
You can learn about input types in HTML at HTML Input Types[^]. So you need your form to post the content of the box plus any other information as required. You then use that to find specific values from your database which you use in the calculations. But if you really don't understand these basic concepts you should work through some proper tutorials.
Cassandra Royalty 23-Nov-22 12:07pm    
Thank you I will look into it
Cassandra Royalty 23-Nov-22 12:10pm    
How would you specify the data needed? I have just started working with MySQL and am out of my zone. Would making a form that takes the user's input and the previously selected checkbox values and then adds those together using a query work?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900