PHP表格行mi_name与item_name值相等时禁用对应行按钮的实现问题
实现方法
你只需要在循环输出按钮的位置增加相等判断逻辑,满足条件时给按钮添加disabled禁用属性即可,同时我还修正了你原有代码里按钮type属性拼写错误、表格属性缺引号的小问题。
修改后的完整代码
<?php require_once('conn.php'); $sql_count="SELECT COUNT(mi_number) FROM a_items z INNER JOIN m3data_items_all a ON (a.mi_number =z.item_number) where plan_id=11 "; $Info_count = mysqli_query($con, $sql_count) or die(mysqli_error()); $row_Info_count = mysqli_fetch_all($Info_count,MYSQLI_ASSOC); $sql_row="SELECT mi_number,item_number, mi_name,item_name,mi_description,item_description,plan_id FROM a_items z INNER JOIN m3data_items_all a ON (a.mi_number =z.item_number) where plan_id=11 "; $Info_data = mysqli_query($con, $sql_row) or die(mysqli_error()); $row_Info_data = mysqli_fetch_all($Info_data,MYSQLI_ASSOC); echo "<div><h2>Count : ".$row_Info_count[0]['COUNT(mi_number)']."<h2></div><table border='1px' cellpadding='5px' cellspacing='0px'> <h1>ALL FETCH DATA</h1> <tr> <th>mi_number</th> <th>item_number</th> <th>mi_name</th> <th>item_name</th> <th>mi_description</th> <th>item_description</th> <th>plan_id</th> </tr>"; foreach($row_Info_data as $data){ // 核心判断:mi_name和item_name相等则给按钮加禁用属性 $disableAttr = $data['mi_name'] == $data['item_name'] ? 'disabled' : ''; echo "<tr> <td>".$data['mi_number']."</td> <td>".$data['item_number']."</td> <td>".$data['mi_name']."</td> <td>".$data['item_name']."</td> <td>".$data['mi_description']."</td> <td>".$data['item_description']."</td> <td>".$data['plan_id']."</td> <td><button type='button' {$disableAttr}>Compare me!</button></td> </tr>"; } echo "</table>"; ?>
核心逻辑说明
- 遍历每行数据时通过三目运算符做值比较,两个字段值相等时
$disableAttr变量值为disabled,否则为空 - 将变量插入到button标签的属性位置,即可实现符合条件时自动禁用按钮,不需要额外的JS逻辑
内容的提问来源于stack exchange,提问作者Pruthvi mohan
相关产品推荐
相关产品推荐

