Home >Backend Development >PHP Tutorial >关于循环判断的

关于循环判断的

WBOY
WBOYOriginal
2016-06-23 13:37:40832browse

比如我从数据表里读取了10条信息
其中第5条输出方式是

  • ".$row1['title']."

  • 第6条输出方式
    • ".$row1['title']."

    • 第10条输出方式
    • ".$row1['title']."

    其他每条是
  • ".$row1['title']."

  • 该怎么写?
    下面是自己乱弄的
    for($i=0;$i if(?):
            echo "
  • ".$row1['title']."
  • ";
            elseif(?): 
            echo  "
    • ".$row1['title']."
    • ";
      elseif(?):  // 注意使用了一个单词的 els
              echo "
    • ".$row1['title']."
    ";
            else:
            echo "
  • ".$row1['title']."
  • ";
            endif;
        }
    或者是读取10条信息
    为5个一组
    输出是

    • 1

    • 2

    • 3

    • 4

    • 5



    • 6

    • 7

    • 8

    • 9

    • 10


    该怎么写?


    回复讨论(解决方案)

    基本算法

    $a = array(1,2,3,4,5,6,7,8,9,10);for($i=0; $i<10; $i++) {  if($i % 5 == 0)    echo ($i ? '</ul>' : '') . "<ul>\n";  echo "<li>$i</li>\n";}echo '</ul>';
    <ul><li>0</li><li>1</li><li>2</li><li>3</li><li>4</li></ul><ul><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li></ul>

    $row = array(0,1,2,3,4,5,6,7,8,9,10,11);foreach (array_chunk($row, 5) as $val){    echo "<ul>"."\n";    foreach ($val as $v){        echo "<li>" . $v . "</li>"."\n";    }    echo "</ul>"."\n";}


    <ul><li>0</li><li>1</li><li>2</li><li>3</li><li>4</li></ul><ul><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li></ul>
    • 10
    • 11

    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