Home > Backend Development > PHP Tutorial > 关于循环判断的解决方案

关于循环判断的解决方案

WBOY
Release: 2016-06-13 12:17:12
Original
919 people have browsed it

关于循环判断的
比如我从数据表里读取了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);<br />for($i=0; $i<10; $i++) {<br />  if($i % 5 == 0)<br />    echo ($i ? '</ul>' : '') . "<ul>\n";<br />  echo "<li>$i</li>\n";<br />}<br />echo '</ul>';<br />
    Copy after login
    <ul><br /><li>0</li><br /><li>1</li><br /><li>2</li><br /><li>3</li><br /><li>4</li><br /></ul><ul><br /><li>5</li><br /><li>6</li><br /><li>7</li><br /><li>8</li><br /><li>9</li><br /></ul><br />
    Copy after login

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


    <br /><ul><br /><li>0</li><br /><li>1</li><br /><li>2</li><br /><li>3</li><br /><li>4</li><br /></ul><br /><ul><br /><li>5</li><br /><li>6</li><br /><li>7</li><br /><li>8</li><br /><li>9</li><br /></ul><br /><ul><br /><li>10</li><br /><li>11</li><br /></ul><br />
    Copy after login
    Related labels:
    source:php.cn
    Statement of this Website
    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
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template