elentz Posted May 28, 2017 Share Posted May 28, 2017 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. Quote Link to comment Share on other sites More sharing options...
requinix Posted May 28, 2017 Share Posted May 28, 2017 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. Quote Link to comment Share on other sites More sharing options...
elentz Posted May 28, 2017 Author Share Posted May 28, 2017 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? Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted May 28, 2017 Share Posted May 28, 2017 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. Quote Link to comment Share on other sites More sharing options...
requinix Posted May 28, 2017 Share Posted May 28, 2017 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"> Quote Link to comment Share on other sites More sharing options...
elentz Posted May 28, 2017 Author Share Posted May 28, 2017 requinixNot 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 Quote Link to comment Share on other sites More sharing options...
elentz Posted May 29, 2017 Author Share Posted May 29, 2017 requinix That bit of code worked beautifully. And I think I halfway understand what is going on too! Thank you so much Quote Link to comment Share on other sites More sharing options...
requinix Posted May 29, 2017 Share Posted May 29, 2017 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 Quote Link to comment Share on other sites More sharing options...
gizmola Posted February 1, 2018 Share Posted February 1, 2018 KathyJohnson, Please learn to use code tags for your posts here. Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted March 14, 2018 Share Posted March 14, 2018 Please be aware that the original post was made nearly a year ago. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.