Codelly
Codelly

Reputation: 61

PHP trying to calculate total of variables from an array

So I am doing the same grocery list I did before in JScript for an assignment but now in PHP. When I try to calculate the total it gives me a strange number.

$result is a calculation from product price multiplied by the quantity (waarde & aantal, its Dutch).

so when I do $total += $result;

And then I echo that, I get a very strange result.

To me it looks like it does not go by all the results. I tried using the $i index for it. But that does not work. What am I missing?

(Very new to PHP just learned a bit of the basic of JavaScript)

The expected outcome is all the totals shown in the table added together to create the total: 15.76

The result now is 31.984.85.98, what even is that magical number? Might be that I do something wrong with the number format, looking into that now too. (Also is it normal to share the code as I did? Apparently it's not really for PHP, I guess because it being a server-side thing) I have a CodePen of the original JavaScript version of it : https://codepen.io/3lly/pen/oNxaPKg maybe for clear view, you can see what I mean. The totals of all the total column cells.

table {
     
    margin-top: 20px;
    display: inline-block;
    
}
 
th, td , input {
    border: 2px solid #FFB000;
    padding: 2px;
    color: black;

}

body {
    text-align: center;
  
}
h1 {
    color: #FFB000;
    font-family: impact;
    text-shadow: -1px 0 black, 0 1px black, 1px 0 black, 0 -1px black;
}
p {
    color: white;
}
 
div {
    display: inline-block;
}
 
#add {
    display: inline-block;
    padding: 10px 20px;
    background: orange;
    box-shadow: -3px 3px black, -2px 2px black, -1px 1px black;
    border: 1px solid orange;

}

#add:hover {
    background: green;
    color: white;
}

input[type="text"], input[type="number"] {
    background-color: skyblue;
}

input[type="number"]:hover {
    background-color: black;
  }

#totaal {
    border: 2px solid #FFB000;
    background-color: #282828;
    padding: 5px;
    color: skyblue;
    
}

#totaal:hover{
    background-color: green;
    color: white;
}
<head>
<link rel="stylesheet" href="style.css">  
    <title>Boodschappenlijst</title>
</head>
<body>
    <h1> Boodschappenlijst </h1>
    <div id="container"></div>
    <table>
        <tr>
        <?php
        $headerTexts = ['Name', 'Prijs', 'Aantal', 'Totaal'];
        for($i=0;$i<count($headerTexts);$i++) {    
            echo "<th>" . $headerTexts[$i] . "</th>";   
        }
        ?>
        </tr>
        <?php  
     
            for($i=0;$i<count($products);$i++) {
                //result calculations
                $total = 0;
                 $result = number_format($products[$i]['waarde'] * $products[$i]['aantal'],2);
                 $total += $result;
                 echo $total;
                 //echo "<pre>" . is_int($products['waarde']) .  "</pre>";
                //Table Rows
                echo    "<tr>";
                echo    "<td>" . $products[$i]['omschrijving']  . "</td>" . 
                        "<td>" . $products[$i]['waarde'] . "</td>" . 
                        "<td>" . $products[$i]['aantal'] . "</td>" . 
                        "<td>" . $result . "</td>";
                echo    "</tr>";                  
        }   

        ?>
        </tr>
    </table>
    <p>Naam</p>
    <form>
        <input type="text" name="item" id="naam" /><br />
            <p>Aantal</p>
        <input type="text" name="quantity" id="qty" /><br />
            <p>Prijs</p>
        <input type="text" name="price" id="prijs" /><br/><br />
        <input type="button" value="Add Product" onclick="updateTable()" id="add"><br /><br />
    </form>
    <div id="totaal"></div>
    <!-- <script src="script.js"></script>-->
</body>
</html>

Upvotes: 0

Views: 160

Answers (1)

Codelly
Codelly

Reputation: 61

So I pushed the results of the total calculation of each products to the array which I did not do before like so :

 $products[$i]['total'] = $products[$i]['waarde'] * $products[$i]['aantal'];

Next I did the calculation like so:

 $total += $products[$i]['total'];

Within the loop. And that fixed it :D It was very simple apperently !

Upvotes: 1

Related Questions