- 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);
}