Jump to content

Using the FOR option to print out a list with changing number


Sleeper

Recommended Posts

Hello.

 

I am working on something where I don't want to do this....

 

if ($pages <= 1){$P=1;}
elseif ($pages > 1 && $pages <= 2){$P=2;}
elseif ($pages > 2 && $pages <= 3){$P=3;}
elseif ($pages > 3 && $pages <= 4){$P=4;}
elseif ($pages > 4 && $pages <= 5){$P=5;}
elseif ($pages > 5 && $pages <= 6){$P=6;}
elseif ($pages > 6 && $pages <= 7){$P=7;}
elseif ($pages > 7 && $pages <= {$P=8;}
elseif ($pages > 8 && $pages <= 9){$P=9;}
elseif ($pages > 9 && $pages <= 10){$P=10;}

 

This could go on and on and I would rather use the for $ i++ for this so it will wright out the coding for as little or as much as it needs rather then I.

 

However I am not sure how I would do this. I also have

 

if ($PN == 1){$L = "0,20";}
elseif ($PN == 2){$L = "20,20";}
elseif ($PN == 3){$L = "40,20";}
elseif ($PN == 4){$L = "60,20";}
elseif ($PN == 5){$L = "80,20";}
elseif ($PN == 6){$L = "100,20";}

were I will need to be able to add 20 to the number.

 

Is this easily done? I'm a bit loss tried a few things and its not working properly.

 

 

ok I was able to do..

 

$i=1;
while ($i <= ($pages+1)) {
$G= $i+1;
if ($pages <= 1){$P=1;}
elseif ($pages > $i && $pages <= $G){$P=$G;}
$i++;
}

 

to solve the first issue. However the second one were I have to add one to each of the PN's but then add 20 to the Limit not sure how I would do that now.

Unless I'm completely misunderstanding your intentions, the first block of code is the same as this:

if( $pages <= 1 ) {
     $P = 1;
} else {
     $P = $pages;
}

 

I see no reason why the second block couldn't be written as:

if( !isset($PN) || $PN < 1) {
$PN = 1;
}
$L = (($PN * 20) -20) . ", 20";

$L = (($PN * 20) -20) . ", 20";

 

duh. Sometimes the simplest idea is the best, not sure why I didn't think of that before.

 

The first one is pulling out the number of rows in the table, so that I can put page 1,2,3 and so on. So if there are 83 rows at 25 per page you get 3.32 pages. So its assigning the that there will be 4 pages.

 

The script on the page just pulls up 4 so I can say you are on page 1 of "4".

 

but thank you for the second one, much simple way of doing it. I did just sort out that I could use...

$j=2;
$ln=25;
while ($j <= ($PN)) {
$G= $j+1;
if ($PN == 1){$L = "0,25";}
elseif ($PN == $j){$L = "$ln,25";}
$i++;
$ln+25;
}

 

and that dose the trick, however one line verses that is much better..lol.

 

Thanks again.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.