Sophie Rhodes Sophie Rhodes - 7 months ago 8
PHP Question

Identifying before and after with fixed point (Loop)

Trying to create a reusable function to recreate an array like such:

$page_step_steps = array(
array("id" => "1", "step" => "done-step"),
'main-active-step next-step-five',
array("id" => "2", "step" => "done-step"),
"main-active-step next-step-five",
array("id" => "3", "step" => "active-step"),
"main-next-step next-step-five",
array("id" => "4", "step" => "next-step"),
"main-next-step next-step-five",
array("id" => "5", "step" => "next-step")
);


I am currently stuck on identifying the everything ABOVE the active step and BELOW the active step, and give them the correct step ID

i.e. everything above the
active-step
needs to have a
done-step
assigned to the
step
value and the array underneath it should have
main-active-step
.

However, everything underneath it should have
next-step
and
main-next-step
.

Sandbox link to try it out: http://sandbox.onlinephpfunctions.com/code/c4b85c3d038560d8d3b4b5ad372f67a22df785c1

Here is the function so far:

function formatting_steps($step, $steps){
$page_step_steps = array();
$steps_t = array(1 => "one", 2 => "two", 3 => "three", 4 => "four", 5 => "five", 6 => "six");
$steps_f = $steps_t[$steps];
$final_steps = $steps * 2;

for($i = 1; $i < $final_steps; $i++){

$r_step = round($i / 2, 0, PHP_ROUND_HALF_UP);

if($i % 2){
if($i == 1){
$page_step_steps[$i]['id'] = $i;

if($i == $step){
$page_step_steps[$i]['step'] = "active-step";
}
else {
$page_step_steps[$i]['step'] = $i;
}
}
else {
$page_step_steps[$i]['id'] = $r_step;

if($r_step == $step){
$page_step_steps[$i]['step'] = "active-step";
}
else {
$page_step_steps[$i]['step'] = $i;
}
}
}
else {
$page_step_steps[] = 'next-step-' . $steps_f;
}

}

$page_step_steps = $page_step_steps;

return $page_step_steps;
}


Output so far:

Array
(
[1] => Array
(
[id] => 1
[step] => 1
)

[2] => next-step-four
[3] => Array
(
[id] => 2
[step] => 3
)

[4] => next-step-four
[5] => Array
(
[id] => 3
[step] => active-step
)

[6] => next-step-four
[7] => Array
(
[id] => 4
[step] => 7
)

)





EDIT: Passing the following in the function :
formatting_steps(3, 4);

JSG JSG
Answer

Just looking at your function, you have an statement that identifies the active state.

If you focus on the else statement, you can do the following

if($step > $i){
    $dfu_steps[$i]['step'] = "done-step";               
}
else{               
    $dfu_steps[$i]['step'] = "next-step";
}

and

if($step > $r_step){
    $dfu_steps[$i]['step'] = "done-step";               
}
else{
    $dfu_steps[$i]['step'] = "next-step";
}   
Comments