Gossamer Forum
Home : General : Internet Technologies :

Simple PHP problem I'm stuck on.

Quote Reply
Simple PHP problem I'm stuck on.
Hi Guys,

Long time no see! I've been out of the game for a while now. I'm writing a PHP script and I'm having a mental block on a simple PHP problem - Hoping someone could help me with Smile
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 :-(

Looks like the forums here have gone quiet since I was a regular Frown
Cheers,
Michael Bray
Quote Reply
Re: [Michael_Bray] Simple PHP problem I'm stuck on. In reply to
I’ve got it close but can’t figure it out. I have done:

Code:
<?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
Cheers,
Michael Bray
Quote Reply
Re: [Michael_Bray] Simple PHP problem I'm stuck on. In reply to

Figured it out, if you're interested:



Code:

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

}


}


?>


Cheers,
Michael Bray