0

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 database and displaying. I want to disable button of that row which is no records. Would you help me in this?

<th>Name</th>  
 <th>Email</th>
 <th>Mobile</th>
  <th>action</th>



  <?php
    if (isset($all_records->num_rows) > 0) {
    // output data of each row
    while($row = $all_records->fetch_assoc()) {
         $name=$row['name'];
         $email=$row['email'];
        $mobile=$row['mobile']; 

        if ($email == 0 || $mobile==0) {
            //disable btn
            }
        else{
            //enable btn
        }   

        echo "
        <tr>
        <td>{$name}</td>
        <td>{$email}</td>
        <td>{$mobile}</td>
        <td class='in_set_btn'><a href='' class='btn'>view</a> <a href=''  class='btn'>Cancel</a></td>
        </tr>
  ";
   }
  }
  • Explain better becasue your code echo only existing rows .. – ScaisEdge Jan 28 '17 at 13:08
  • Sure, I have 4 column in my table called as Id, Name, Email, Mobile and i am retrieve all records and also displaying button. In case for row, Email and Mobile column is Null or empty then i want to disable the button –  Jan 28 '17 at 13:14
  • I assume `$email` and `$mobile` are string variables. So `if ($email == 0)` is wrong. What you have to do is: `if (empty($email) || empty($mobile)) {` – EhsanT Jan 28 '17 at 13:28
  • Thanks for replying Mr.EhsanT, Thanks for information.I tried that but same issue –  Jan 28 '17 at 13:31
  • So, you want to check each record and it email or mobile was empty, then disable "view" and "cancel" links? If so how do you want to disable them? you want to show them but they do not have any links, or you do not want to show them or what? – EhsanT Jan 28 '17 at 13:35
  • @Hybreeder use ternary operator.Just one line code is enough for you. – Hikmat Sijapati Jan 28 '17 at 13:53
  • Can you try now with modified code. It should work – Yogesh Kumar Gupta Jan 28 '17 at 14:08

3 Answers3

0
<?php
    if (isset($all_records->num_rows) > 0) {
    // output data of each row
    while($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>
        ";
   }
?>
Yogesh Kumar Gupta
  • 1,009
  • 1
  • 10
  • 10
  • Thank for replying Mr.Yougesh, I tried your code. Not working –  Jan 28 '17 at 13:21
  • You want both the buttons to be hidden in case it is not valid?? – Yogesh Kumar Gupta Jan 28 '17 at 13:24
  • Yes, or any one, Incase mobile and email fields are blank –  Jan 28 '17 at 13:25
  • Can you please clearly tell which button need to be shown and when. If mobile is not valid but email is valid, what should be the behavior or viceversa. Also, if both are valid, what should be there?? – Yogesh Kumar Gupta Jan 28 '17 at 13:34
  • Mr. Yogesh Kumar, incase email is empty than view hide and mobile is empty tha cancel hide. –  Jan 28 '17 at 13:38
  • I have removed the button's code all together from document. In case you want to keep the button and hide them on the page, just give them a class of disabled and use css to display: none; or just add attribute disabled and you will no longer be able to click on the button – Yogesh Kumar Gupta Jan 28 '17 at 13:44
  • I tried your code it is working almost but little bit issue, button totally hide, I want to disable the button so user can see it but not able to clicked –  Jan 28 '17 at 13:51
  • disabled will not work with anchor tag. using css we can do.. Thanks for help Mr.Yogesh. it is working for me. Upvote from my side –  Jan 28 '17 at 14:20
0
        <th>Name</th>  
         <th>Email</th>
         <th>Mobile</th>
          <th>action</th>



          <?php
            if (isset($all_records->num_rows) > 0) {
            // output data of each row
            while($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>
          ";
           }
          }
        ?>
Kaushik solanki
  • 438
  • 3
  • 15
0

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

<td class='in_set_btn'><a href='' class='btn' {$disable}>view</a> <a href=''  class='btn' {$disable}>Cancel</a></td>
Hikmat Sijapati
  • 6,869
  • 1
  • 9
  • 19