You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP循环渲染手指表格时,无法按手指数量生成对应输入框

问题:根据手指数量动态生成对应输入框失败

问题背景

我需要为10根手指各展示3张图片(共30张),通过PHP动态循环在表格中渲染,要求每根手指对应一个输入框(比如2根手指就显示2个输入框)。已经通过json_decode解析对象数组并使用foreach循环输出,但当前仅显示一个输入框,无法根据数组值生成对应数量的输入框。

我的数据数组

Array ( 
    [0] => stdClass Object ( [code] => 0 [message] => Success [bio_id] => 1 [ref_id] => 2 [hand] => 1 [fin] => 1 [poz] => 1 ) 
    [1] => stdClass Object ( [code] => 0 [message] => Success [bio_id] => 1 [ref_id] => 2 [hand] => 1 [fin] => 1 [poz] => 2 ) 
    [2] => stdClass Object ( [code] => 0 [message] => Success [bio_id] => 1 [ref_id] => 2 [hand] => 1 [fin] => 1 [poz] => 3 ) 
    [3] => stdClass Object ( [code] => 0 [message] => Success [bio_id] => 1 [ref_id] => 2 [hand] => 1 [fin] => 2 [poz] => 1 ) 
    [4] => stdClass Object ( [code] => 0 [message] => Success [bio_id] => 1 [ref_id] => 2 [hand] => 1 [fin] => 2 [poz] => 2 ) 
    [5] => stdClass Object ( [code] => 0 [message] => Success [bio_id] => 1 [ref_id] => 2 [hand] => 1 [fin] => 2 [poz] => 3 ) 
)

当前代码

$finger = json_decode($blob->getallbiofile()); //print_r($finger);
$i = 0;
foreach($finger as $fing) {
    if($fing->code == '0'){
        $i++;
?>
<tr>
    <td class="">
        <?php echo $finhelp->fingurevalue("hand", $fing->hand); ?> - <?php echo $finhelp->fingurevalue("fin", $fing->fin); ?> - <?php echo $finhelp->fingurevalue("poz", $fing->poz); ?></td>
    <td class="center">
        <a href="showfinger?id=<?=$fing->bio_id?>" target="_blank">
            <img src="images/fingerprint.png" class="img-circle img-responsive reportfingerimage"></a>
    </td>
    <?php if($i == $fing->fin) { ?>
    <td class="" rowspan="3">
        <div class="col-xs-2">
            <input type="hidden" name="hand[]" value="<?=$fing->hand?>">
            <input type="hidden" name="finger[]" value="<?=$fing->fin?>">
            <input class="form-control fininput" id="ex1" name="finger_value[]" type="text" value="">
        </div>
    </td>
    <?php continue; } ?>
</tr>
<?php
    }
}
?>

问题现象

当前仅显示一个输入框,但预期为第二根手指也生成一个输入框。


问题分析

你的逻辑里用$i == $fing->fin来判断是否显示输入框,但$i是循环的累计计数(从1到6),而$fing->fin的值是1和2,只有当$i=1和$i=2时才会触发,但$i=2对应的是fin=1的第二张图片,此时输出输入框后用continue跳过了闭合</tr>,导致表格结构混乱,而且后续fin=2的行里$i已经是4、5、6,永远不会等于2,所以第二根手指的输入框不会显示。

正确的思路应该是按手指分组,每3行(对应同一手指的3张图片)只在第一行显示带rowspan="3"的输入框单元格,后续两行不输出这个单元格。

修正后的代码

我们可以先记录上一个手指的fin值,只有当当前手指的fin和上一个不同时,才输出输入框单元格,否则跳过:

$finger = json_decode($blob->getallbiofile());
$last_fin = null; // 记录上一个手指的fin值
foreach($finger as $fing) {
    if($fing->code != '0') continue; // 跳过错误数据
?>
<tr>
    <td class="">
        <?php echo $finhelp->fingurevalue("hand", $fing->hand); ?> - <?php echo $finhelp->fingurevalue("fin", $fing->fin); ?> - <?php echo $finhelp->fingurevalue("poz", $fing->poz); ?>
    </td>
    <td class="center">
        <a href="showfinger?id=<?=$fing->bio_id?>" target="_blank">
            <img src="images/fingerprint.png" class="img-circle img-responsive reportfingerimage">
        </a>
    </td>
    <?php if($fing->fin != $last_fin) { ?>
        <td class="" rowspan="3">
            <div class="col-xs-2">
                <input type="hidden" name="hand[]" value="<?=$fing->hand?>">
                <input type="hidden" name="finger[]" value="<?=$fing->fin?>">
                <input class="form-control fininput" id="finger_<?=$fing->fin?>" name="finger_value[]" type="text" value="">
            </div>
        </td>
        <?php $last_fin = $fing->fin; // 更新上一个fin值 ?>
    <?php } ?>
</tr>
<?php
}
?>

关键改进点

  1. 新增$last_fin变量跟踪当前处理的手指ID,确保每个手指只在第一行输出输入框单元格,并设置rowspan="3"覆盖3行。
  2. 移除了错误的$i计数逻辑,避免和手指ID混淆。
  3. 修正了输入框的id属性,确保每个输入框ID唯一(原代码里所有输入框都用id="ex1"不符合HTML规范)。
  4. 优化了条件判断,直接跳过code != 0的数据,代码更简洁。

这样处理后,每个手指(fin=1、fin=2...)都会对应生成一个输入框,且正确跨3行显示,表格结构也不会混乱。

内容的提问来源于stack exchange,提问作者Kuntal Parbat

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:52:44