
本文旨在解决在PHP中,根据表格行中两列的值是否相等,动态禁用对应按钮的问题。通过修改循环遍历数据并生成HTML表格的代码,在生成按钮时增加条件判断,实现当mi_name列和item_name列的值相等时,禁用该行的按钮。文章提供两种实现方式,并附带代码示例,帮助开发者快速实现此功能。
在PHP中动态禁用HTML表格中的按钮,通常涉及到在服务器端生成HTML代码时,根据特定条件来决定是否添加disabled属性。以下是一种实现方法,假设你已经从数据库中获取了数据并存储在数组中。
核心思路:
在循环遍历数据并生成HTML表格时,在生成按钮的代码中添加条件判断。如果mi_name列的值等于item_name列的值,则为按钮添加disabled属性。
立即学习“PHP免费学习笔记(深入)”;
实现步骤:
代码示例 1:使用if...else语句
<?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($con));
$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($con));
//print_r($Info);
$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){
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>";
if($data['mi_name'] == $data['item_name']) {
echo "<td><button type='buttton' class='disabled' disabled>Compare me!</button></td>";
} else {
echo "<td><button type='buttton'>Compare me!</button></td>";
}
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>代码示例 2:使用三元运算符
可以使用更简洁的三元运算符来实现相同的功能:
<?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($con));
$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($con));
//print_r($Info);
$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){
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>";
echo "<td><button type='buttton' ".($data['mi_name'] == $data['item_name'] ? "class='disabled' disabled" : "").">Compare me!</button></td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>代码解释:
注意事项:
总结:
通过在服务器端生成HTML代码时,根据条件动态添加disabled属性,可以轻松实现在PHP中动态禁用HTML表格中的按钮。 使用 if...else 语句或三元运算符都能达到目的,选择哪种方式取决于个人偏好和代码的可读性要求。 此外,为了更好的用户体验,建议配合CSS样式来区分启用和禁用的按钮。
以上就是PHP:根据条件动态禁用表格中的按钮的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号