简体   繁体   English

将ID分配给在while循环内使用PHP回显的字段

[英]Assigning ID's to fields that are echoed using PHP inside a while loop

I currently have two dropdown menus that a user will select the course and a golfer which will load a scorecard. 我目前有两个下拉菜单,用户可以选择球场,而高尔夫球手则可以加载计分卡。

What I am trying to do now is when a user enters a value into the "Score" field I want the "points" to be automatically shown ("Points" is a read only input field). 我现在想做的是,当用户在“分数”字段中输入一个值时,我希望自动显示“分数”(“分数”是一个只读输入字段)。 To do this I am assuming that I will have to give each table row for score and points a specific ID. 为此,我假设我将不得不为每个表行提供分数并指定一个特定的ID。

echo "<div class='scorecardTable'>
<table 'id=scorecardTable'>
<tr>
<th>HoleNumber</th>
<th>Par</th>
<th>Stroke Index</th>
<th>Score</th>
<th>Points</th>
</tr>'";

while($row = mysqli_fetch_array($result)) { 
    echo "<tr>";
    echo "<td>" . $row['holeNumber'] . "</td>";
    echo "<td>" . $row['par'] . "</td>";
    echo "<td>" . $row['strokeIndex'] . "</td>";
    echo "<td> <input type='text' maxlength='2' id='score' /></td>";
    echo "<td> <input type='text' id='points' readonly></td>";
    echo "</tr>";
}
echo "</table>";

From the above code I am using PHP to print the information from my database but I was just wondering if anyone would know how to assign a unique ID to each of the score and points input fields so I can apply the calculation to each user input. 从上面的代码中,我正在使用PHP从数据库中打印信息,但我只是想知道是否有人会知道如何为每个得分和分数输入字段分配唯一的ID,以便将计算应用于每个用户输入。

You could use a variable to count the rows and set the id. 您可以使用变量来计数行并设置ID。

$rowIndex = 0;
while($row = mysqli_fetch_array($result)) { 
    echo "<tr>";
    echo "<td>" . $row['holeNumber'] . "</td>";
    echo "<td>" . $row['par'] . "</td>";
    echo "<td>" . $row['strokeIndex'] . "</td>";
    echo "<td> <input type='text' maxlength='2' id='score$rowIndex' /></td>";
    echo "<td> <input type='text' id='points$rowIndex' readonly></td>";
    echo "</tr>";
    $rowIndex++;
}

You may also use 您也可以使用

id='score[$rowIndex]'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM