I write a simple code to generate EAN8 code and check did record exist in database. When i use just code it's working perfect but when i try to change transform it for a function i have only blank page.
This code working
do{
$number = rand(1,9999999);
$number = str_pad($number, 7, 0, STR_PAD_LEFT);
$sum = ($number[0] * 3) +($number[1] * 1) + ($number[2] * 3) + ($number[3] * 1) + ($number[4] * 3) + ($number[5] * 1) + ($number[6] * 3);
$control = 10 - ($sum%10);
$ean8 = $number.$control;
$ean8 = substr($ean8, 0,8);
$db->where('numberCard',$ean8);
$user = $db->getOne('user');
}
while($db->count !== 0);
echo $ean8;
and this same code past in function return blank page
function generateEan(){
do{
$number = rand(1,9999999);
$number = str_pad($number, 7, 0, STR_PAD_LEFT);
$sum = ($number[0] * 3) +($number[1] * 1) + ($number[2] * 3) + ($number[3] * 1) + ($number[4] * 3) + ($number[5] * 1) + ($number[6] * 3);
$control = 10 - ($sum%10);
$ean8 = $number.$control;
$ean8 = substr($ean8, 0,8);
$db->where('numberCard',$ean8);
$user = $db->getOne('user');
}
while($db->count !== 0);
return $ean8;
}
echo generateEan();
Where is the problem ? Where i have made mistake? Please, help me and have a nice night
inside the scope of generateEan function, exists $db ?
maybe you can do a simple function and test if it exists
Related
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.
I have found a PHP script github ISBN-Calc Routine to perform the ISBN-10 checksum calculation:
<?php
/**
* Calculate ISBN checksum
*
* #param string $isbn
* #return integer
*/
function isbn_checksum($isbn) {
$sum = 0; $isbn = str_split(preg_replace('/[^\d]/', '', $isbn));
foreach($isbn as $key => $z) {
if($key >= 12) break;
$sum += ($key % 2) ? $z * 3 : $z;
}
$checksum = (10 - $sum % 10);
return ($checksum == 10) ? 0 : $checksum;
}
But f.e for my ISBN-10: 0470173424 I get Checksum: 0with this github script.
Accoring to ISBN online checker the checksum should be 4 as is it in the ISBN. Can anyone here provide me with the correct PHP routine, please?
Thanks
That function is for calculating an ISBN-13 check digit, not ISBN-10 - that's why it breaks the loop after the 12th character.
The algorithm for ISBN-10 is different, and requires multiplying the first 9 digits of the number by 10 down to 2. The difference between that sum and the next multiple of 11 is the check-digit. For your example, this would be:
(10 * 0) +
(9 * 4) +
(8 * 7) +
(7 * 0) +
(6 * 1) +
(5 * 7) +
(4 * 3) +
(3 * 4) +
(2 * 2) = 161.
The next multiple of 11 is 165, so the check-digit should be 4 (as you say). In the case where the check-digit would be 10, X is used. We can model this in PHP like this:
function isbn10($isbn) {
$isbn = preg_replace('/[^\d]/', '', $isbn);
$digits = str_split(substr($isbn, 0, 9));
$sum = 0;
foreach ($digits as $index => $digit) {
$sum += (10 - $index) * $digit;
}
$check = 11 - ($sum % 11);
return ($check === 10) ? 'X' : $check;
}
echo isbn10('047017342');
4
You can see this working here: https://eval.in/1039654
The previously marked answer is close but incomplete.
Specifically this portion:
$check = 11 - ($sum % 11); // This can output 1,2,3,4,5,6,7,8,9,10,11 not 0
return ($check === 10) ? 'X' : $check; // This is incomplete does not address 11
The code does not deal with the situation where 11 - 0 = 11. I have tried to clarify it below.
function isbn10($isbn)
{
$isbn = preg_replace('/[^\d]/', '', $isbn);
$digits = str_split(substr($isbn, 0, 9));
$sum = 0;
foreach ($digits as $index => $digit)
{
$sum += (10 - $index) * $digit;
}
$check = 11 - ($sum % 11);
// $check may hold either 10 or 11, but not 0
// 10 becomes X, 11 becomes 0 -- output is 1 character only
if ($check == 10)
{
$check = 'X';
}
elseif ($check == 11)
{
$check = '0';
}
return $check;
}
An example ISBN where the earlier answer fails is 0134093410
There is an library from GitHub: https://github.com/Fale/isbn
There is a function called "Check":
Initialization:
$isbn = new Isbn\Isbn();
Check values: (Example)
$isbn->check->is10('888183718'); // Will return false
$isbn->check->is13('9788889527191'); // Will return true
$isbn->check->is13('978888952719'); // Will return false
You can download the library from the given link.
Maybe that helps a bit.
Have a nice weekend!
If you want to check if the ISBN-10 is correct
Validate ISBN-10
<?php
function isValidIsbn10($isbn) {
$check = 0;
for ($i = 0; $i < 10; $i++) {
if ('x' === strtolower($isbn[$i])) {
$check += 10 * (10 - $i);
} elseif (is_numeric($isbn[$i])) {
$check += (int)$isbn[$i] * (10 - $i);
} else {
return false;
}
}
return (0 === ($check % 11)) ? 1 : false;
}
var_dump( isValidIsbn10('0470173424') );
Source: https://stackoverflow.com/a/14096142/5201919
Will show
1 for true
Demo
https://eval.in/1053913
I need to round the returned number to closest multiple of 5 in this PHP.
return number_format(($entry['69']*$entry['68.2']*0.7), 0, ".", ",").'€' ;
How to get it correctly?
You can use round($val/5)*5:
$val = round($entry['69'] * $entry['68.2'] * 0.7 / 5) * 5;
return number_format($val, 0, ".", ",").'€' ;
Here is a custom function with greater detail of how everything is happening. You can change anything here if you need to update your code.
<?php
//Enter your code here, enjoy!
function roundClosest($num){
$helpVar = round($num/5);
$helpVar2 = $helpVar * 5;
if($num - $helpVar2 > 2.9){
$res = ($helpVar + 1) * 5;
}
else{
$res = $helpVar * 5;
}
return $res;
}
echo roundClosest(62.34);
echo "\n";
echo roundClosest(63.67);
Output:
60
65
Hope this helps!
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
Since i'm more of a "noob" at PHP than i'd hope, Is it possible to inverse the following function for example:
public function baseID($resourceid){
$rid = $resourceid;
$version = 0;
while ($rid > 16777216){
$version++;
if ($version == 1){
//the constant applied to all items
$rid -= 1342177280;
}elseif ($version == 2){
//the value added to the first updated version
$rid -= 50331648;
}else{
//the value added on all subsequent versions
$rid -= 16777216;
}
}
//$returnable = array('baseID'=>$rid,'version'=>$version);
return $rid;
}
Would it be possible to input the "baseID" and return a "resourceID" instead of the current way ?
I apologize if this is the wrong place to be asking such question
Not really. Your function returns the same value for all rids that are of the form:
5 * 2^28 + (3 + n) * 2^24
Where n is a positive integer.
>>> baseID(5 * 2**28 + (3 + 1) * 2**24)
16777216
>>> baseID(5 * 2**28 + (3 + 2) * 2**24)
16777216
>>> baseID(5 * 2**28 + (3 + 3) * 2**24)
16777216
So given just 16777216, you won't be able to determine what went into your function.
This function only works to a limited range, however at a certain point the resource IDs will start returning the same results.
public function resourceID($baseid){
$bid = $baseid;
$version = 0;
while ($bid <= 16777216){
++$version;
if ($version === 1){
$bid += 1342177280;
} elseif ($version === 2){
$bid += 50331648;
} else {
$bid += 16777216;
}
}
return $bid;
}