I have one issue that I can't solve. So I have for loop.
So here is little code:
for ($i=0;$i<3;$i++) {
$int = $i + 1;
if($sms->mobio_check($servID,$request->input("code$int"))) {
continue;
$cart->success($product->id,$product->server->name);
} else {
return redirect()->to(route('mcCheckoutFailed'))->withErrors(['codeError'=>__('messages.invalidCode',['input'=>$int])]);
}
}
I want if three ifs return true to run function $sms->success();.
What is wrong here?
You could rely on the fact that if the loop finished, then it's OK, any failures will cause the return in the loop to exit...
for ($i=0;$i<3;$i++) {
$int = $i + 1;
if( ! $sms->mobio_check($servID,$request->input("code$int"))) {
return redirect()->to(route('mcCheckoutFailed'))->withErrors(['codeError'=>__('messages.invalidCode',['input'=>$int])]);
}
}
$cart->success($product->id,$product->server->name);
You can do it like this:
$success = true;
for ($i=0;$i<3;$i++) {
$int = $i + 1;
if($success = $success || $sms->mobio_check($servID,$request->input("code$int"))) {
continue;
$cart->success($product->id,$product->server->name);
} else {
return redirect()->to(route('mcCheckoutFailed'))->withErrors(['codeError'=>__('messages.invalidCode',['input'=>$int])]);
}
}
if ($success) $sms->success();
you can try this
$count = 0;
for ($i=0;$i<3;$i++) {
$int = $i + 1;
if($sms->mobio_check($servID,$request->input("code$int"))) {
$count++;
} else {
return redirect()->to(route('mcCheckoutFailed'))->withErrors(['codeError'=>__('messages.invalidCode',['input'=>$int])]);
}
}
if($count == 3)
$cart->success($product->id,$product->server->name);
Related
The below code works and does output exactly what i want. I made a foreach loop getting the values of a specific field ($CustomFields...) which is part of a framework variable. Then is only counts that field when the condition is "group".
After that i want to het the average price of all fields / count.
// ########### Get average hourly rate for group classes
$itemsperhour = array();
$countperhour = 0;
foreach($listings as $listing) {
if ($CustomFields->fieldValue('jr_typeoflesson',$listing,false,false) == 'group') {
$itemsperhour[] = $CustomFields->field('jr_hourlyrateus',$listing,false,false);
$countperhour = $countperhour + 1;
}
}
//print_r($items);
if ($countperhour > 0) {
$totalperhour = array_sum($itemsperhour);
$averageperhour =($totalperhour / $countperhour);
echo round($averageperhour,2);
} else {
echo "No data";
}
unset ($averageperhour);
As said, the snippet works. But may I ask how other people would write such a script related to optimise such a piece of code (for speed and readability?
PHP 5.6+
Jasper
Below is one way of optimizing:
$totalperhour = 0;
$countperhour = 0;
foreach($listings as $listing) {
if ($CustomFields->fieldValue('jr_typeoflesson',$listing,false,false) == 'group') {
$totalperhour += $CustomFields->field('jr_hourlyrateus',$listing,false,false);
$countperhour = $countperhour + 1;
}
}
if($countperhour > 0) {
$averageperhour =($totalperhour / $countperhour);
echo round($averageperhour,2);
$averageperhour = '';
} else {
echo "No data";
}
I would suppose to use array_reduce function for getting the average:
$averageperhour = array_reduce($listings, function($average, $listing) use (&$CustomFields)
{
static $sum = 0;
static $counter = 0;
if ($CustomFields->fieldValue('jr_typeoflesson', $listing, false, false) == 'group') {
$sum += $CustomFields->field('jr_hourlyrateus', $listing, false, false);
$counter ++;
$average = round(($sum / $counter), 2);
}
return $average;
}, 'No data');
echo $averageperhour;
Not sure about speed improvement (needs testing), but this variant seems to me like more readable.
How about this?
$itemsPerHour = [];
foreach($listings as $listing) {
if ($CustomFields->fieldValue('jr_typeoflesson', $listing, false, false) !== 'group') {
continue;
}
$itemsPerHour[] = $CustomFields->field('jr_hourlyrateus', $listing, false, false);
}
$countPerHour = count($itemsPerHour);
if ($countPerHour > 0) {
$averagePerHour = array_sum($itemsPerHour) / $countPerHour;
echo round($averagePerHour,2);
} else {
echo "No data";
}
I want to put the input like "RKKRRRRK" and try to get the output like largest continuous segment.. Suppose my input may be "RKKKR" then my program will display 'KKK' is the largest continuous segment.. and then it also display the count is 3..
I've already write the code for counting 'R' values.. now i want this program also... need help anyone help me.. thanks in advance.
Here the code:-
<?php
function numberOfR($string1)
{
for($i=0;$i <strlen($string1);$i++)
{
if($string1[$i]!='K')
{
$count++;
}
}
return $count;
}
$return_value= numberOfR("RKKRK");
echo "R's count is:";
echo $return_value;
?>
<?php
function getLongetSegment($string) {
$currentSegmentChar='';
$currentSegment="";
$biggestSegment="";
$current_length=0;
$biggest_length=0;
for($i=0;$i<strlen($string);$i++) {
$char = $string[$i];
if($char != $currentSegmentChar || $currentSegmentChar == '') {
if($current_length >= $biggest_length) {
$biggestSegment = $currentSegment;
$biggest_length = $current_length;
}
$currentSegmentChar = $char;
$currentSegment = $char;
$current_length = 1;
}
elseif($currentSegmentChar != '') {
$currentSegment .= $char;
$current_length++;
}
}
if($current_length >= $biggest_length) {
$biggestSegment = $currentSegment;
}
return array("string" => $biggestSegment,"length" => $biggest_length);
}
print_r(getLongetSegment("RKKRGGG"));
?>
Result: GGG
You can use preg_match_all over here as
preg_match_all('/(.)\1+/i','RKKRRRRK',$res);
usort($res[0],function($a,$b){
return strlen($b) - strlen($a);
});
echo $res[0][0];
Not sure if I understood this quite right. Something like this:
function maxCharSequece($string1)
{
$maxSeq = $seq = 0;
$maxChar = $lastChar = null;
for( $i = 0; $i < strlen($string1); $i++ )
{
$c = $string1[$i];
if (!$lastChar) $lastChar = $c;
if ( $lastChar == $c ){
if ( ++$seq > $maxSeq ) $maxChar = $lastChar;
}
else {
$maxSeq = $seq;
$seq = 0;
}
}
return $maxChar;
}
You can use preg_replace_callback to receive all continuous segments and select the longest
$sq = '';
preg_replace_callback('/(.)\1+/',
function ($i) use (&$sq) {
if(strlen($i[0]) > strlen($sq)) $sq = $i[0];
}, $str);
echo $sq . " " . strlen($sq);
i got 7 if else statement, may I know how i can compile it into one function file? so I just trigger the function rather a long and messy code.
below are my codes:
<?php
$r1a = $_POST['rowone'];
if ($r1a<7) {
$r2a=$r1a+1;
} else {
$r2a=$r1a+1-7;
}
if ($r2a<7) {
$r3a=$r2a+1;
} else {
$r3a=$r2a+1-7;
}
if ($r3a<7) {
$r4a=$r3a+1;
} else {
$r4a=$r3a+1-7;
}
if ($r4a<7) {
$r5a=$r4a+1;
} else {
$r5a=$r4a+1-7;
}
if ($r5a<7) {
$r6a=$r5a+1;
} else {
$r6a=$r5a+1-7;
}
if ($r6a<7) {
$r7a=$r6a+1;
} else {
$r7a=$r6a+1-7;
}
?>
the second problem I have is adding all together, there are 7 vertical row that have 3 value and another 5 row that have 2 value to add. I found out my code over here if there is a total value of 12 it will show 0 because of my code -12. Is there anyone I wan alter the code if the value is 12, the result will +1-12?
Thanks!
<?php
$r4a = $r1a+$r2a+$r3a;
if ($r4a<12) {
$r4a=$r4a;
} else {
$r4a=$r4a-12;
}
$r4b = $r1b+$r2b+$r3b;
if ($r4b<12) {
$r4b=$r4b;
} else {
$r4b=$r4b-12;
}
$r4c = $r1c+$r2c+$r3c;
if ($r4c<12) {
$r4c=$r4c;
} else {
$r4c=$r4c-12;
}
$r4d = $r1d+$r2d+$r3d;
if ($r4d<12) {
$r4d=$r4d;
} else {
$r4d=$r4d-12;
}
$r4e = $r1e+$r2e+$r3e;
if ($r4e<12) {
$r4e=$r4e;
} else {
$r4e=$r4e-12;
}
$r4f = $r1f+$r2f+$r3f;
if ($r4f<12) {
$r4f=$r4f;
} else {
$r4f=$r4f-12;
}
$r4g = $r1g+$r2g+$r3g;
if ($r4g<12) {
$r4g=$r4g;
} else {
$r4g=$r4g-12;
}
$r4h = $r2h+$r3h;
if ($r4h<12) {
$r4h=$r4h;
} else {
$r4h=$r4h-12;
}
$r4i = $r2i+$r3i;
if ($r4i<12) {
$r4i=$r4i;
} else {
$r4i=$r4i-12;
}
$r4j = $r2j+$r3j;
if ($r4j<12) {
$r4j=$r4j;
} else {
$r4j=$r4j-12;
}
$r4k = $r2k+$r3k;
if ($r4k<12) {
$r4k=$r4k;
} else {
$r4k=$r4k-12;
}
$r4l = $r2l+$r3l;
if ($r4l<12) {
$r4l=$r4l;
} else {
$r4l=$r4l-12;
}
?>
I don't think you need the if's at all. Modulo operations should solve it for you.
function inc($a) {
return ($a % 7)+1;
}
$r1a = $_POST['rowone'];
$r2a = inc($r1a);
$r3a = inc($r2a);
$r4a = inc($r3a);
$r5a = inc($r4a);
$r6a = inc($r5a);
$r7a = inc($r6a);
Code hasn't been tested, but you get the general idea.
Another alternative:
$v = $_POST['rowone'];
$arr = array(1, 2, 3, 4, 5, 6, 7);
$b = array_slice($arr, 0, $v-1);
$arr = array_slice($arr, $v-1);
$arr = array_merge($arr, $b);
print_r($arr);
Answer to your second question where I have only made 0 into 1 and kept the rest as it were(To clearify, if the input to mod12 e.g $r1a+$r2a+$r3a is a multiple of 12 it will return 1, in other cases it will return a standard modulo 12 e.g 13 will become 1):
function mod12($a) {
$m = $a % 12;
if ($m == 0) {
return 1;
}else {
return $m;
}
}
$r4a = mod12($r1a+$r2a+$r3a);
$r4b = mod12($r1b+$r2b+$r3b);
$r4c = mod12($r1c+$r2c+$r3c);
$r4d = mod12($r1d+$r2d+$r3d);
$r4e = mod12($r1e+$r2e+$r3e);
$r4f = mod12($r1f+$r2f+$r3f);
$r4g = mod12($r1g+$r2g+$r3g);
$r4h = mod12($r2h+$r3h);
$r4i = mod12($r2i+$r3i);
$r4j = mod12($r2j+$r3j);
$r4k = mod12($r2k+$r3k);
$r4l = mod12($r2l+$r3l);
If I understood well what you try to achieve, this should work:
$r1a = $_POST['rowone'];
for ( $i = 1; $i < 7; $i++ ){
$cur = 'r'.$i.'a';
$next = 'r'.($i+1).'a';
$$next = $$cur < 7 ? $$cur+1 : $$cur+1-7
}
I had an interview today and the person asked me this question:
How do you find easily an item in a circularly sorted array
Since I didn't know the answer, I tried to find a solution. Here's what I have:
Thanks
<?php
function searchincircularsorterlist($a, $len, $num) {
$start=0;
$end=$len-1;
$mid = 0;
while($start<$end) {
$mid=$start+$end/2;
if ($num == $a[$mid]) {
return $num;
}
if($num<$a[$mid]) {
if($num<$a[$start] && $a[$start]<=$a[$start+1])
$start=$mid++;
else
$end=$mid--;
}
else {
if($num>$a[$end] && $a[$end-1]<=$a[end])
$end=$mid--;
else
$start=$mid++;
}
}
if ($start == $end && $num == $a[$start]) {
return $num;
}
return -1;
}
$array = array(7,8,9,0,1,2,3,4,5,6);
var_dump(searchincircularsorterlist($array,sizeof($array),4));
I am trying to work with a circularly sorted array but for some reason it does not work. What's wrong with my code?
1) learn priority of operations. You should have: $mid=($start+$end)/2; which you ended up dividing $end by 2 and then $start - the result. This is why you got an infinite loop.
2) use: $start=$mid+1; and not $start=$mid++; that will help reducing the number of loops
<?php
function searchincircularsorterlist($a, $len, $num) {
$start=0;
$end=$len-1;
$mid = 0;
while($start<$end) {
$mid=($start+$end)/2;
if ($num == $a[$mid]) {
return $num;
}
if($num<$a[$mid]) {
if($num<$a[$start] && $a[$start]<=$a[$start+1])
$start=$mid+1;
else
$end=$mid-1;
}
else {
if($num>$a[$end] && $a[$end-1]<=$a[end])
$end=$mid-1;
else
$start=$mid+1;
}
}
if ($start == $end && $num == $a[$start]) {
return $num;
}
return -1;
}
$array = array(7,8,9,0,1,2,3,4,5,6);
var_dump(searchincircularsorterlist($array,sizeof($array),4));
I'm using a include file to translate my text. it works pretty good, but now I need to on button click translate some mor words, and return doesn't work anymore, but echo does.
so what I'm searching is a way of know if return is possible or not, code example
for ($i = 0; $i < count($palavras); $i++) {
if ($palavras[$i] == $palavra) {
if($lingua == 1) {
return $traducao_1[$i];
}
if($lingua == 2) {
return $traducao_2[$i];
}
}
}
this one works good first time page is executed, since this is included file.
how to make this?
if(!return $traducao_1[$i]) {
thanks
ok, tryed to answer, but always got an error, so I'm editing this as answer
Thank you all for help, I manage a way of make it work like adding one action to the function and checking if action == , then do something, like this
function test($palavra, $lingua, $accao) {
for ($i = 0; $i < count($palavras); $i++) {
if ($palavras[$i] == $palavra) {
if($lingua == 1) {
if($accao != "2_chamada") {
return $traducao_1[$i];
} else {
echo $traducao_1[$i];
}
}
}
}
}
Again, thanks for help
maybe check to see if it's set first before returning
for ($i = 0; $i < count($palavras); $i++) {
if ($palavras[$i] == $palavra) {
if($lingua == 1) {
$return = $traducao_1[$i];
if($return != false) {
return $return;
} else {
echo 'something';
}
}
if($lingua == 2) {
$return = $traducao_2[$i];
if($return != '') {
return $return;
}
}
}
}
return can only be used in functions
i reccommend this for you
function test($palavras,$palavra,$traducao_1,$traducao_2){
for ($i = 0; $i < count($palavras); $i++) {
if ($palavras[$i] == $palavra) {
if($lingua == 1) {
return $traducao_1[$i];
}
if($lingua == 2) {
return $traducao_2[$i];
}
}
}
}
if(!test($palavras)) {