Reset Counter in While Loop - php

I would like to reset counter in the following loop. Have no idea how to do it
$j = 1;
while($row = mysqli_fetch_array($check)) {
$value = $row['tmpi'];
$calculate = mysqli_query($link,"SELECT * FROM tmpi_db WHERE tmpi_id = '".value."'");
$action = mysqli_num_rows($calculate);
if($j == $action) {
//RESET $j, $j must be one again, nothing is working I have tried $j = 1;
//It's keep incrementing 1,2,3,4,5,6,7.....
}
}
$j++
Please help

Something like this should work. Simply start at zero, increment it in beginning and reset the $j if the action occurs, the next time it loops the $j will increment again and be in zero configuration (i.e. contain value 1):
$j = 0;
while($row = mysqli_fetch_array($check))
{
$j++;
$value = $row['tmpi'];
$calculate = mysqli_query($link,"SELECT * FROM tmpi_db WHERE tmpi_id = '".value."'");
$action = mysqli_num_rows($calculate);
if($j == $action)
{
$j = 0;
}
}

Just move your $j++ in an else section eg
if($j == $action){
//stuff and reset
}else{
$j++;
}

You can try this
if($j == $action) i{
//your code
$j=0;
} else {
$j++;
}

Related

How to reiterate same loop again in php?

I have the following foreach loop, how can I reiterate over the same loop index again?
$n = 0;
$currentTotal = 0;
$currentSettlement = $coupons[0]->settlement_id;
foreach ($coupons as $coupon){
if($currentSettlement == $coupon->settlement_id){
$currentTotal += $coupon->amount;
}else{
$settlements[$n]['total'] = $currentTotal;
$currentTotal = 0;
$currentSettlement = $coupon->settlement_id;
$n++;
// after this I want to reiterate current loop from the beginning
}
}
If you want necessary foreach loop you can use a function which you fix that it runs only twice:
$n = 0;
$currentTotal = 0;
$currentSettlement = $coupons[0]->settlement_id;
for2loop(1,$coupons);
function for2loop($loopagain,$coupons)
{
foreach ($coupons as $coupon) {
if ($currentSettlement == $coupon->settlement_id) {
$currentTotal += $coupon->amount;
} else {
$settlements[$n]['total'] = $currentTotal;
$currentTotal = 0;
$currentSettlement = $coupon->settlement_id;
$n++;
if($loopagain == 1)
for2loop(2,$coupons); // after this I want to reiterate current loop from the beginning
}
}
return $currentSettlement
}
but i think that yu must prefer while loop or for loop better

form two arrays in a for loop checking for ignition state change condition

I need a logic which checks for ignition state change and if the ignition is on push that index into start array and if state then changes to off push into stop array so that I can consider one start array index to stop array index as one trip. I had a logic but this pushes all the start values and all the stop values I only need state change index
$isIgniOn = false; $startArray = array(); $stopArray = array();
for ($i = 0; $i < count($reportData); $i++) {
if ($reportData[$i]['ignition_status'] == 1) {
$startArray[] = $i;
$isIgniOn = true;
} else {
if ($isIgniOn) {
$isIgniOn = false;
$stopArray[] = $i;
} else {
$startArray[] = $i;
}
}
}
The problem is, you are not actually checking to see if the state has changed and need to add some extra logic in your program:
$isIgniOn = false; $startArray = array(); $stopArray = array();
for ($i = 0; $i < count($reportData); $i++) {
if ($reportData[$i]['ignition_status'] == 1 && !$isIgniOn) {
$startArray[] = $i;
$isIgniOn = true;
} elseif ($reportData[$i]['ignition_status'] == 0 && $isIgniOn) {
{
$isIgniOn = false;
$stopArray[] = $i;
}
}
}

Convert simple string to Required Format : PHP

I would like to Convert simple string to set based on below logic
if string is 3,4-8-7,5 then I need the set as (3,8,7),(4,8,5).
The Logic behind to building the set are we need to consider ',' as OR condition and '-' as AND condition.
I am trying my best using For loop :
$intermediate = array();
$arry_A = explode('-', '3,4-8-7,5');
for ($i = 0; $i < count($arry_A); $i++) {
$arry_B = explode(',', $arry_A[$i]);
for ($j = 0; $j < count($arry_B); $j++) {
if (count($intermediate) > 0) {
for ($k = 0; $k < count($intermediate); $k++) {
$intermediate[$k] = $intermediate[$k] . ',' . $arry_B[$j];
}
} elseif (count($intermediate) === 0) {
$intermediate[0] = $arry_B[$j];
}
}
}
echo $intermediate, should give final result.
This Code works correctly, Try this
<?php
$intermediate = array();
$str="";
$val='3,4-8-7,5';
$vals=str_replace(',','-',$val);
$j=1;
$arry_A = explode('-',$vals );
$str.='(';
for ($i = 0; $i < count($arry_A); $i++) {
if($j==3){
$str.=$arry_A[$i].',';
$str.='),';
$str.='(';
$j=1;
}
else
$str.=$arry_A[$i].',';
$j++;
}
echo substr($str, 0, -2);
?>

check and display multiple values in for loop

