Method paginate does not exist Laravel 5.2 - php

I get the following error when I try to paginate in Laravel 5.2.
BadMethodCallException in Macroable.php line 81: Method paginate does
not exist.
And code:
Illuminate\Contracts\Pagination\Paginator;
...
$count = 0;
$gdprecords = Gdprecord::all();
foreach($gdprecords as $gdprecord) {
$count++;
$Pa = $gdprecord->adultpopulation;
$Pc = $gdprecord->childpopulation;
$P = $Pa + $Pc; // total population
$T = ($gdprecord->gdpcapita * $P) * $gdpratio; // basic income for country
if($bi_ratio == 0) {
$gdprecord->bi_adult = round($T / $Pa, 2);
} else {
$gdprecord->bi_adult = round($T / ($Pa + ($Pc / $bi_ratio)), 2);
}
$gdprecord->bi_adult_monthly = round($gdprecord->bi_adult / 12, 2);
$gdprecord->bi_adult_daily = round($gdprecord->bi_adult / 365, 2);
if($bi_ratio == 0) {
$gdprecord->bi_child = 0;
} else {
$gdprecord->bi_child = round($gdprecord->bi_adult / $bi_ratio, 2);
}
$gdprecord->bi_child_monthly = round($gdprecord->bi_child / 12, 2);
$gdprecord->bi_child_daily = round($gdprecord->bi_child / 365, 2);
}
$sorted_gdprecords = $gdprecords->sortByDesc('bi_adult')->paginate(25);
I don't want to paginate until after the array has been sorted. How do I make this work?
Thanks, Philip

Here is a solution I found, borrowing code from someone else:
public function paginate($items,$perPage)
{
$pageStart = \Request::get('page', 1);
// Start displaying items from this number;
$offSet = ($pageStart * $perPage) - $perPage;
// Get only the items you need using array_slice
$itemsForCurrentPage = array_slice($items, $offSet, $perPage, true);
return new LengthAwarePaginator($itemsForCurrentPage, count($items), $perPage,Paginator::resolveCurrentPage(), array('path' => Paginator::resolveCurrentPath()));
}
and add this code:
$gdprecords2 = $gdprecords->sortByDesc('bi_adult')->all();
$perPage = 25;
$sorted_gdprecords = self::paginate($gdprecords2, $perPage);
Thanks for your help. -Philip

Related

Abelian Sandpile Model in PHP

