Function help

I have created a function where the query is to get details daily monthly and yearly. The data is displayed correctly within the file, I need to copy it to a $_SESSION as this data will be used in other files. The data is not being copied and works only in that file. Help please

function sharecal($date_start,$date_end) {
    $profit=0;
          $conn = mysqli_connect('localhost', 'root', '12May@61', 'investments');
$query="SELECT * from sale WHERE sale_date BETWEEN '$date_start' AND '$date_end'";

$result = mysqli_query($conn, $query) ;
while ($row = mysqli_fetch_assoc($result)) {
    $id=$row['id_holding'];
    $sale_qty=$row['qty_sold'];
        $query_buy="SELECT * FROM buy WHERE id_holding=$id";
        $result_buy = mysqli_query($conn, $query_buy) ;         
            while ($row_buy = mysqli_fetch_assoc($result_buy)) {
                $profit+=($row['sale_rate']-$row_buy['buy_rate'])*$sale_qty;
        }
}
            echo $date_start ."--".$date_end . "Rs.: ";
            echo $profit;
            }


echo "Daily";
$date_start=$date_end=date("Y-m-d");
sharecal($date_start,$date_end);
echo $profit;  (does not work)
$_SESSION['daily']=$profit; (does not work)

echo "Monthly";
$date_start=date("$year-$month-01");
$date_end=date("$year-$month-31");
//echo $date_start.$date_end."<br>";
sharecal($date_start,$date_end);
echo "<br>";




If you need to use this this “data” for more than one process then using the function the way you are doing it is redundant and pointless. You really shouldn’t need to use sessions but call for the data that is needed then calculate the results (profit). Hint that would be a great place for a function.

You could split retrieving the data from a database into a function or calculating the profit into another function. I would look into using JavaScript/JSON as that would be a great place for it and you wouldn’t need a function at all but a separate file (or even on the same page if warranted).

Your function isn’t returning a value
So there is nothing to store in the session array
So either move the session assignments k
into the function or have the function return a valur

Sponsor our Newsletter | Privacy Policy | Terms of Service