Jump to content

IF, ELSE, cant get them to work :(


k00ld00dz

Recommended Posts

Hello, I am trying to make this work.  what i am trying to do is to make a function that changes a variable back and forth. for example, I need to have a variable $bgcolorplace that is set to 1, as soon as the function is executed the variable $bgcolorplace changes to 0.  The next time the function executes, variable should turn to 1 again.  This pattern should repeat forever only when the function is executed.  I am trying to use this to change the background color of a generated HTML code which will be on the webpage.  Here is my script.

 

<?php

$input=($_GET['inputText']);      //get info from JS

$ItemNum='Item'.$input;          //make a var with contents "Item" + the info from JS to display later

$bgcolorplace;                          //var that keeps track of what color to make the next background

$bgcolor;                                    //keeps the color of the background for this execution

function changebgcolor(){       

  if ($bgcolorplace=="0"){         

  $bgcolorplace="1";

                  //$bgcolor="444444"

  echo("0 to 1<br/>".$bgcolorplace."<br/>");//display which part of code is executed 1 to 0 or 0 to 1(can never get the script to run this part), this line is temp/test code

  }else{

  $bgcolorplace="0";

                  //$bgcolor="ffffff"

  echo("1 to 0<br/>".$bgcolorplace."<br/>");  //display which part of code is executed 1 to 0 or 0 to 1(It works), this line is temp/test code

  }

}

//this next section I have temporarily commented for testing purposes, this section works fine  :)

/*$out="<tr bgcolor=#'".$bgcolor."' id='".$input."'><td align='center' width='5%'>".$input."</td><td align='center' width='30%'>bible</td><td align='center' width='50%'>Softcover</td><td align='center' width='5%'>45</td><td align='center' width='5%'>1</td><td align='center' width='5%'>45</td></tr>";*/

 

if ($input=='123'){

changebgcolor();

//echo($out);

}else{

echo('none');

}

?>

 

I get a responds of this everytime

 

1 to 0

1

 

this means that the function is being processed, but the value of $bgcolorplace is always a 1, it never changes to 0 like I am trying to inside the IF Else Statement.  The commented Items are just my code that I have temp disabled while I try to figure out why my IF Else statement doesnt work.  Help is much appreciated, and thank you in advanced. :)

 

Link to comment
Share on other sites

I just put your code between tags its easier to read  :) ill have a look at the code also

<?php
$input=($_GET['inputText']);      //get info from JS
$ItemNum='Item'.$input;           //make a var with contents "Item" + the info from JS to display later
$bgcolorplace;                           //var that keeps track of what color to make the next background
$bgcolor;                                    //keeps the color of the background for this execution
function changebgcolor(){         
  if ($bgcolorplace=="0"){           
        $bgcolorplace="1";
                  //$bgcolor="444444"
        echo("0 to 1<br/>".$bgcolorplace."<br/>");//display which part of code is executed 1 to 0 or 0 to 1(can never get the script to run this part), this line is temp/test code
     }else{
        $bgcolorplace="0";
                  //$bgcolor="ffffff"
        echo("1 to 0<br/>".$bgcolorplace."<br/>");   //display which part of code is executed 1 to 0 or 0 to 1(It works), this line is temp/test code
     }
}
//this next section I have temporarily commented for testing purposes, this section works fine  
/*$out="<tr bgcolor=#'".$bgcolor."' id='".$input."'><td align='center' width='5%'>".$input."</td><td align='center' width='30%'>bible</td><td align='center' width='50%'>Softcover</td><td align='center' width='5%'>45</td><td align='center' width='5%'>1</td><td align='center' width='5%'>45</td></tr>";*/

if ($input=='123'){
      changebgcolor();
      //echo($out);
}else{
   echo('none');
}
?>

Link to comment
Share on other sites

echo is actually a function, so quotes are permitted, just not required

Yep, just like print & exit and a few other's that I can't recall; but to be honest majority of people just do: echo "Foo"; these days.

 

Cheers,

Rw

Link to comment
Share on other sites

change varijable $bgcolorplace to static

try[/code]<?php

function changebgcolor(){

    static $bgcolorplace = 0;

    if ($bgcolorplace=="0"){

        $bgcolorplace="1";

                  //$bgcolor="444444"

        echo("0 to 1<br/>".$bgcolorplace."<br/>");//display which part of code is executed 1 to 0 or 0 to 1(can never get the script to run this part), this line is temp/test code

    }else{

        $bgcolorplace="0";

                  //$bgcolor="ffffff"

        echo("1 to 0<br/>".$bgcolorplace."<br/>");  //display which part of code is executed 1 to 0 or 0 to 1(It works), this line is temp/test code

    }

}

 

for($i=0; $i<10; $i++){

    changebgcolor();

    echo "<hr />\n";

}

?>[/code]

Link to comment
Share on other sites

Ok I found my problem.  Thank you all who replied so quickly, I really appreciate your help.  The reply about variable scopes helped a lot, but that was not my only problem with the code.  This PHP file was executed from a JavaScript code using AJAX, so the PHP file opened, did what it was suppose to, and closed.  I did not realize that every time my JS called for the PHP file, the PHP file started without the variables that were stored from the previous time it was called.  To combat this problem, I had to create an external file using the PHP file to store the $bgcolorplace variable.  Also I had to write PHP so that the file that was automatically generated the previous execution would be loaded into the $bgcolorplace variable.  I also had to change the order around a little bit. Here is my new PHP script:

<?php
function readtrace(){
$trace = ("colortrace.txt");
if (!file_exists($trace)) {
	echo "<br>count_my_page not found, New trace file being created.<br>";
    	fopen($trace, 'w') or die("<br>cannot create trace file, check permissions.  If you do not have permissions, contact your system administrator<br>");}
$value = file($trace);
global $bgcolorplace;
$bgcolorplace=$value[0];
}
function storetrace($place){
$trace = ("colortrace.txt");
if (!file_exists($trace)) {
	echo "<br>count_my_page not found, New trace file being created.<br>";
    	fopen($trace, 'w') or die("<br>cannot create trace file, check permissions.  If you do not have permissions, contact your system administrator<br>");}
$value = file($trace);
$value[0]=$place;
$fp = fopen($trace , "w");
fputs($fp , "$value[0]");
fclose($fp);
}

$input=($_GET['inputText']);
$ItemNum='Item'.$input;
$Title='Bible';
$Description='softcover'; 
$PriceEach='45';
function changebgcolor(){
global $bgcolor;
global $bgcolorplace;
  if ($bgcolorplace=="1"){
	  $bgcolorplace--;
	  $bgcolor="00ee00";
	  storetrace($bgcolorplace);
  }elseif($bgcolorplace=="0"){
	  $bgcolorplace++;
	  $bgcolor="aaffaaa";
	  storetrace($bgcolorplace);
  }else {echo ("<br/>bgcolorplace undefined, check global variable in Data.php<br/>");}
  
}
if ($input=='123'){
	readtrace();
	$bgcolor;
	changebgcolor();
$out=/*$ItemNum.*/"<tr bgcolor='#".$bgcolor."' id='".$ItemNum."'><?--jsbreak--><td align='center' width='5%'>".$input."</td><td align='center' width='30%'>".$Title."</td><td align='center' width='50%'>".$Description."</td><td align='center' width='5%'>".$PriceEach."</td><td align='center' width='5%'>1</td><td align='center' width='5%'>45</td></tr>";
	echo($out);
}else{ 
echo('none');
}

?>

 

Link to comment
Share on other sites

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.