Gina
Gina

Reputation: 85

Disable button on conditions

I have a buttons which are displayed from sql query:

     $username = new User();
    $name = $username->data()->username;
    $sql1 = DB::getInstance()->query("SELECT names FROM list WHERE username = '$name'");
   if (!$sql1->count()) {
        echo 'No data';
    } else {
        foreach ($sql1->results() as $sql1) {
            ?>

                                    <p><button class="" > <?php echo $sql1->names; ?></button></p>
                                    <?php                            
                             }
                          }

This displays two buttons which match the conditions from the query, so I'm trying to disable one of the displayed buttons if it doesn't match another condition. For example, there are two buttons, John and Poodle. And a query to match if one of the buttons is an animal.

So if the button john does not match the query, it should be disabled.

Upvotes: 0

Views: 1880

Answers (3)

Frankie
Frankie

Reputation: 499

To disable a button ith php+html, you can try with a query value returned "disabled". For example: in your query use a CASE WHEN statement to return disabled value when your condition is defined:

SELECT filed1,
CASE 
    WHEN field1= '19' THEN 'disabled'
    WHEN field1='20' THEN ''
    WHEN filed1='21' THEN ''
    WHEN field1='22' THEN ''
    ELSE 'disabled'
END AS for_button 
FROM table1 AS tbl1 
WHERE tbl1.field1_param = '$param'

Put in the HTML TAG button this:

<input type="submit" value="Button OnOff" <?=$disableButton['for_button']; ?>>

If the value returned is disabled, in the for_button variable, write the valure returned from query and the button will are disabled.

Upvotes: 0

arkascha
arkascha

Reputation: 42885

I will try to express the idea sketched in the comments to the question, as you asked me to and explain it step by step:

foreach ($sql1->results() as $set) {
    echo sprintf('<p><button %s>%s</button></p>'."\n", 
        in_array($set->name, array('poodle','cat','sheep')) ? 'disabled' : '',
        $set->name);
}

Here $set is a different object (set of attributes) for each iteration of the foreach loop. You said you have two entries in that queries result, so two buttons get generated. Each $set has a name ($set->name) if you understand your code correct (I don't know your database...). This name is used twice for generating each button: first the name is used as text in the button and second it is used in a conditional to decide if the button should be disabled or not. That condition is implemented as a trinary expression, line 3 in the example above. In the line a function is called: in_array(). That returns true or false. If true, then the attribute "disabled" is added to the button, if false then the empty string ('') is added instead, so the button does not have the disabled property.

This is obviously not finished code. It is meant to give you the idea, so you should understand it, not just copy and try it. Feel free to ask if questions arise!

Upvotes: 1

varunsinghal
varunsinghal

Reputation: 329

Try the following:

<button class="<?php if($sql1->names != "required_name") echo "disabled" ?>">
<?php echo $sql1->names; ?> 
</button>

Upvotes: 0

Related Questions