I am attempting to make an Abelian Sandpile Model in PHP. I am getting started with a possible solution, and it is working for three dimensional array but not working for 5 dimensional array. I am not sure why the following issue is occurring.
$pile = array(array(0,1,0),array(0,2,3),array(1,0,1));
Expected : 021, 110, 112
Actual result : 021, 110, 112
$pile = array(array(0,0,3,0,0),array(0,0,3,0,0),array(3,3,3,3,3),array(0,0,3,0,0),array(0,0,3,0,0));
Expected : 01010, 12321, 03030, 12321, 01010
Actual result : 01010, 12221, 02020, 12221, 01010
PHP Script:
<?php
//$pile = array(array(0,1,0),array(0,2,3),array(1,0,1));
//expected : 021, 110, 112
//$result = sandpile($pile, 2);
$pile = array(array(0,0,3,0,0),array(0,0,3,0,0),array(3,3,3,3,3),array(0,0,3,0,0),array(0,0,3,0,0));
//expected : 01010, 12321, 03030, 12321, 01010
$result = sandpile($pile, 5);
function sandpile($pile, $n)
{
$total_rows = count($pile);
$mid_x = (int) ($total_rows / 2);
$mid_y = (int) (count($pile[$mid_x]) / 2);
$arr_result = array_redistribute($pile, $mid_x, $mid_y, $n);
return $arr_result;
}
function array_redistribute($arr_input = array(), $x, $y, $n = 1)
{
$arr_input[$x][$y] = $arr_input[$x][$y] + $n;
// if grains in a cell reaches 4
if ($arr_input[$x][$y] > 3)
{
$arr_input[$x][$y] = 0;
// increment closest cell
if(isset($arr_input[$x-1][$y]))
$arr_input[$x-1][$y] = $arr_input[$x-1][$y] + 1;
if(isset($arr_input[$x+1][$y]))
$arr_input[$x+1][$y] = $arr_input[$x+1][$y] + 1;
if(isset($arr_input[$x ][$y-1]))
$arr_input[$x ][$y-1] = $arr_input[$x][$y-1] +1;
if(isset($arr_input[$x][$y+1]))
$arr_input[$x][$y+1] = $arr_input[$x][$y+1] + 1;
// if closest cell value > 3
if(isset($arr_input[$x-1][$y]) && $arr_input[$x-1][$y]>3)
{
$arr_input = array_redistribute($arr_input, $x-1, $y, 1);
}
if(isset($arr_input[$x+1][$y]) && $arr_input[$x+1][$y]>3)
{
$arr_input = array_redistribute($arr_input, $x+1, $y, 1);
}
if(isset($arr_input[$x][$y-1]) && $arr_input[$x][$y-1]>3)
{
$arr_input = array_redistribute($arr_input, $x, $y-1, 1);
}
if(isset($arr_input[$x ][$y+1]) && $arr_input[$x][$y+1]>3)
{
$arr_input = array_redistribute($arr_input, $x, $y+1, 1);
}
return $arr_input;
}
?>

85/5000 Asynchronous equation in PHP: a variable depends on another that has not yet been declared

I have a sum in my code that needs the return of a function, but the return of this function depends that the sum is already ready, because it uses a parameter that is still going to be created, can you solve it?
It's a calculator made in excel and I'm programming, but excel is that way and it works, but when I went to the code I got caught up in that problem.
//See the code:
public function valorRenda($rendaMensal, $valorEntrada, $tempoMeses, $imovelTipo){
$resultado = array();
$resultado['tipoSimulacao'] = 'rendaCliente';
$taxaMensal = $this->taxaPorTipo('rendaCliente', $imovelTipo, $rendaMensal) / 100;
//Cálculo do valor da primeira parcela
$primeiraParcela = $rendaMensal * 0.3;
//Calcula valor máximo do financiamento
$multiplicador = 0.5;
$i = 1;
for ($i = 1; $i <= 9999999999; $i++) {
$amortizacao = ($i * $multiplicador) / $tempoMeses;
$juros = (($i * $multiplicador) * ($taxaMensal));
$valor = $amortizacao + $juros + $this->custoMensal($valorImovel,$valorEntrada);
if ($valor >= $primeiraParcela) {
$projecao = ($i * $multiplicador) + $valorEntrada;
$valorImovel = ($projecao * $primeiraParcela / $valor);
break;
}
}
$financiamentoMaximo = round($valorImovel);
$financiamentoMaximo = $this->formatoReal($financiamentoMaximo);
$resultado['financiamentoMaximo'] = $financiamentoMaximo;
$resultado['financiamentoMaximoFormatado'] = $this->formatoReal($financiamentoMaximo, false);
//Calcula a primeira parcela
$primeiraParcelaSemTaxas = $primeiraParcela - $this->custoMensal($valorImovel,$valorEntrada);
$primeiraParcelaSemTaxas = round($primeiraParcelaSemTaxas, 2);
$primeiraParcelaSemTaxas = $this->formatoReal($primeiraParcelaSemTaxas);
$resultado['primeiraParcela'] = $primeiraParcelaSemTaxas;
$resultado['primeiraParcelaFormatado'] = $this->formatoReal($primeiraParcelaSemTaxas, false);
$primeiraParcelaTotal = $primeiraParcela;
$primeiraParcelaTotal = round($primeiraParcelaTotal, 2);
$primeiraParcelaTotal = $this->formatoReal($primeiraParcelaTotal);
$resultado['primeiraParcelaTotal'] = $primeiraParcelaTotal;
$resultado['primeiraParcelaTotalFormatado'] = $this->formatoReal($primeiraParcelaTotal, false);
$valorUltimaParcela = ((($valorImovel - $valorEntrada) - ((($valorImovel - $valorEntrada) / $tempoMeses) * ($tempoMeses - 1))) + ((($valorImovel - $valorEntrada) - ((($valorImovel - $valorEntrada) / $tempoMeses) * ($tempoMeses - 1)))) * $taxaMensal) + $this->custoMensal($valorImovel,$valorEntrada);
$ultimaParcelaSemTaxas = $valorUltimaParcela - $this->custoMensal($valorImovel,$valorEntrada);
$ultimaParcelaSemTaxas = round($ultimaParcelaSemTaxas, 2);
$ultimaParcelaSemTaxas = $this->formatoReal($ultimaParcelaSemTaxas);
$resultado['ultimaParcela'] = $ultimaParcelaSemTaxas;
$resultado['ultimaParcelaFormatado'] = $this->formatoReal($ultimaParcelaSemTaxas, false);
$primeiraParcelaTotal = $valorUltimaParcela;
$primeiraParcelaTotal = round($primeiraParcelaTotal, 2);
$primeiraParcelaTotal = $this->formatoReal($primeiraParcelaTotal);
$resultado['ultimaParcelaTotal'] = $primeiraParcelaTotal;
$resultado['ultimaParcelaTotalFormatado'] = $this->formatoReal($primeiraParcelaTotal, false);
$resultado['taxa'] = number_format(round(($taxaMensal * 100), 2), 2);
$resultado['periodo'] = $tempoMeses;
$this->incluiCustoMensal($resultado);
return $resultado;
}
//The function:
private function custoMensal($valorImovel, $entradaImovel)
{`enter code here`
$taxa_dfi = ($this->dfi / 100) * $valorImovel;
$taxa_mip = ($this->mip / 100) * ($valorImovel- $entradaImovel);
return $taxa_dfi + $taxa_mip + $this->tac;
}
Where is the problem:
$valor = $amortizacao + $juros + $this->custoMensal($valorImovel,$valorEntrada);
In this line I need to pass $valorImovelto the function custoMensal(), what happens is that the variable $valorImovel has not yet been created because it depends on the variable $valor.

Add parameters in result array

I'm using Code Igniter,I try to add $finalamount to the result_array.
$this->db->select('customer_id, customer_ref, i_points, group_points, created_on');
$run_q = $this->db->get('employees');
if($run_q->num_rows() > 0){
foreach ($run_q->result_array() as $get) {
$totalpoints = $get['i_points'] + $get['group_points'];
$percent = 17;
$amountbycustomer = $totalpoints * 26 * $percent;
$amountbypref = $this->final($get['customer_id'],$percent);
$finalamount = $amountbycustomer + $amountbypref;
$get['finalamount'] = $finalamount;
}
return $run_q->result_array();
}
But in my view, this error
<td class="employeeCustomer"><?=$get['customer_id']?></td>
<td class="employeeCustomerRef"><?=$get['customer_ref']?></td>
<td class="employeeAmount"><?=$get['finalamount']?></td>//line 65
Message: Undefined index: finalamount
Filename: employees/employeeslist.php
Line Number: 65
You are returning just the result array from the db and not actually adding the final amount to the individual rows. You have to do something like this:
public function stmt() {
$this->db->select('customer_id, customer_ref, i_points, group_points, created_on');
$run_q = $this->db->get('employees');
if ($run_q->num_rows() > 0) {
$rows = $run_q->result_array();
foreach ($rows as $k => $get) {
$totalpoints = $get['i_points'] + $get['group_points'];
$percent = 17;
$amountbycustomer = $totalpoints * 26 * $percent;
$amountbypref = $this->final($get['customer_id'], $percent);
$finalamount = $amountbycustomer + $amountbypref;
$rows[$k]['finalamount'] = $finalamount;
}
return $rows;
}
return false;
}
OR using pass by reference (notice: &get):
$rows = $run_q->result_array();
foreach ($run_q->result_array() as &$get) {
$totalpoints = $get['i_points'] + $get['group_points'];
$percent = 17;
$amountbycustomer = $totalpoints * 26 * $percent;
$amountbypref = $this->final($get['customer_id'], $percent);
$finalamount = $amountbycustomer + $amountbypref;
$get['finalamount'] = $finalamount;
}
return $rows;

Rewrite a PHP function with arrays instead

Is there any way I could rewrite this function with an array instead of all these if statements? Could i maybe use some for loop together with an array? How would that look like? Any suggestions of simpler code?
Here is my php function:
function to_next_level($point) {
/*
**********************************************************************
*
* This function check how much points user has achievents and how much procent it is until next level
*
**********************************************************************
*/
$firstlevel = "3000";
$secondlevel = "7000";
$thirdlevel = "15000";
$forthlevel = "28000";
$fifthlevel = "45000";
$sixthlevel = "80000";
if($point <= $firstlevel) {
$total = ($point/$firstlevel) * 100;
$remaining = round($total);
//echo number_format($remaining, 0, '.', ' ');
return $remaining;
} elseif ($point <= $secondlevel) {
$total = ($point/$secondlevel) * 100;
$remaining = round($total);
//echo number_format($remaining, 0, '.', ' ');
return $remaining;
} elseif ($point <= $thirdlevel) {
$total = ($point/$thirdlevel) * 100;
$remaining = round($total);
//echo number_format($remaining, 0, '.', ' ');
return $remaining;
} elseif ($point <= $forthlevel) {
$total = ($point/$forthlevel) * 100;
$remaining = round($total);
//echo number_format($remaining, 0, '.', ' ');
return $remaining;
} elseif ($point <= $fifthlevel) {
$total = ($point/$fifthlevel) * 100;
$remaining = round($total);
//echo number_format($remaining, 0, '.', ' ');
return $remaining;
} elseif ($point <= $sixthlevel) {
$total = ($point/$sixthlevel) * 100;
$remaining = round($total);
//echo number_format($remaining, 0, '.', ' ');
return $remaining;
}
}
Try this:
function to_next_level($point) {
/*
**********************************************************************
*
* This function check how much points user has achievents and how much procent it is until next level
*
**********************************************************************
*/
$levelArray = array(3000, 7000, 15000, 28000, 45000, 80000);
foreach ($levelArray as $level)
{
if ($point <= $level) {
$total = ($point/$level) * 100;
$remaining = round($total);
//echo number_format($remaining, 0, '.', ' ');
return $remaining;
}
}
}
Start using OOP programming style. This is the perfect opportunity since it is a task without much complexity. Create a class that acts as central authority. That class can receive more methods over time. That way your code stays easy to maintain since all those functions are kept inside a class.
<?php
class levelAuthority
{
public static $thresholds = [ 3000, 7000, 15000, 28000, 45000, 80000 ];
public static function getDistanceToNextlevel($points)
{
foreach (self::$thresholds as $threshold) {
if ($points <= $threshold) {
$total = ($points/$threshold) * 100;
$remaining = round($total);
return $remaining;
}
}
}
}
// in the calling scope:
$points = 5000;
echo levelAuthority::getDistanceToNextlevel($points);
lots of answers to this!!
here is mine using a while loop - single exit point outside the loop:
function to_next_level($point) {
/*
**********************************************************************
*
* This function check how much points user has achievements and how much percent it is until next level
*
**********************************************************************
*/
$arr_level = array(3000,15000,28000,45000,80000);
$remaining = false;
while (!$remaining and list($key,$level) = each($arr_level)) {
if ($point <= $level) {
$total = ($point/$level) * 100;
$remaining = round($total);
}
}
// will return false if $point is greater than highest value in $arr_level
return $remaining;
}
You could write an additional function, that does the calculations and trigger it from the if/else if/else blocks.
function calculate_remaining($points, $level) {
$total = ($point/$level) * 100;
$remaining = round($total);
return $remaining;
}
You'd trigger this like:
if($point <= $firstlevel) {
return $calculate_remaining($point, $firstlevel);
} elseif ($point <= $secondlevel) {
return $calculate_remaining($point, $secondlevel);
} etc.
What about something like this?
function to_next_level($point)
{
$levels = array(
3000,
7000,
15000,
28000,
45000,
80000
);
foreach ($levels as $level)
{
if ($point <= $level)
{
$total = ($point / $level) * 100;
$remaining = round($total);
//echo number_format($remaining, 0, '.', ' ');
return $remaining;
}
}
}
The point levels are in order in the array, so [0] is $firstlevel, and so on. You simply iterate through the array and return whenever we reach the condition where $point is <= to the the $level.
Also, since $levels is static, it can be defined outside of the function.
simple:
<?php
$point = 100;
$remaining = 0;
$data = [
'firstlevel' => 3000,
'secondlevel' => 7000,
'thirdlevel' => 15000,
'forthlevel' => 28000,
'fifthlevel' => 45000,
'sixthlevel' => 80000
];
foreach($data as $item)
{
if($point <= $item)
{
$remaining = round(($point / $item ) * 100); //or return val
}
}
How about putting your variable into array and loop it?
function to_next_level($point) {
$data[0] = "3000";
$data[1] = "7000";
$data[2] = "15000";
$data[3] = "28000";
$data[4] = "45000";
$data[5] = "80000";
foreach ($data as $key => $value) {
if($point <= $value) {
$total = ($point/$value) * 100;
$remaining = round($total);
return $remaining;
}
}
}
I haven't try it. But it might work for you.

else if not working in

if condition matches but else if condition is not working when i insert $total_salary = 10000 and $salary_type = NO.
if ($this->request->is('post')) {
$DATA = $this->data;
$employee_salary = $this->Salary->save($DATA);
$total_salary = $employee_salary['Salary']['salary'];
$salary_type = $employee_salary['Salary']['salary_in_ctc'];
echo $total_salary . $salary_type;
if (($total_salary > 15000) && ($salary_type === 'YES')) {
$pf_company = 1500;
$pf_employee = 1500;
$percent = 0.62;
$gross_salary = ($total_salary - $pf_company);
$base_salary = ($percent * $gross_salary);
$HRA = ($base_salary / 2);
$others = ($gross_salary - ($base_salary + $HRA));
$inhand_salary = ($gross_salary - $pf_employee);
} elseif (($total_salary < 15000) && ($salary_type === 'NO')) {
echo'hello';
$pf_company = 1200;
$pf_employee = 1200;
$percent = 0.62;
$gross_salary = ($total_salary - $pf_company);
$base_salary = ($percent * $gross_salary);
$HRA = ($base_salary / 2);
$others = ($gross_salary - ($base_salary + $HRA));
$inhand_salary = ($gross_salary - $pf_employee);
}
try to match the uppercase condition and also check for white space in you conditions.
strtoupper(trim($salary_type))
The possible second issue could be:
As you are checking the datatype also in the condition $salary_type ==='NO' and in the post you stated you are passing $salary_type = NO. which doesn't seems to be a string type.
Please try removing one = from condition or passing NO as a string to validate the condition

Categories