Laravel controller won't pass parameter to service - php
so i made this input view which input some data, and then some services to process the data, these services are being called from the controller, the problem is when i try to call this particular function SUPMPFunc($index, $loop, $length) the controller seems to be not passing the arguments to the service.
This is the too few arguments error is get:
Controller:
<?php
class supmpController extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
public function Store(Request $request)
{
$medInput = new MedInput();
$deviceId = $medInput->MedInput();
$toolsInput = new ToolsInput();
$toolsRes = $toolsInput->ToolsInput(request("toolsIndex"));
$toolsfunctionInput = new ToolsFunctionInput();
$functionality = $toolsfunctionInput->ToolsFunctionInput(request("checkIndex"));
$temperature = new TempFunc();
$humidity = new HumidFUnc();
$condition = array(
$temperature->TempFunc(),
$humidity->HumidFUnc(),
);
$electricity = new Electricityinput();
$elInput = $electricity->ElectricityInput(request("electricIndex"));
$index = request("tablesupmpindex");
$loop = request("loopVal");
$length = request("supmpLength");
$SUPMP = new SUPMPFunc();
$result = $SUPMP->SUPMPFunc($index, $loop, $length);
$analysis = new AnalysisFunc();
$texttoShow = $analysis->AnalysisFunc();
$data = array (
array($deviceId, $texttoShow, $toolsRes, request("loopVal")), //0
$condition, //1
$functionality,//2
$elInput, //3
$result[0], //4
$result[1], //5
$result[2], //6
$result[3], //7
$result[4], //8
$result[5], //9
$result[6], //10
);
return view('laporan', ['data' => $data]);
}
}
Service:
<?php
class SUPMPFunc
{
public function SUPMPFunc($index, $loop, $length)
{
$Avg = new AvgFunc();
$TINV = new TINVFunc();
$round = new RoundFunc();
$stdev = new StdevFunc();
$budget = new BudgetFunc();
$drift = new DriftFunc();
$driftres = $drift->Drift1895040();
$medInput = new MedInput();
$deviceId = $medInput->MedInput();
$resolution = $deviceId[6];
///////////////////////////////////////////////////////////////////////////////
// everything below is related to calibration value
$settingsArray = [];
$valueArray = [];
for($i = 0; $i <= $length; $i++)
{
array_push($settingsArray, request("table".$index."setVal".$i));
}
$A1 = request('inVal_A-1');
$B1 = request('inVal_B-1');
$C1 = request('inVal_C-1');
$D1 = request('inVal_D-1');
$E1 = request('inVal_E-1');
$F1 = request('inVal_F-1');
$G1 = request('inVal_G-1');
$A2 = request('inVal_A-2');
$B2 = request('inVal_B-2');
$C2 = request('inVal_C-2');
$D2 = request('inVal_D-2');
$E2 = request('inVal_E-2');
$F2 = request('inVal_F-2');
$G2 = request('inVal_G-2');
$A3 = request('inVal_A-3');
$B3 = request('inVal_B-3');
$C3 = request('inVal_C-3');
$D3 = request('inVal_D-3');
$E3 = request('inVal_E-3');
$F3 = request('inVal_F-3');
$G3 = request('inVal_G-3');
$A4 = request('inVal_A-4');
$B4 = request('inVal_B-4');
$C4 = request('inVal_C-4');
$D4 = request('inVal_D-4');
$E4 = request('inVal_E-4');
$F4 = request('inVal_F-4');
$G4 = request('inVal_G-4');
$A5 = request('inVal_A-5');
$B5 = request('inVal_B-5');
$C5 = request('inVal_C-5');
$D5 = request('inVal_D-5');
$E5 = request('inVal_E-5');
$F5 = request('inVal_F-5');
$G5 = request('inVal_G-5');
$A6 = request('inVal_A-6');
$B6 = request('inVal_B-6');
$C6 = request('inVal_C-6');
$D6 = request('inVal_D-6');
$E6 = request('inVal_E-6');
$F6 = request('inVal_F-6');
$G6 = request('inVal_G-6');
$A7 = request('inVal_A-7');
$B7 = request('inVal_B-7');
$C7 = request('inVal_C-7');
$D7 = request('inVal_D-7');
$E7 = request('inVal_E-7');
$F7 = request('inVal_F-7');
$G7 = request('inVal_G-7');
$Aval = array(-$A1, -$A2, -$A3, -$A4, -$A5, -$A6, -$A7);
$Bval = array(-$B1, -$B2, -$B3, -$B4, -$B5, -$B6, -$B7);
$Dval = array(-$D1, -$D2, -$D3, -$D4, -$D5, -$D6, -$D7);
$Fval = array(-$F1, -$F2, -$F3, -$F4, -$F5, -$F6, -$F7);
$Cval = array(-$C1, -$C2, -$C3, -$C4, -$C5, -$C6, -$C7);
$Eval = array(-$E1, -$E2, -$E3, -$E4, -$E5, -$E6, -$E7);
$Gval = array(-$G1, -$G2, -$G3, -$G4, -$G5, -$G6, -$G7);
$koreksiStd = array(
array(0.0, 0.6, 0.7, 1.0, 0.9, 1.4, 1.3), //koreksistdUp
array(0.0, 0.7, 0.8, 1.1, 1.1, 1.7, 1.7), //koreksi tdDn
array(0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5), //Usertifikat up
array(0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5) //Usertifikat dn
);
$usertifikat = 0.5;
for($i = 0; $i < count($koreksiStd[0]); $i++ )
{
$Bval[$i] = $Bval[$i] + $koreksiStd[0][$i];
$Dval[$i] = $Dval[$i] + $koreksiStd[0][$i];
$Fval[$i] = $Fval[$i] + $koreksiStd[0][$i];
}
for($j = 0; $j < count($koreksiStd[1]); $j++ )
{
$Cval[$j] = $Cval[$j] + $koreksiStd[1][$j];
$Eval[$j] = $Eval[$j] + $koreksiStd[1][$j];
$Gval[$j] = $Gval[$j] + $koreksiStd[1][$j];
}
$valVi = 2;
$valVi2 = 50;
$valVi3 = 50;
$dividerD = 2 / sqrt(3);
$dividerA = 2 / sqrt(6);
$valURes = $resolution / $dividerA;
$stdSertifikat = [];
$UstdSertifikat = [];
$hysterisis = [];
for($i = 0; $i < count($koreksiStd[2]); $i++)
{
$stdSertifikat[$i] = $koreksiStd[2][$i];
array_push($UstdSertifikat, $stdSertifikat[$i] / 2);
}
for($i = 0; $i < count($Aval); $i++)
{
array_push($hysterisis, abs((($Cval[$i] - $Bval[$i])) +
($Eval[$i] - $Dval[$i]) +
($Gval[$i] - $Fval[$i])) / 3
);
}
$upAvg = [];
$dnAvg = [];
$upKoreksi = [];
$dnKoreksi = [];
$upStdev = [];
$dnStdev = [];
$upURepeat = [];
$dnURepeat = [];
$zeroErr = [];
$zeroErrU = [];
for($i = 0; $i < count($koreksiStd[0]); $i++)
{
$array = array(
$Bval[$i], $Dval[$i], $Fval[$i]
);
array_push($upAvg, $round->Roundto($Avg->Average($array), 1));
}
for($i = 0; $i < count($koreksiStd[0]); $i++)
{
$array = array(
$Cval[$i], $Eval[$i], $Gval[$i]
);
array_push($dnAvg, $round->Roundto($Avg->Average($array), 1));
}
for($i = 0; $i < count($Aval); $i++)
{
array_push($upKoreksi, $round->Roundto(($upAvg[$i] - $Aval[$i]), 1));
}
for($i = 0; $i < count($Aval); $i++)
{
array_push($dnKoreksi, $round->Roundto(($dnAvg[$i] - $Aval[$i]), 1));
}
for($i = 0; $i < count($Aval); $i++)
{
$array = array(
$Bval[$i], $Dval[$i], $Fval[$i]
);
array_push($upStdev, $stdev->Stdev($array));
}
for($i = 0; $i < count($Aval); $i++)
{
$array = array(
$Cval[$i], $Eval[$i], $Gval[$i]
);
array_push($dnStdev, $stdev->Stdev($array));
}
for($i = 0; $i < count($Aval); $i++)
{
array_push($upURepeat, $upStdev[$i] / (2 * sqrt(3)));
}
for($i = 0; $i < count($Aval); $i++)
{
array_push($dnURepeat, $dnStdev[$i] / (2 * sqrt(3)));
}
for($i = 0; $i < count($Aval); $i++)
{
array_push($zeroErr, max(abs($Cval[$i] - $Bval[$i]),
abs($Eval[$i] - $Dval[$i]),
abs($Gval[$i] - $Fval[$i])
));
}
//this one has the same value as u hysterisis column
for($i = 0; $i < count($Aval); $i++)
{
array_push($zeroErrU, $zeroErr[$i] / (2 * (pow(3, 0.5))));
}
$KANval = 7.5;
$UPbudgetua = [];
$UPbudgetRes = [];
$UPjumlahuicipow = [];
$UPjumlahuicivi = [];
$UPketidakpastianGab = [];
$UPkebebasanEff = [];
$UPfaktorCakup = [];
$UPketidakpastianBen = [];
$UPfinalRes = [];
for($i = 0; $i < 7; $i++)
{
array_push($UPbudgetua, $upStdev[$i], $zeroErr[$i], $resolution, 0, $stdSertifikat[$i], $hysterisis[$i], ((0.1 * $driftres[0][$i]) / 2));
$UPbudgetRes[$i] = $budget->Budget($UPbudgetua);
$UPjumlahuicipow[$i] = $round->Roundto(array_sum($UPbudgetRes[$i][6]), 3);
$UPjumlahuicivi[$i] = $round->Roundto(array_sum($UPbudgetRes[$i][7]), 3);
$UPketidakpastianGab[$i] = $round->Roundto(sqrt($UPjumlahuicipow[$i]), 3);
$UPkebebasanEff[$i] = $round->Roundto((pow($UPketidakpastianGab[$i], 4) / $UPjumlahuicivi[$i]), 3);
$UPfaktorCakup[$i] = $round->Roundto($TINV->TINV(0.05, $UPkebebasanEff[$i]), 3);
$UPketidakpastianBen[$i] = $round->Roundto($UPketidakpastianGab[$i] * $UPfaktorCakup[$i], 3);
if($UPketidakpastianBen[$i] <= $KANval)
{
$UPfinalRes[$i] = $KANval;
}
else
{
$UPfinalRes[$i] = $round->Roundto($UPketidakpastianBen[$i], 1);
}
array_splice($UPbudgetua, 0, count($UPbudgetua));
}
$DNbudgetua = [];
$DNbudgetRes = [];
$DNjumlahuicipow = [];
$DNjumlahuicivi = [];
$DNketidakpastianGab = [];
$DNkebebasanEff = [];
$DNfaktorCakup = [];
$DNketidakpastianBen = [];
$DNfinalRes = [];
for($i = 0; $i < 7; $i++)
{
array_push($DNbudgetua, $dnStdev[$i], $zeroErr[$i], $resolution, 0, $stdSertifikat[$i], $hysterisis[$i], ((0.1 * $driftres[0][$i]) / 2));
$DNbudgetRes[$i] = $budget->Budget($DNbudgetua);
$DNjumlahuicipow[$i] = $round->Roundto(array_sum($DNbudgetRes[$i][6]), 3);
$DNjumlahuicivi[$i] = $round->Roundto(array_sum($DNbudgetRes[$i][7]), 3);
$DNketidakpastianGab[$i] = $round->Roundto( sqrt($DNjumlahuicipow[$i]), 3);
$DNkebebasanEff[$i] = $round->Roundto((pow($DNketidakpastianGab[$i], 4) / $DNjumlahuicivi[$i]), 3);
// $DNfaktorCakDN[$i] = $round->Roundto($round->TINV(0.05, $DNkebebasanEff[$i]), 3);
$DNfaktorCakup[$i] = $round->Roundto($TINV->TINV(0.05, $DNkebebasanEff[$i]), 3);
$DNketidakpastianBen[$i] = $round->Roundto($DNketidakpastianGab[$i] * $DNfaktorCakup[$i], 3);
if($DNketidakpastianBen[$i] <= $KANval)
{
$DNfinalRes[$i] = $KANval;
}
else
{
$DNfinalRes[$i] = $round->Roundto($DNketidakpastianBen[$i], 1);
}
array_splice($DNbudgetua, 0, count($DNbudgetua));
}
$result = array(
$Aval,
$upAvg,
$dnAvg,
$upKoreksi,
$dnKoreksi,
$UPfinalRes,
$DNfinalRes,
$length,
);
return $result;
}
}
You need add parameters in constructor:
$index = request("tablesupmpindex");
$loop = request("loopVal");
$length = request("supmpLength");
$SUPMP = new SUPMPFunc($index, $loop, $length); <--edit
Related
Optimisation 3 array and foreach
Hi how can i optimize my code ? $time_start = microtime(true); $max = 2000; $vx = []; for ($i = 0; $i < $max; $i++) { $vx[$i] = $i; } $vy = []; for ($i = 0; $i < $max; $i++) { $vy[$i] = $i; } $z = []; $nb = 0; foreach ($vx as $kx=>$x) { foreach ($vy as $ky=>$y) { $z[$x][$y] = cos($x*$x)-sin($y*-2); $nb++; } } $time_end = microtime(true); echo ($time_end-$time_start).' microsec to process and calculate '.$nb.' z values'; And after this $time_start = microtime(true); $max = 2000; $vx = []; $vy = []; for ($i = 0; $i < $max; $i++) { $vx[$i] = $i; $vy[$i] = $i; } $z = []; $nb = 0; foreach ($vx as $kx=>$x) { foreach ($vy as $ky=>$y) { $z[$x][$y] = cos($x*$x)-sin($y*-2); $nb++; } } $time_end = microtime(true); echo ($time_end-$time_start).' microsec to process and calculate '.$nb.' z values';
The foreach loops are not necessary nor are the 2 arrays you build. It can all be done in 2 for loops like this $max = 10; $z = []; for ($i = 0; $i < $max; $i++) { for ($j = 0; $j < $max; $j++) { $z[$i][$j] = cos($i*$i)-sin($j*-2); } } print_r($z);
And after this $time_start = microtime(true); $max = 2000; $vx = []; $vy = []; for ($i = 0; $i < $max; $i++) { $vx[$i] = $i; $vy[$i] = $i; } $z = []; $nb = 0; for ($i = 0; $i < $max; $i++) { for ($j = 0; $j < $max; $j++) { $z[$i][$j] = cos($i*$i)-sin($j*-2); } } $time_end = microtime(true); echo ($time_end-$time_start).' microsec to process and calculate '.$nb.' z values';``` can i optimise the double foreach ?
PHP sorty array by column and slice it
I am trying to sort an array based on a column value: Example: $cats[0]['holders'] = 55; $cats[1]['holders'] = 66; $cats[2]['holders'] = 77; $cats[3]['holders'] = 23; $cats[4]['holders'] = 64; $cats[5]['holders'] = 82; $cats[6]['holders'] = -3; $cats[7]['holders'] = -5; $cats[8]['holders'] = -17; $cats[9]['holders'] = -25; $cats[10]['holders'] = 66; $cats[11]['holders'] = -10; $cats[12]['holders'] = 0; $cats[13]['holders'] = 5; $cats[14]['holders'] = 4; $cats[15]['holders'] = -3; function compareHolders($a, $b) { $aPoints = $a['holders']; $bPoints = $b['holders']; return strcmp($aPoints, $bPoints); } $cats = usort($cats, 'compareHolders'); I would like to get the top 5 values and bottom 5 values: $first_5_tokens = array_slice($tokens, 0, 5, true); print_r($first_5_tokens); $last_5_tokens = array_slice($tokens, -5); print_r($last_5_tokens); This is not sorting the array for me by "holder" value. How can I do this? Thanks!
Your comparison function sorts array in descending order and also, don't use it like - $cats = usort($cats, 'compareHolders'); Instead just use it like - usort($cats, 'compareHolders'); I changed your function and did it like this. $cats[0]['holders'] = 55; $cats[1]['holders'] = 66; $cats[2]['holders'] = 77; $cats[3]['holders'] = 23; $cats[4]['holders'] = 64; $cats[5]['holders'] = 82; $cats[6]['holders'] = -3; $cats[7]['holders'] = -5; $cats[8]['holders'] = -17; $cats[9]['holders'] = -25; $cats[10]['holders'] = 66; $cats[11]['holders'] = -10; $cats[12]['holders'] = 0; $cats[13]['holders'] = 5; $cats[14]['holders'] = 4; $cats[15]['holders'] = -3; foreach($cats as $cat) { echo $cat['holders'] . "<br>"; } // function compareHolders($a, $b) { // // $aPoints = $a['holders']; // $bPoints = $b['holders']; // // return strcmp($aPoints, $bPoints); // // } function compareHolders($a, $b) { $a = $a['holders']; $b = $b['holders']; if ($a == $b) return 0; return ($a > $b) ? -1 : 1; } usort($cats, 'compareHolders'); echo "<h4>After</h4>\n"; foreach($cats as $cat) { echo $cat['holders'] . "<br>"; } // print_r($cats); $tokens = $cats; echo "<h4>First Five</h4>\n"; $first_5_tokens = array_slice($tokens, 0, 5, true); print_r($first_5_tokens); echo "<h4>Last Five</h4>\n"; $last_5_tokens = array_slice($tokens, -5); print_r($last_5_tokens);
Making array after explode
I want to create an array from an input string. Before this code, I've tried explode, but the array remains length 1. Each string that I've tried is still one in array[0]. Here's my code so far: public function word() { $kata = array($this->kal->getHasil()); if (!empty($kata)) { $n = count($kata) for ($i = 0; $i < $n; $i++) { $imin = $i; for ($j = $i; $j < $n; $j++) { if ($kata[$j] < $kata[$imin]) { $imin = $j; } } $temp = $kata[$i]; $kata[$i] = $kata[$imin]; $kata[$imin] = $temp; } for ($i = 0; $i < $n; $i++) { echo "$kata[$i] "; } } } public function tokenize() { $temp = $this->kal->getHasil(); $token = explode(" ", $temp); return $token; } $hasil = $pp->tokenize(); for ($i = 0; $i < sizeof($hasil); $i++) { $st = new stemming(); $hasil[$i] = $pp->singkatan($hasil[$i]); $hasil[$i] = $st->stem($hasil[$i]); $hasil[$i] = $pp->stopWord($hasil[$i]); //echo "$hasil[$i] "; $hb = new hitungBobot($hasil[$i]); $hb->word(); } How would I fix this?
You can use a globar var, see the code: public function word(){ $kata = array($this->kal->getHasil()); global $output; if(!empty($kata)){ $ar= count($kata) $output += $ar; } public function tokenize() { $temp = $this->kal->getHasil(); $token = explode(" ",$temp); return $token; } $output = 0; $hasil = $pp->tokenize(); for($i=0; $i<sizeof($hasil); $i++) { $st = new stemming(); $hasil[$i] = $pp->singkatan($hasil[$i]); $hasil[$i] = $st->stem($hasil[$i]); $hasil[$i] = $pp->stopWord($hasil[$i]); //echo "$hasil[$i] "; $hb = new hitungBobot($hasil[$i]); $hb->word(); } echo $output;
php - permutation - possible number
already look around but cant find what i want for PHP. just say i have a number : 1234 ( can be splitted first into array ) and i want to get how many number combination possible for 2 digits, 3 digits , and 4 digits for example : possible 4 digits will be : 1234,1243,1324,1342, and so on. ( i dont know how many more ) possible 2 digits will be : 12,13,14,21,23,24,31,32,34,41,42,43 the closest one i get is : $p = permutate(array('1','2','3','4')); $result = array(); foreach($p as $perm) { $result[]=join("",$perm); } $result = array_unique($result); print join("|", $result); function permutate($elements, $perm = array(), &$permArray = array()){ if(empty($elements)){ array_push($permArray,$perm); return; } for($i=0;$i<=count($elements)-1;$i++){ array_push($perm,$elements[$i]); $tmp = $elements; array_splice($tmp,$i,1); permutate($tmp,$perm,$permArray); array_pop($perm); } return $permArray; } but how can i edit this so i can display for 3 and 2 digits ? Thanks
i got what i want it's from #mudasobwa link. and i edit to what i want. <?php $in = array(1,2,3,4,5,6); $te = power_perms($in); // print_r($te); $thou=0; $hun =0; $pu = 0; for($i=0;$i<count($te);$i++) { $jm = count($te[$i]); for($j=0;$j<$jm;$j++) { $hsl[$i] = $hsl[$i] . $te[$i][$j]; } if($hsl[$i] >=100 && $hsl[$i] < 1000 ) { $ratus[$hun] = intval($hsl[$i]); $hun = $hun + 1; } if($hsl[$i] <100 && $hsl[$i] >=10) { $pul[$pu] = intval($hsl[$i]); $pu = $pu + 1; } if($hsl[$i] >=1000 && $hsl[$i] < 10000) { $th[$thou] = intval($hsl[$i]); $thou = $thou + 1; } } $th=array_unique($th); $pul = array_unique($pul); $ratus = array_unique($ratus); sort($ratus); sort($pul); sort($th); print_r($th); function power_perms($arr) { $power_set = power_set($arr); $result = array(); foreach($power_set as $set) { $perms = perms($set); $result = array_merge($result,$perms); } return $result; } function power_set($in,$minLength = 1) { $count = count($in); $members = pow(2,$count); $return = array(); for ($i = 0; $i < $members; $i++) { $b = sprintf("%0".$count."b",$i); $out = array(); for ($j = 0; $j < $count; $j++) { if ($b{$j} == '1') $out[] = $in[$j]; } if (count($out) >= $minLength) { $return[] = $out; } } // usort($return,"cmp"); //can sort here by length return $return; } function factorial($int){ if($int < 2) { return 1; } for($f = 2; $int-1 > 1; $f *= $int--); return $f; } function perm($arr, $nth = null) { if ($nth === null) { return perms($arr); } $result = array(); $length = count($arr); while ($length--) { $f = factorial($length); $p = floor($nth / $f); $result[] = $arr[$p]; array_delete_by_key($arr, $p); $nth -= $p * $f; } $result = array_merge($result,$arr); return $result; } function perms($arr) { $p = array(); for ($i=0; $i < factorial(count($arr)); $i++) { $p[] = perm($arr, $i); } return $p; } function array_delete_by_key(&$array, $delete_key, $use_old_keys = FALSE) { unset($array[$delete_key]); if(!$use_old_keys) { $array = array_values($array); } return TRUE; } ?>
Sum the value of variable return by for loop in php
I have this code that is working perfectly. My question is how can I sum all the value of $manpowerCost and $otherCharges? for ($i = 1; $i<=9; $i++) { $m = '0'.$i; $sumMc = array(); $revenue = revenue($project, $year.'-'.$m); $manpowerCost = manpowerCost($project, $year.'-'.$m); $otherCharges = otherCharges($project, $year.'-'.$m); $netIncome = netIncome($revenue, $manpowerCost, $otherCharges); if ($manpowerCost<=0 && $m <= $projected_date){ $manpowerCost = round(($projected_mc * $percentage_mc),2); $projected_total_mc += $manpowerCost; $pt = $pt + $manpowerCost; } else { $projected_mc = $manpowerCost; $manpowerCost = $projected_mc; } if ($otherCharges<=0 && $m <= $projected_date){ $otherCharges = round(($projected_oc * $percentage_oc),2); $projected_total_oc += $otherCharges; $pt = $pt + $otherCharges; } else { $projected_oc = $otherCharges; $otherCharges = $projected_oc; } echo '<td>RE:'.number_format($revenue, 2, '.', '').'<br />MC:'.number_format($manpowerCost, 2, '.', '').'<br />OC:'.number_format($otherCharges, 2, '.', '').'<br />NI:'.$netIncome.'</td>'; }
U can Try like this $powercost_total = '0'; for ($i = 1; $i<=9; $i++) { $manpowerCost = manpowerCost($project, $year.'-'.$m); $powercost_total = $manpowerCost + 4powercost_total; ....
Declare $TotalManPowerCost and $TotalOtherCharges variables outside of the loop, and add $manpowercost and $otherCharges to them in each iteration of the loop