creating a function to print some text, hard to describe problem


#1

I started with this and was repeating it over and over with $bit#

[php]
$bit0=1

]if ($bit0==1){
print("<input type=submit name=GPIO value=‘GPIO0’ style=‘background-color:green’\n");
}else{
print("<input type=submit name=GPIO value=‘GPIO0’ style=‘background-color:red’\n");
}

[/php]

so I tried this

[php]
function printButton($GPIO){
if ($bit.$GPIO==1){
print("<input type=submit name=GPIO value=‘GPIO".$GPIO."’ style=‘background-color:green’\n");
}else{
print("<input type=submit name=GPIO value=‘GPIO".$GPIO."’ style=‘background-color:red’\n");
}
}

printButton(0)

[/php]

the print part works its the

[php]if ($bit.$GPIO==1){}[/php]

which is causing me problems


#2

Well, are you trying to create a list of inputs name=gpi01, name=gpi02, name=gpi0etc…

Or are you trying to alternate output lines of two colors? There are several ways of doing each.


#3

Here is my all of my code

I have variables $bit0, $bit1

I want to print a form which has buttons which are coloured relating to the value of $bitx

If $bitx=1 green
If $bitx=0 red

I can print each button seperatly a s in my first example but if I have a lot of variables then this is messy and time consuming

[php]

<?php $bit0=1; $bit1=0; $bit2=0; $bit3=0; $bit4=0; function GPIO0() { print("THIS IS FUNCTION GPIO0"); } function GPIO1() { print("THIS IS FUNCTION GPIO1"); } function postFunction() { $postF = ($_POST["GPIO"]); return $postF(); } if (isset($_POST["GPIO"])){ postFunction(); } function printButton($GPIO){ $GPIOX="\$bit".$GPIO; print $GPIOX; if ($GPIOX==1){ print("\n"); }else{ print("\n"); } } print("\n"); print("\n"); printButton(0); printButton(1); print("\n"); print("\n"); ?>

[/php]

This is the code i am trying to shorten

[php]

<?php $bit0=1; $bit1=0; $bit2=0; $bit3=0; $bit4=0; function GPIO0() { print("THIS IS FUNCTION GPIO0 WHICH WILL DO SOMETHING MORE INTERESTING IN THE FUTURE"); } function GPIO1() { print("THIS IS FUNCTION GPIO1"); } function postFunction() { $postF = ($_POST["GPIO"]); return $postF(); } if (isset($_POST["GPIO"])){ postFunction(); } print("\n"); print("\n"); if ($GPIO0==1){ print("\n"); }else{ print("\n"); } if ($GPIO1==1){ print("\n"); }else{ print("\n"); } print("\n"); print("\n"); ?>

[/php]

In reality I have upto $bit14 but its un-necessary to paste them all as it jsut makes for a huge piece of code


#4

sorry, to answer your question more accurately, im trying to create a list of inputs name=GPIO value=GPIO1,2,3,4… whos button colour is related to the corresponding variable $bit0,1,2,3,4


#5

This does exactly what I want but the buttons don’t change colour.

[php]<?php
$bit0=1;
$bit1=0;
$bit2=0;
$bit3=0;
$bit4=0;
$bit5=0;
$bit6=0;
$bit7=0;
$bit8=0;
$bit9=0;

function GPIO0() {
print(“THIS IS FUNCTION GPIO0”);
}

function GPIO1() {
print(“THIS IS FUNCTION GPIO1”);
}

function GPIO2() {
print(“THIS IS FUNCTION GPIO2”);
}

function GPIO3() {
print(“THIS IS FUNCTION GPIO3”);
}

function GPIO4() {
print(“THIS IS FUNCTION GPIO4”);
}

function GPIO5() {
print(“THIS IS FUNCTION GPIO5”);
}

function GPIO6() {
print(“THIS IS FUNCTION GPIO6”);
}

function GPIO7() {
print(“THIS IS FUNCTION GPIO7”);
}

function GPIO8() {
print(“THIS IS FUNCTION GPIO8”);
}

function GPIO9() {
print(“THIS IS FUNCTION GPIO9”);
}

function postFunction() {
$postF = ($_POST[“GPIO”]);
return $postF();
}

if (isset($_POST[“GPIO”])){
postFunction();
}

function printButton($GPIO){
$GPIOX="$bit".$GPIO;
if ($GPIOX==1){
print("\n");
}else{
print("\n");
}
}

print("<html><body>\n");
print("<form action=index.php method=POST>\n");
printButton(0);
printButton(1);
printButton(2);
printButton(3);
printButton(4);
printButton(5);
printButton(6);
printButton(7);
printButton(8);
printButton(9);
print("</form>\n");
print("</body></html>\n"); 

?>[/php]


#6

Thanks, that helps. Well, there are many many ways to do this. The easiest I can think of quickly is to use a simple array. It would be so much easier and more options would be available… Something like this:
(BASED on 14 options as you mentioned…)

[php]

