-2

anybody can help me me with this? I have a variable such:

$page = '50';
$newpage = 'http://www.mydomain.com/page/'.$page.'';

I want new page echo such this:
http://www.mydomain.com/page/50
http://www.mydomain.com/page/49
......
.......
....... until page acho such:
http://www.mydomain.com/page/1
dmckee --- ex-moderator kitten
  • 98,632
  • 24
  • 142
  • 234

3 Answers3

2
<?php
for($page=50;$page>0;$page--) {
    $newpage = 'http://www.mydomain.com/page/'.$page.'';
    echo "$newpage\n";
}
?>
j08691
  • 204,283
  • 31
  • 260
  • 272
1

Look at this for loop:

$prefix = 'http://www.mydomain.com/page/';
for ($page = 50; $page >= 1; $page--) {
    echo $prefix.$page;
}

First, the variable $page is initialized. I use 50 instead of '50' because we're dealing with numbers and not with strings.

The next bit is the condition while the loop continues: $page >= 1 - so the loop will stop after 1.

The last part is a decrement operator, it subracts 1 off $page for each loop iteration.

Finally, the prefix and the page number are combined using the concat operator (.).

Yogu
  • 9,165
  • 5
  • 37
  • 58
0

What you are looking for is :

<?php
for($i=50; 0 <= $i; $i--){
    echo $i.'<br />';
}
?>

if you don't want to go upto 0, change <= for <. This will loop from 50 until 0 so it will eacho in order :

50
49
48
47
46
...
3
2
1
0

Here 50 is your start number, 0 is your last number to go. So in this example it print out 50 until 0.

David Bélanger
  • 7,400
  • 4
  • 37
  • 55