Hi Guys,
I’m having a mental block on a simple PHP problem - Hoping someone could help me with 😊
I need to write a loop that goes up, then back down between 1 and a variable number. I.e. if $v = 5 I need the loop output to be
1,2,3,4,5,5,4,3,2,1,1,2,3,4,5,5,4,3,2,1
I know the solution is going to be simple but for some reason I can’t figure it out :-(
I’ve got it close but can’t figure it out. I have done:
<?php
$i = 1;
$direction = "up";
$v = 5;
$loop = 20;
$l = 1;
while ($l < $loop)
{
if($direction == "up")
{
$l++;
print $i;
$i++;
if ($i == $v){
$direction = "down";
}
} else {
$l++;
print $i;
$i = $i-1;
if ($i == 1){
$direction = "up";
}
}
}
?>But it outputs 1,2,3,4,5,4,3,2,1,2,3,4,5,4 etc
When I need the last numbers to repeat next to each other i.e. 1,2,3,4,5,5,4,3,2,1,1,2,3,4,5
Figured it out, if you’re interested:
$i = 1;
$direction = "up";
$v = 5;
$loop = 20;
$l = 1;
while ($l < $loop){
print $i;
$l++;
if($direction == "up"){
if($i >= 4){
$i = 4;
$direction = "down";
} else {
$i++;
}
} else {
if($i <= 1){
$i = 1;
$direction = "up";
} else {
$i = $i - 1;
}
}
}
?>LOL I knew there was going to be an easier way to do it.
Application for organising a sport tournament that has a lot of different brackets with a different amount of competitors per division. The competition is run on 4 matted areas (although this can change).
I figured the best way to assign them to mats was to order the divisions from most competitors to least, and then assign them using that loop. So if the brackets were as follows
1) 25 Competitors 2) 23 Competitors 3) 23 Competitors 4) 21 Competitors 5) 19 Competitors 6) 18 Competitors 7) 11 Competitors 8) 7 Competitors
It would loop though and put the largest 4 divisions on mats 1, 2, 3 and 4. If it started again at Mat 1 then it was going to take longer to finish as it was getting larger divisions each time, so it reverses and gives puts match 5 onto mat 4, match 6 onto mat 3 etc.
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.