输入文本的唯一ID和NAME

时间:2017-12-14 09:43:44

标签: php increment

我将MySQL数据输出到表。我需要设置<input>的唯一ID和名称。当前ID为brop,但我想添加增量编号(brop1brop2,...)

<td><input type="text" id="brop" name="brop" value="'.$row["broj_operacije"].'"></td>

我尝试了以下代码,但它不起作用:

$key = 1;
<td><input type="text" id="brop'. $key++ .' name="brop'. $key++ .'" value="'.$row["broj_operacije"].'"></td>

3 个答案:

答案 0 :(得分:2)

您必须为循环外的行号声明变量(for / foreach)。您还应该在循环体的开头增加一次行号。在您的情况下,您在一行中多次递增行号:

$rowNumber = 0;

foreach ($rows as $row) {
    $rowNumber++;
    echo '<td><input type="text" id="brop'.$rowNumber.'" name="brop'.$rowNumber.'" value="'.$row["broj_operacije"].'"></td>';
}

使用for循环的另一种解决方案:

for ($rowNumber = 0; $rowNumber < count($rows); $rowNumber++) {
    $row = $rows[$rowNumber];
    echo '<td><input type="text" id="brop'.$rowNumber + 1.'" name="brop'.$rowNumber + 1.'" value="'.$row["broj_operacije"].'"></td>';
}

在这种情况下,您不需要额外的变量来保存行号。

答案 1 :(得分:1)

$键= 1;应该在while / for循环之外声明

$key=1;
foreach ($rows as $row) {
    echo '<td><input type="text" id="brop'. $key++ .' name="brop'. $key++ .'" value="'.$row["broj_operacije"].'"></td>';
}

答案 2 :(得分:0)

<?php
  $i=1;
   while($fetch_data=mysql_fetch_assoc($sql_query))
    {
       echo '<tr>
                <td>
                   <input type="text" id="brop'.$i.'" name="brop'.$i.'">
                </td>           
              </tr>';
         $i++;
    }
 ?>     
相关问题