Skip to content
Advertisement

Hiding a button dependant on cell value in table

I currently have a table that data is being reported into with a few buttons at the end to perform actions (Consign, Unconsign, Edit, Delete). I’m trying to hide the Consign and Unconsign buttons for each row in the table depending on the Yes/No value a column.

So far, I’ve managed to get the button Consign to hide if the consigned cell is Yes. The problem is that for cells returning No, the Consign button is still hidden and the Unconsign button still shows. What would be the best way to fix this?

Here is the code and example so far.

<thead>
     <tr>
       <th data-checkbox="true"></th>
       <th data-field="no" data-sortable="true" data-sorter="dateSorter" data-visible="false">No</th>
       <th data-field="location" data-sortable="true" data-filter-control="select" data-filter-control-placeholder="Select Location">Location</th>
       <th data-field="name" data-visible="false">Name</th> 
       <th data-field="area" data-visible="false">Area</th>
       <th data-field="sendto" data-visible="false">Send To</th>        
       <th data-field="reference">Reference</th>
       <th data-field="consigned" data-filter-control="select" data-filter-default="No">Consigned</th>
       <th data-field="date_consigned" data-visible="false">Date Consigned</th>
       <th data-field="action" class="col-lg-2">Action</th>
     </tr>
</thead>
<tbody> 
<?php

        if ($result->num_rows > 0) {
            while($row = $result->fetch_assoc()) {
              echo '<tr>';
              echo '<td></td>';             
              echo '<td>'.$row["no"].'</td>';
              echo '<td>'.$row["location"].'</td>';
              echo '<td>'.$row["name"].'</td>'; 
              echo '<td>'.$row["area"].'</td>';     
              echo '<td>'.$row["sendto"].'</td>';
              echo '<td>'.$row["reference"].'</td>';
              echo '<td>'.$row["consigned"].'</td>';
              echo '<td>'.$row["date_consigned"].'</td>';
                if ($value['consigned'] != 'Yes') {
              echo "<td>
                        <a href='#consign_".$row['no']."' class='btn btn-warning btn-sm hide' data-toggle='modal' id='consign' name='consign'><span class='glyphicon glyphicon-check'></span> Consign</a>
                        <a href='#unconsign_".$row['no']."' class='btn btn-warning btn-sm' data-toggle='modal' id='unconsign' name='unconsign'><span class='glyphicon glyphicon-check'></span> Unconsign</a>                            
                        <a href='#edit_".$row['no']."' class='btn btn-success btn-sm' data-toggle='modal'><span class='glyphicon glyphicon-edit'></span> Edit</a>
                        <a href='#delete_".$row['no']."' class='btn btn-danger btn-sm' data-toggle='modal'><span class='glyphicon glyphicon-trash'></span> Delete</a>
                    </td>";
                } else {
              echo "<td>
                        <a href='#consign_".$row['no']."' class='btn btn-warning btn-sm' data-toggle='modal' id='consign' name='consign'><span class='glyphicon glyphicon-check'></span> Consign</a>
                        <a href='#unconsign_".$row['no']."' class='btn btn-warning btn-sm hide' data-toggle='modal' id='unconsign' name='unconsign'><span class='glyphicon glyphicon-check'></span> Unconsign</a>                           
                        <a href='#edit_".$row['no']."' class='btn btn-success btn-sm' data-toggle='modal'><span class='glyphicon glyphicon-edit'></span> Edit</a>
                        <a href='#delete_".$row['no']."' class='btn btn-danger btn-sm' data-toggle='modal'><span class='glyphicon glyphicon-trash'></span> Delete</a>
                    </td>";
                }
              echo "</tr>";
                include('edit-delete-modal.php');
              echo "</tr>";
            }
          }
?>

Image of table buttons

Edit: The suggestion here doesn’t work for me as it’s an issue with a php table with many more variables and not a singular div. I have already managed to hide part of what I’m trying to achieve so I know my code works to an extent and the suggested solutions woulnd’t acheive this for me as it would hide everything.

Advertisement

Answer

I found something in your logic (if statement)

 if ($value['consigned'] != 'Yes') { // true when $value['consigned'] is No
          echo "<td>
                    <a href='#consign_".$row['no']."' class='btn btn-warning btn-sm hide' data-toggle='modal' id='consign' name='consign'><span class='glyphicon glyphicon-check'></span> Consign</a>
                    <a href='#unconsign_".$row['no']."' class='btn btn-warning btn-sm' data-toggle='modal' id='unconsign' name='unconsign'><span class='glyphicon glyphicon-check'></span> Unconsign</a>                            
                    <a href='#edit_".$row['no']."' class='btn btn-success btn-sm' data-toggle='modal'><span class='glyphicon glyphicon-edit'></span> Edit</a>
                    <a href='#delete_".$row['no']."' class='btn btn-danger btn-sm' data-toggle='modal'><span class='glyphicon glyphicon-trash'></span> Delete</a>
                </td>";
            } else {
          echo "<td>
                    <a href='#consign_".$row['no']."' class='btn btn-warning btn-sm' data-toggle='modal' id='consign' name='consign'><span class='glyphicon glyphicon-check'></span> Consign</a>
                    <a href='#unconsign_".$row['no']."' class='btn btn-warning btn-sm hide' data-toggle='modal' id='unconsign' name='unconsign'><span class='glyphicon glyphicon-check'></span> Unconsign</a>                           
                    <a href='#edit_".$row['no']."' class='btn btn-success btn-sm' data-toggle='modal'><span class='glyphicon glyphicon-edit'></span> Edit</a>
                    <a href='#delete_".$row['no']."' class='btn btn-danger btn-sm' data-toggle='modal'><span class='glyphicon glyphicon-trash'></span> Delete</a>
                </td>";
            }
          echo "</tr>";
            include('edit-delete-modal.php');
          echo "</tr>";
        }

Suggest:

modify if statement

$value['consigned'] == 'Yes'
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement