We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Simple PHP problem I'm stuck on

Development and Programming

Mike Bray's avatar
Mike Bray
11 posts
14 years ago
Mike Bray's avatar Mike Bray

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 :-(

       
Mike Bray's avatar
Mike Bray
11 posts
14 years ago
Mike Bray's avatar Mike Bray

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

       
Mike Bray's avatar
Mike Bray
11 posts
14 years ago
Mike Bray's avatar Mike Bray

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;
        }
    
    }

}

?>
       
Mat-Moo's avatar
Mat-Moo
350 posts
14 years ago
Mat-Moo's avatar Mat-Moo

Or using some Math 😊

$i = 0;
$divvy = 5;
$num = 20;
while($i < $num)
{
    $bit = (($i % $divvy)+1);
    print (((floor($i / $divvy) % 2) == 0) ? $bit  : (($divvy+1)-$bit)).",";
    $i++;
}
       
Mark Bowen's avatar
Mark Bowen
12,637 posts
14 years ago
Mark Bowen's avatar Mark Bowen

Okay you’ve reeled me in now 😉

Just wondering if you can share what you needed this for?

Best wishes,

Mark

P.S. I’ll be totally honest here and say that I’m just being nosy, coupled with a large dose of intrigue 😉

       
jwindhorst's avatar
jwindhorst
99 posts
14 years ago
jwindhorst's avatar jwindhorst

how about:

$up=array(1,2,3,4,5);
$both = array_merge($up, array_reverse($up));
foreach($both as $num)
    echo $num . '
';
       
Mike Bray's avatar
Mike Bray
11 posts
14 years ago
Mike Bray's avatar Mike Bray

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.

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.