IF 判斷轉換 FOR 語句

來源:互聯網
上載者:User
if($page>0 && $page<11){$i=1;$pages=10;if($c<11){$pages=$c;}}elseif($page>10 && $page<21){$i=11;$pages=20;if($c<21){$pages=$c;}}elseif($page>20 && $page<31){$i=21;$pages=30;if($c<31){$pages=$c;}}elseif($page>30 && $page<41){$i=31;$pages=40;if($c<41){$pages=$c;}}elseif($page>40 && $page<51){$i=41;$pages=50;if($c<51){$pages=$c;}}elseif($page>50 && $page<61){$i=51;$pages=60;if($c<61){$pages=$c;}}elseif($page>60 && $page<71){$i=61;$pages=70;if($c<71){$pages=$c;}}elseif($page>70 && $page<81){$i=71;$pages=80;if($c<81){$pages=$c;}}elseif($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;

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.