Jump to content

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.

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.

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?

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.

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">
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

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
  • 8 months later...
  • 1 month later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.