Jump to content
elentz

How can I change a button using a variable

Recommended Posts

I have a page that I am using on a Raspberry Pi that I use for turning relays on and off.  I  have a piece of code that I want to be able to change the button in the form by the status of (in this case) the input of whether the door is closed or not.  Here is the code as it is now:

<?php
$state[0] = "Closed";
$state[1] = "Open";
$pinStatus = trim(shell_exec("gpio -g read 21"));
echo "<h3 style=\"font-color: #ff0000;\">South Door -  $state[$pinStatus]</h3>"; 
?>
</h3></th>
<th><h3>Relay Page</h3></th>

                </tr>

                <tr>
                    <th><h3><form action= "" method="post">
    <p>
        <button name="northdoor"; button type="submit" style="background-color:transparent; border-color:transparent;"> 
        <img src="imgs/On_Button.png" height="125"/>
        </button>
    </p>
    </form>
</h3></th>

When the status is Closed I want the button to be Off_Button.png and when it is Open I want it to be On_button.png.  I am getting the status of the door to another part of this page.

 

Thanks for any ideas.

Share this post


Link to post
Share on other sites

See how you used $state to decide whether to say the door is open or closed? Do the same thing but with the image you want to show.

 

This is pretty straightforward. Try it and post your code if you have problems.

Share this post


Link to post
Share on other sites

Thanks,  I am real close,  I get one image and it doesn't change with the state[status]  If I flip the $state[0] to $state[1] then the button changes.

<?php
$state[0] = "imgs/On_Button.png";
$state[1] = "imgs/Off_Button.png";
$pinStatus = trim(shell_exec("gpio -g read 20"));
echo "<h3 style=\"font-color: #ff0000;\">North Door -  $state[$pinStatus]</h3>"; 
?>
</th>
<th><h3>
<?php
$state1[0] = "Closed";
$state1[1] = "Open";
$pinStatus = trim(shell_exec("gpio -g read 21"));
echo "<h3 style=\"font-color: #ff0000;\">South Door -  $state1[$pinStatus]</h3>"; 
?>
</h3></th>
<th><h3>Relay Page</h3></th>

                </tr>

                <tr>
                    <th><h3><form action= "" method="post">
    <p>
        <button name="northdoor"; button type="submit" style="background-color:transparent; border-color:transparent;"> 
        <img src="<?php echo $state[$pinStatus]?>" height="125"/>
        </button>
    </p>
    </form>

Almost there,  what am I missing?

Share this post


Link to post
Share on other sites

Almost there,  what am I missing?

 

Basic programming knowledge, it seems.

 

Never heard of the if statement?

if ($some_condition)
{
    // code to be executed in case $some_condition is true
}
else
{
    // code to be executed in case $some_condition is false
}

There's also the ternary operator which yields different expressions depending on whether a condition is fulfilled or not:

$some_condition ? $value_for_true_case : $value_for_false_case

I'm sure you can now figure out how to display different buttons depending on the value of $pinStatus.

Share this post


Link to post
Share on other sites

You're reading from a different pin now?

 

It seems like you want to say Open/Closed and also change the image being used, right? Here's something simple:

function get_door_state($pin) {
	$pinStatus = trim(shell_exec("gpio -g read {$pin}"));
	return [
		// "key" => [0 value, 1 value][$pinStatus],
		"onoff" => ["On", "Off"][$pinStatus], // is this right? 0=on and 1=off?
		"openclosed" => ["Closed", "Open"][$pinStatus]
	];
}
$north = get_door_state(20);
$south = get_door_state(21);
<h3 style="font-color: #ff0000;">North Door - <?=$north["openclosed"]?></h3>
<img src="imgs/<?=$north["onoff"]?>_Button.png" height="125">

Share this post


Link to post
Share on other sites
requinix

Not a different pin, I actually have two to read and act on.  I used only one to work with until it works then I was going to apply what I got help with or figured out myself.  I originally used  echo "<h3 style=\"font-color: #ff0000;\">South Door - $state1[$pinStatus]</h3>";  to indicate the status of the door.  I want to use the status to change the button(s).  The buttons are just a red and green power icon.  I do not need to echo the status if I can change the button.  Yes the status returned is a 1 = off and a 0 for on.

 

Thanks

Share this post


Link to post
Share on other sites

requinix

 

That bit of code worked beautifully.  And I think I halfway understand what is going on too!

 

Thank you so much

Share this post


Link to post
Share on other sites

The most complicated part of that is

// "key" => [0 value, 1 value][$pinStatus],
"onoff" => ["On", "Off"][$pinStatus], // is this right? 0=on and 1=off?
"openclosed" => ["Closed", "Open"][$pinStatus]
The [] syntax is shorthand for array(), and doing two in a row like that first creates an array and then immediately pulls an item out of it.

echo ["zero", "one", "two", "three"][2]; // two

Share this post


Link to post
Share on other sites

KathyJohnson,

 

Please learn to use code tags for your posts here.

Share this post


Link to post
Share on other sites

Please be aware that the original post was made nearly a year ago.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.