dreamfly0514 2013-08-27 17:45
浏览 11
已采纳

以变​​量名增加数字

Background

I have a for loop creating a table of inputs for an html form:

for ($i = 1; $i <= $x; $i++) {
  echo '<select name="waldo_'.$i.'" id="waldo_'.$i.'">
      <option value="">...</option>
      <option value="foo">Foo</option>
      <option value="bar">Bar</option>
    </select>
    <label for="foo_'.$i.'">Foo '.$i.'</label>
    <input id="foo_'.$i.'" type="text" value="" name="foo_'.$i.'">
    <label for="bar_'.$i.'">Bar '.$i.'</label>
    <input id="bar_'.$i.'" type="text" value="" name="bar_'.$i.'">';
}

On submit, this populates a database.

Problem

Each submission needs to be editable. When I return to the form (as an admin) I need to see everything that was stored in the database by a particular user.

for ($i = 1; $i <= $x; $i++) {
  echo '<select name="waldo_'.$i.'" id="waldo_'.$i.'">
      <option value="">...</option>
      <option value="foo"';
  if($row['waldo_'.$i] == "foo") echo " selected='selected'";
  echo '>Foo</option>
      <option value="bar"';
  if($row['waldo_'.$i] == "bar") echo " selected='selected'"; 
  echo '>Bar</option>
    </select>
    <label for="foo_'.$i.'">Foo '.$i.'</label>
    <input id="foo_'.$i.'" type="text" value="'./*...*/.'" name="foo_'.$i.'">
    <label for="bar_'.$i.'">Bar '.$i.'</label>
    <input id="bar_'.$i.'" type="text" value="'./*...*/.'" name="bar_'.$i.'">';
}

My select properly "selects" the correct option, but I don't seem to be able to populate the text input values in a similar manner.
Somehow I need to echo the content in $foo_1, $foo_2, $foo_3, ..., $foo_x.

I have tried using $foo_.$i but that doesn't seem to work.

Is there a simple solution to this problem? Or is there a better method to format everything?

  • 写回答

1条回答 默认 最新

  • dpsq8476 2013-08-27 17:50
    关注

    If I'm not misunderstanding your question:

    $_POST["foo_".$i]
    

    Should show you the submitted data.

    EDIT: Or maybe this is what you are looking for?

    for ($i = 1; $i <= $x; $i++) {
      echo '<select name="waldo_'.$i.'" id="waldo_'.$i.'">
          <option value="">...</option>
          <option value="foo"';
      if(isset($row['waldo_'.$i]) && $row['waldo_'.$i] == "foo") echo " selected='selected'";
      echo '>Foo</option>
          <option value="bar"';
      if(isset($row['waldo_'.$i]) && $row['waldo_'.$i] == "bar") echo " selected='selected'"; 
      echo '>Bar</option>
        </select>
        <label for="foo_'.$i.'">Foo '.$i.'</label>
        <input id="foo_'.$i.'" type="text" value="';
      if(isset($row['foo_'.$i]) && $row['foo_'.$i] != "") echo $row['foo_'.$i];
        echo '" name="foo_'.$i.'">
        <label for="bar_'.$i.'">Bar '.$i.'</label>
        <input id="bar_'.$i.'" type="text" value="';
      if(isset($row['bar_'.$i]) && $row['bar_'.$i] != "") echo $row['bar_'.$i];
        echo '" name="bar_'.$i.'">';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法