# Function help

**URL:** <https://www.phphelp.com/t/function-help/36095>\
**Category:** Uncategorized\
**Created:** [April 3, 2024, 8:31am UTC](https://www.phphelp.com/t/function-help/36095 "2024-04-03T08:31:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dmanghani](https://www.phphelp.com/letter_avatar_proxy/v4/letter/d/e9c0ed/32.png) [@dmanghani](https://www.phphelp.com/u/dmanghani)\
**Post date:** [April 3, 2024, 8:31am UTC](https://www.phphelp.com/t/function-help/36095/1 "2024-04-03T08:31:20Z")

</div>

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

```auto
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>";

```

---

<div class="post-metadata">

**Author:** ![Strider64](https://www.phphelp.com/user_avatar/www.phphelp.com/strider64/32/3161_2.png) [@Strider64](https://www.phphelp.com/u/Strider64)\
**Post date:** [April 3, 2024, 11:12am UTC](https://www.phphelp.com/t/function-help/36095/2 "2024-04-03T11:12:31Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![Laffin](https://www.phphelp.com/user_avatar/www.phphelp.com/laffin/32/232_2.png) [@Laffin](https://www.phphelp.com/u/Laffin)\
**Post date:** [April 8, 2024, 6:13pm UTC](https://www.phphelp.com/t/function-help/36095/3 "2024-04-08T18:13:08Z")

</div>

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
