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.

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

$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.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.