drouie2014 2014-02-15 09:13
浏览 73

如何添加自动增量编号来选择标签?

$x = date("");

function add_nol($number,$add_nol)
{
   while (strlen($number)<$add_nol)
   {
      $number = "0".$number;
   }

   return $number;
}

for($y=10;$y<=50;$y++)
{
   echo "<select name='id'>";
   echo "<option value='". $x."".add_nol($y,3)."'>" . $x."".add_nol($y,3) ."</option>";
   echo "</select>";
}

I want to add auto increment numbers into select tag, but I am getting drop down for each numbers from the above code. How to fix this error?

  • 写回答

2条回答 默认 最新

  • dougan1465 2014-02-15 09:15
    关注

    Two things to remember:

    • The <select> element is used to create a drop-down list.
    • The <option> tags inside the <select> element define the available options in the list.

    With your current code, the generated HTML markup would be similar to this:

    <select name='id'>
        <option value='010'>010</option>
    </select>
    <select name='id'>
        <option value='011'>011</option>
    </select>
    ...
    

    This is incorrect. You are creating a separate dropdown on each loop iteration. You only need one <select>tag - they should go outside the loop.

    echo "<select name='id'>";
    for($y=10;$y<=50;$y++)
    {
        echo "<option value='". $x."".add_nol($y,3)."'>" . $x."".add_nol($y,3) ."</option>";
    }
    echo "</select>";
    

    Demo

    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号