<?PHP //Create bits array with 14 values... $bits[] = new array(); for ($i = 1; $i <= 14; $i++) { $bits[$i]=0; } //Some code that sets a value, for testing, let's use #3... $bits['3']=1; //Set up routine to do some code depending on which bit is set... if ($bits['1']==1){ // Do something if bits#1 is on... }elseif($bits['2']==1){ // Do something if bits#2 is on... }elseif($bits['3']==1){ // Do something if bits#3 is on... This one would hit and run with our example... } ?>

[/php]
Using this way, you could also use the array to display your buttons, or whatever… Something like this:
[php]
// Set up the colors…
$colors[5] = array(“green”, “red”, “blue”, “yellow”, “orange”);
for ($i = 1; $i <= 5; $i++) {
print “\n”);
}
[/php]
Just an idea… Hope it helps…


#7

I’ve moved my bit register to an array and come up with this

[php]<?php

$bit = array();
for ($i = 0; $i <= 9; $i++) {
$bit[$i]=0;
}

$bit[‘0’]=1;
$bit[‘1’]=1;
$bit[‘2’]=0;
$bit[‘3’]=1;
$bit[‘4’]=0;
$bit[‘5’]=1;
$bit[‘6’]=1;
$bit[‘7’]=0;
$bit[‘8’]=1;
$bit[‘9’]=1;

function GPIO0() {
print(“THIS IS FUNCTION GPIO0 AND WILL EXECUTE SOME COOL SERVERSIDE CODE EVENTUALLY”);
}

function GPIO1() {
print(“THIS IS FUNCTION GPIO1”);
}

function GPIO2() {
print(“THIS IS FUNCTION GPIO2”);
}

function GPIO3() {
print(“THIS IS FUNCTION GPIO3”);
}

function GPIO4() {
print(“THIS IS FUNCTION GPIO4”);
}

function GPIO5() {
print(“THIS IS FUNCTION GPIO5”);
}

function GPIO6() {
print(“THIS IS FUNCTION GPIO6”);
}

function GPIO7() {
print(“THIS IS FUNCTION GPIO7”);
}

function GPIO8() {
print(“THIS IS FUNCTION GPIO8”);
}

function GPIO9() {
print(“THIS IS FUNCTION GPIO9”);
}

function postFunction() {
$postF = ($_POST[“GPIO”]);
return $postF();
}

if (isset($_POST[“GPIO”])){
postFunction();
}

function printButton($GPIO){
if ($bit[$GPIO]==1){
print("
\n");
}else{
print("
\n");
}
}

print("<html><body>\n");
print("<form action=index.php method=POST>\n");
printButton(0);
printButton(1);
printButton(2);
printButton(3);
printButton(4);
printButton(5);
printButton(6);
printButton(7);
printButton(8);
printButton(9);
print("</form>\n");
print("</body></html>\n"); 

?>[/php]

but this part doesn’t work

[php]
function printButton($GPIO){
if ($bit[$GPIO]==1){
print("
\n");
}else{
print("
\n");
}
[/php]

yet if I move it out of the function it works fine

[php]
$GPIO=1;
if ($bit[$GPIO]==1){
print("
\n");
}else{
print("
\n");
}

I feel so close to getting it to do what I want…
[/php]


#8

I think you are close, too! BUT, I think your functions are a bit messed up…

First, you have a function to print a button. Then, you loop thru it 9 times. Also, all of those functions for the new bit array are not needed. If the forms have several inputs, you need to check for them. So, you can not use the general “if(isset($_POST[“GPIO”])){” as the field “GPIO” does not exist. To handle the posted variable, you would have to scan thru the $_POST array to find out which GPIO’s are active and then handle them. That can be done with a simple FOR clause.

So, for this section:
function printButton($GPIO){
if ($bit[$GPIO]==1){
print("
\n");
}else{
print("
\n");
}
}
print("\n");
print("\n");
printButton(0);
printButton(1);
printButton(2);
printButton(3);
printButton(4);
printButton(5);
printButton(6);
printButton(7);
printButton(8);
printButton(9);
print("\n");
print("\n");
?>
I would use something like this, which I think is what you want and much better looking…
[php]
function printButton(){
for ($i = 0; $i <= 9; $i++) {
print “
\n”;
}else{
print “red’>\n”;
}
}
print("\n");
print("\n");
printButton(0);
print("\n");
print("\n");
?>
[/php]
Or, something like that! By the way, are you placing the “printButton” into a function because you are going to use it a lot? Or, just because someone told you you needed it that way? I was just curious why you have all those functions. Planning to put them into a reusable include file? Just nosey!

So, now the reading section… The field’s are posted to your code from the form. All of the fields with inputs placed into them are inside the $_POST array. The easiest way is to just put them into just IF clause’s…
Something like:
if ($_POST[‘GPIO0’]==1){
// Do something cool with #0
}
Like you already had… You would have to make one for each button. If the routines inside of these are similar or the same, then you can combine them. If that is the case, then, ask and we can show you how that would be done…

Good luck.   Help this helped!

#9

thanks; i woke up this morning and wrote this

[php]
for ($i=0; $i<=9; $i++){
if ($bit[$i]==1){
print("
\n");
}else{
print("
\n");
}
}
[/php]

Then read your post and its pretty much identical thanks for your help; the more functions I learn the easier it gets :smiley:

Marked Solved


#10

Great! Always nice to get a puzzle fixed up…

CYA in the bitstream…