My code is not working. I'm trying to disable a button based on data from a table cell. This is an example of my table.
Table A (tickets)
*----------------*
| id | repair_id |
| 1 | 10, 11 |
| 2 | 12 |
*----------------*
and my current code:
<?php
$sql = "SELECT repair_id FROM tickets WHERE t_id =".$ticket_id." ";
$sql .= "AND repair_id REGEXP ',' ";
$box_check = mysqli_query($connection, $sql);
$box_empty = mysqli_fetch_array($box_check);
if(count($box_empty) == 0) {
echo "<button class='btn btn-primary' disabled='disabled' name='add_box'>Add Box</button>";
} else {
echo "<button class='btn btn-primary' type='submit' name='add_box'>Add Box</button>";
}
?>
My goal is to allow the box to be active so long as there is more than 1 id
in repair_id
.
Currently the box is disabled or enabled on all depending on if I set if(count($box_empty) == true/false/0/1)
.
Right now, ... REGEXP ', '
selects only those rows that have more than one repair ids. Based on your question,
My goal is to allow the box to be active so long as there is more than 1 id in repair_id.
Use a simple SELECT
query without REGEX
and make use of strpos() function to check whether the repair_id
column contains more than one repair ids or not, and then display the buttons accordingly, like this:
$sql = "SELECT repair_id FROM tickets WHERE t_id =".$ticket_id;
$result = mysqli_query($connection, $sql);
while($row = mysqli_fetch_array($result)){
if(strpos($row['repair_id'], ",") === false) {
echo "<button class='btn btn-primary' disabled='disabled' name='add_box'>Add Box</button>";
} else {
echo "<button class='btn btn-primary' type='submit' name='add_box'>Add Box</button>";
}
}