I have for loop in which I have to check that the array that I am retriving should not empty. And if empty then don't display that field.
$checkboxes = array();
$text = array();
$Enhanced = array();
$Search = array();
$Landing = array();
$spotlights="";
for ($i = 1; $i <= 20; $i++) {
$checkboxes[$i] = $_POST[$i];
$text[$i] = $_POST[$i.'t'];
}
for ($p = 1; $p <= 20; $p++) {
$checkboxes[$p] = $_POST[$p];
$Enhanced[$p] = $_POST['Enhanced'.$p];
}
for ($q = 1; $q <= 20; $q++) {
$checkboxes[$q] = $_POST[$q];
$Search[$q] = $_POST['Search'.$q];
}
for ($r = 1; $r <= 20; $r++) {
$checkboxes[$r] = $_POST[$r];
$Landing[$r] = $_POST['Landing'.$r];
}
for ($j = 1; $j <= 20; $j++) {
if($checkboxes[$j]!="")
{
$spotlights=$spotlights."<strong>".$checkboxes[$j]."</strong><br>".$Enhanced[$j]."<br>".$Search[$j]."<br>".$Landing[$j]."<br>".$text[$j]."<br><br>";
}
}
echo $spotlights;
In above code, $Enhanced[$j], $Search[$j], $Landing[$j] may or may not be empty as these are checkboxes. If any of them is empty i don't want to print that. In above case if any of three field empty then it display <br/> which i don't want.
If I check them one by one or combination of them then my code will be longer.
How do I check in smarter way? How should I write the line of $spotlights=... so that it will display only non empty variables($Enhanced[$j], $Search[$j], $Landing[$j])?
I have tried bolow which is not working.
using functions:
for ($j = 1; $j <= 20; $j++) {
if($checkboxes[$j]!="")
{
$spotlights=$spotlights."<strong>".$checkboxes[$j]."</strong>".enhanced($j).search($j).landing($j)."<br>".$text[$j]."<br><br>";
}
}
function enhanced($j) {
if($Enhanced[$j]!="")
{
return "<br/>".$Enhanced[$j];
}
}
function search($j) {
if($Search[$j]!="")
{
return "<br/>".$Search[$j];
}
}
function landing($j) {
if($Landing[$j]!="")
{
return "<br/>".$Landing[$j];
}
}
above code not displaying values of $Enhanced[$j], $Search[$j], $Landing[$j]
checking values independently
for ($j = 1; $j <= 20; $j++) {
if($checkboxes[$j]!="")
{
$spotlights .= $spotlights."<strong>".$checkboxes[$j]."</strong>";
if($Enhanced[$j]!="")
{
$spotlights .= "<br>".$Enhanced[$j];
}
if($Search[$j]!="")
{
$spotlights .= "<br>".$Search[$j];
}
if($Landing[$j]!="")
{
$spotlights .= "<br>".$Landing[$j];
}
$spotlights .= "<br>".$text[$j]."<br><br>";
}
values are getting repeated by using above code.
Use array_filter.
Something like that:
$checkboxes = array_filter($checkboxes);
foreach($checkboxes as $checkbox)
{
$j = array_search($checkbox, $checkboxes)
$spotlights=$spotlights."<strong>".$checkboxes[$j]."</strong><br>".$Enhanced[$j]."<br>".$Search[$j]."<br>".$Landing[$j]."<br>".$text[$j]."<br><br>";
echo $spotlights;
}
array_filter will remove all NULL, false or '' from array, but will keep the position, so if you have in checkbox[3] something different, in checkboxes after array_filter will be on position 3. doesn't matter if on position 2 was NULL.
I don't think checking them individually will be a lot of trouble, as there are only three of them, but if you want to keep it one line, the line where you define $spotlights with this:
$spotlights=$spotlights."<strong>".$checkboxes[$j]."</strong>"
.($Enhanced[$j] != "" ? "<br>".$Enhanced[$j] : "")
.($Search[$j] != "" ? "<br>".$Search[$j] : "")
.($Landing[$j] != "" ? "<br>".$Landing[$j] : "")
."<br>".$text[$j]."<br><br>";
Hope this helped.

Getting an undefined offset in my PHP array while in a foreach

I need some help, even though I think I'm checking for the length of the array and I should be breaking out of the loop, I still get warnings on my [else if ($value....] line. So either I'm missing something crucial or I've been staring at this code segment too long and its obvious. Any insight would be appreciated.
$count = count($filter); //Filter is an array
if ($count > 1 ){
//Compare values and generate a range to choose from
$i = 1;
foreach($filter as $value){
//Break the loop if at the end of the array
if ($i >= $count){
//throw new exception($i .' '.$count);
break;
}
//if the value is smaller then the next procceding value, because they are already in order of presidence,
//add it to our range of potentials.
else if($value < $filter[$i]->value){
array_push($range, key($filter));
}
$i++;
}
}else {
return false;
}
I suspect that there are gaps in your array. Try this:
$filter = array_values($filter); // this will remove any gaps in the array
$count = count($filter);
if ($count <= 1)
return false;
for ($i = 0; $i < $count; $i++)
{
if ($i != $count-1 && $filter[$i]->value < $filter[$i+1]->value)
array_push($range, key($filter));
}
Your array might have non-numeric keys. Then try this:
foreach($filter as $key=>$value)
{
// test for $filter[$key];
}
Or your $filter array doesn't hold objects, then you can't use the -> in
$filter[$key]->value
try this code.... no need of checking count..
$range = array();
$i = 1;
foreach($filter as $value)
{
if(isset($filter[$i]) && $value < $filter[$i]->value)
{
array_push($range, key($filter));
$i++;
}
else
{
break;
}
}

Categories