Home  >  Article  >  Backend Development  >  IF 判断转换 FOR 语句

IF 判断转换 FOR 语句

WBOY
WBOYOriginal
2016-06-23 13:39:231340browse

	if($page>0 && $page<11){$i=1;$pages=10;if($c<11){$pages=$c;}}else	if($page>10 && $page<21){$i=11;$pages=20;if($c<21){$pages=$c;}}else	if($page>20 && $page<31){$i=21;$pages=30;if($c<31){$pages=$c;}}else			if($page>30 && $page<41){$i=31;$pages=40;if($c<41){$pages=$c;}}else	if($page>40 && $page<51){$i=41;$pages=50;if($c<51){$pages=$c;}}else	if($page>50 && $page<61){$i=51;$pages=60;if($c<61){$pages=$c;}}else	if($page>60 && $page<71){$i=61;$pages=70;if($c<71){$pages=$c;}}else	if($page>70 && $page<81){$i=71;$pages=80;if($c<81){$pages=$c;}}else	if($page>80 && $page<91){$i=81;$pages=90;if($c<91){$pages=$c;}}

说明:
$i 为起始页
$page 为结束页
$c 为总页数

效果:
实现循环输出 不用更改页面代码


回复讨论(解决方案)

//已知,需提供$page=90;$c=98;//未知,进行判断$pages=0;$i=0;//$max_pages=$page;//为防止$page过大最好设成固定值$max_page=90;for($index=0;$index<$max_page;$index+=10){	if($page>$index&&$page<$index+11)	{		$i=$index;		$pages=$index+10;		if($c<=$pages) $pages=$c;//不考虑$i?		break;	}}echo '$i='.$i.';$pages='.$pages;

其实也可以不用for,嘿嘿

//已知,需提供$page=90;$c=98;//未知,进行判断$pages=0;$i=0;$i=intval(($page-1)/10)*10;if($i>0) $pages=$i+10;if($c<=$pages) $pages=$c;echo '$i='.$i.';$pages='.$pages;

$i = floor(($page-1)/10)*10+1;	$pages=$page;

pages需要改下,应等于c

$i = floor(($page-1)/10)*10+1; $pages=$c;

改为FOR 是不想固定页数
不提供固定值。
$page=90; $c=98;

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn