I was using the following code repeatedly
$query="SELECT * FROM holdings WHERE type='14'";
$result = mysqli_query($conn, $query) or die('Unable to shares'.mysqli_error($conn));
while ($row = mysqli_fetch_assoc($result)) {
$code=$row['name'];
$id=$row['id_holding'];
$query_buy="SELECT * FROM buy WHERE id_holding=$id";
$result_buy = mysqli_query($conn, $query_buy) or die('Unable to shares'.mysqli_error($conn));
while ($row_buy = mysqli_fetch_assoc($result_buy)) {
$buy_qty=$row_buy['buy_qty'];
$buy_rate=$row_buy['buy_rate'];
$buy_total=$buy_rate*$buy_qty;
$n++;
echo "<table><td>$n.$code.</td>";
echo "<td>".number_format($buy_qty,0)."</td>";
echo "<td style='text-align: right;'>".number_format($buy_rate,2)."</td>";
echo "<td style='text-align: right;'>".number_format(($buy_total),2)."</td>";
$buy_total_share+=$buy_total;
echo "<td>".$buy_total_share."</table>";
}
}
so I made it a function as follows
function buyrate($id) {
$conn = mysqli_connect('localhost', 'root', '12May@61', 'investments');
$query_buy="SELECT * FROM buy WHERE id_holding=$id";
$result_buy = mysqli_query($conn, $query_buy) or die('Unable to shares'.mysqli_error($conn));
while ($row_buy = mysqli_fetch_assoc($result_buy)) {
$buy_qty=$row_buy['buy_qty'];
$buy_rate=$row_buy['buy_rate'];
$buy_total=$buy_rate*$buy_qty;
echo "<td><center>".number_format($buy_qty,0)."</td>";
echo "<td style='text-align: right;'>".number_format($buy_rate,2)."</td>";
echo "<td style='text-align: right;'>".number_format(($buy_total),2)."</td>";
$buy_total_share+=$buy_total;
$_SESSION['buy_total_share']=$buy_total_share;
}
the only thing that is not working is “$buy_total_share+=$buy_total;”
in the first instance, it does the totaling correctly bu as a function It simply returns the last value!
also I have to add the $conn statement in every function and the include config.php does not work!
Help Please