Skip to content Skip to sidebar Skip to footer

How To Disable Button When There Are No Records Column Using Php?

I want to disable the button if there are no records in a specific column in the database. I have two buttons in echo statement View and cancel. I am retrieving the value from the

Solution 1:

<?phpif (isset($all_records->num_rows) > 0) {
    // output data of each rowwhile($row = $all_records->fetch_assoc()) {
        $name=$row['name'];
        $email=$row['email'];
        $mobile=$row['mobile']; 
        $emailBtn = "<a href='' class='btn'>view</a>";
        $mobileBtn = "<a href='' class='btn'>cancel</a>";
        if (empty($email)) {
            $emailBtn = "<a disabled href='' class='btn'>view</a>";
        }
        if (empty($mobile)) {
            $mobileBtn = "<a disabled href='' class='btn'>cancel</a>";
        }

        echo"
            <tr>
            <td>{$name}</td>
            <td>{$email}</td>
            <td>{$mobile}</td>
            <td class='in_set_btn'>".$emailBtn.$mobileBtn."</td>
            </tr>
        ";
   }
?>

Solution 2:

<th>Name</th><th>Email</th><th>Mobile</th><th>action</th><?phpif (isset($all_records->num_rows) > 0) {
            // output data of each rowwhile($row = $all_records->fetch_assoc()) {
                 $name=$row['name'];
                 $email=$row['email'];
                $mobile=$row['mobile']; 
        $disable = '';
                if ($email == 0 || $mobile==0) {
                    $disable = "disabled";
                    }
                else{
                    $disable = "";
                }   

                echo"
                <tr>
                <td>{$name}</td>
                <td>{$email}</td>
                <td>{$mobile}</td>
                <td class='in_set_btn'><a href='' {$disable} class='btn'>view</a> <a href='' {$disable} class='btn'>Cancel</a></td>
                </tr>
          ";
           }
          }
        ?>

Solution 3:

Just use a ternary condition (?:) for define a variable $disable.Like this

$disable=  (empty($email)|| empty($mobile))?"disabled":" ";//if one of the column is empty return disabled

then add class in your button like this..

<td class='in_set_btn'><a href='' class='btn'".$disable.">view</a> <a href=''  class='btn'".$disable.">Cancel</a></td>

OR

<tdclass='in_set_btn'><ahref=''class='btn' {$disable}>view</a><ahref=''class='btn' {$disable}>Cancel</a></td>

Post a Comment for "How To Disable Button When There Are No Records Column Using Php?"