0
  • Laravel 5.7

I have a function which change the length of decimal numbers based on the name. I need to use that function 9 times. That's why I'm trying to call it, but I'm getting "Undefined index stop" error.

function makeDecimals()
{
    global $input, $decimal;
    $datas = ["entry", "stop", "target"];

    foreach ($datas as $data) {

        if (strlen($input[$data]) > 0) { //This is the line that throw the error.

            $input[$data] = str_replace(',', '.', $input[$data]);
            $dataArray = explode(".", $input[$data]);

            if (count($dataArray) == 1) {
                $decimalPart = null;
            } else {
                $decimalPart = $dataArray[1];
            }

            if ($decimalPart != null) {
                $decimalLength = strlen($decimalPart);

                if ($decimalLength < $decimal) {
                    for ($i = $decimalLength; $i < $decimal; $i++) {
                        $input[$data] = $input[$data] . 0;
                    }
                }
                if ($decimalLength > $decimal) {
                    for ($i = $decimalLength; $i > $decimal; $i--) {
                        $input[$data] = substr($input[$data], 0, -1);
                    }
                }
            } else {
                $input[$data] = $input[$data] . '.';
                for ($i = 0; $i < $decimal; $i++) {
                    $input[$data] = $input[$data] . 0;
                }
            }
        } else {
            $input[$data] = null;
        }
    }
}

I'm calling the function above inside of store and update functions in Laravel. For instance:

public function update(Request $request, $id)
{
    $transaction = Trade::findOrFail($id);
    $input = $request->all();
    $assetName = $input['asset'];


    if (strpos($assetName, 'XAU') !== false || strpos($assetName, 'OIL') !== false) {
        $decimal = 2;
    } elseif (strpos($assetName, 'JPY') !== false || strpos($assetName, 'XAG') !== false) {
        $decimal = 3;
    } else {
        $decimal = 5;
    }

    $this->makeDecimals();

    $transaction->update($input);
}

If I make the following changes on the first function it works, but this is not what I want to do.

  • Remove foreach loop
  • Instead of using $input[$data] use $input['entry'], $input['stop'], and $input['target'] separately.
  • Instead of calling a function several times paste the code blocks with different $input['...'].

BTW, I tried using $input['entry'] instead of $input[$data] inside of the makeDecimals function. It didn't give any error, but function didn't do what it should do.

EDIT:

Because of the answers I got, I think I should put this part as well. As I said before, instead of calling function, if I use code block directly, it works. For instance if I change update function to that, it works.

public function update(Request $request, $id)
{
    // The codes down below are the same as my original update function

    $transaction = Trade::findOrFail($id);
    $input = $request->all();
    $assetName = $input['asset'];


    if (strpos($assetName, 'XAU') !== false || strpos($assetName, 'OIL') !== false) {
        $decimal = 2;
    } elseif (strpos($assetName, 'JPY') !== false || strpos($assetName, 'XAG') !== false) {
        $decimal = 3;
    } else {
        $decimal = 5;
    }

    // The end of the same codes.


    // I removed $this->makeDecimals() and paste the codes down below.
    // The differences are:
    // • There is no foreach loop.
    // • I used  $input['entry'] instead of $input[$data]
    // When I duplicate those codes and use 'stop' instead of 'entry', it also works.

    if ($input['entry']) {

        $input['entry'] = str_replace(',', '.', $input['entry']);
        $entryArray = explode(".", $input['entry']);

        if (count($entryArray) == 1) {
            $decimalPart = null;
        } else {
            $decimalPart = $entryArray[1];
        }

        if ($decimalPart != null) {
            $decimalLength = strlen($decimalPart);

            if ($decimalLength < $decimal) {
                for ($i = $decimalLength; $i < $decimal; $i++) {
                    $input['entry'] = $input['entry'] . 0;
                }
            }
            if ($decimalLength > $decimal) {
                for ($i = $decimalLength; $i > $decimal; $i--) {
                    $input['entry'] = substr($input['entry'], 0, -1);
                }
            }
        } else {
            $input['entry'] = $input['entry'] . '.';

            for ($i = 0; $i < $decimal; $i++) {
                $input['entry'] = $input['entry'] . 0;
            }
        }

        $transaction->update($input);
    }
Bulent
  • 3,307
  • 1
  • 14
  • 22

2 Answers2

0

The error is clear, "stop" doesn't exist in your input. A quick check fixes that.

    if (strlen($input[$data]) > 0) { //This is the line that throw the error.

to:

    if (isset($input[$data]) && strlen($input[$data]) > 0) { 
Darryl E. Clarke
  • 7,537
  • 3
  • 26
  • 34
  • Thanks, but I know that stop exists because when I use $input['stop'] it works perfectly fine. – Bulent Sep 18 '19 at 16:07
0

For what I understood your function function makeDecimals() is just number_format from php plus the str_replace.

You can use it like this

$number = $request->input('entry');
$formatedNumber = number_format(str_replace(',', '.', $number), $decimal, '.');
N69S
  • 16,110
  • 3
  • 22
  • 36
  • Thanks but my problem is not to format the numbers. I can't make my function work when I call it. But the code block works when I use constants instead of $data variable. – Bulent Sep 18 '19 at 16:16
  • @BülentAkgül the make a `try {...} catch()` around the `foreach()` if an exception happen, just dump `$datas` to check what's wrong with it with `dd($datas, $data)` – N69S Sep 18 '19 at 16:19