本文旨在解决在PHP表格中,根据特定两列的值是否相等来动态禁用按钮的问题。通过修改循环生成表格行的代码,添加条件判断,当mi_name列和item_name列的值相等时,禁用对应行的按钮。文章提供了两种实现方式,包括使用if…else语句和更简洁的三元运算符,并附带示例代码,帮助开发者快速实现该功能。

在PHP中,动态控制HTML元素的属性是常见的需求。本文将介绍如何在生成表格时,根据两列的值是否相等来动态禁用按钮。 这在数据比较、权限控制等场景下非常有用。

实现方法

核心思路是在循环生成表格行的过程中,对mi_name和item_name这两列的值进行比较。如果相等,则为按钮添加disabled class,从而禁用按钮。

立即学习“PHP免费学习笔记(深入)”;

方法一:使用 if…else 语句

这种方法比较直观,易于理解。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

<?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());

     //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'>Compare me!</button></td>";

            } else {

                echo "<td><button type='buttton'>Compare me!</button></td>";

            }

            echo "</tr>";

        }

        echo "</table>";

?>

在上面的代码中,关键部分是if($data[‘mi_name’] == $data[‘item_name’])这个条件判断。如果mi_name和item_name相等,则输出带有disabled class的按钮,否则输出普通的按钮。

方法二:使用三元运算符

表格形态的AI工作流搭建工具,支持批量化的AI创作与分析任务,接入DeepSeek R1满血版

飞书多维表格26

查看详情飞书多维表格

三元运算符可以简化代码,使代码更简洁。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

<?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());

     //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'" : "").">Compare me!</button></td>";

            echo "</tr>";

        }

        echo "</table>";

?>

这两种方法最终的效果是一样的。$data[‘mi_name’] == $data[‘item_name’] ? ” class=’disabled'” : “” 这段代码的意思是:如果$data[‘mi_name’]等于$data[‘item_name’],则返回” class=’disabled'”,否则返回空字符串。

注意事项

  • 确保你的CSS样式中定义了.disabled class,用于禁用按钮的样式。 例如:

    1

    2

    3

    4

    .disabled {

        opacity: 0.5; /* 降低透明度 */

        cursor: not-allowed; /* 改变鼠标指针 */

    }

  • 仅仅通过CSS的disabled class禁用按钮,并不能阻止用户通过其他方式(例如:开发者工具)来触发按钮的事件。为了更安全地禁用按钮,建议在服务器端也进行相应的权限验证。

总结

本文介绍了两种在PHP表格中根据列值动态禁用按钮的方法,分别是使用if…else语句和三元运算符。您可以根据自己的喜好和代码风格选择合适的方法。 记住,前端的禁用只是用户体验上的限制,真正的安全控制需要在后端进行。

以上就是PHP表格:根据列值动态禁用按钮的详细内容

分类: PHP

0 条评论

发表回复

Avatar placeholder

您的邮箱地址不会被公开。 必填项已用 * 标注