k00ld00dz Posted September 9, 2010 Share Posted September 9, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/213006-if-else-cant-get-them-to-work/ Share on other sites More sharing options...
fortnox007 Posted September 9, 2010 Share Posted September 9, 2010 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'); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/213006-if-else-cant-get-them-to-work/#findComment-1109366 Share on other sites More sharing options...
MatthewJ Posted September 9, 2010 Share Posted September 9, 2010 You have to pass values in to functions... You may want to read up on variable scope http://php.net/manual/en/language.variables.scope.php function changebgcolor($bgcolorplace) { ... } There is most definitely a much better way to do this, but this should help get you on track Quote Link to comment https://forums.phpfreaks.com/topic/213006-if-else-cant-get-them-to-work/#findComment-1109367 Share on other sites More sharing options...
schilly Posted September 9, 2010 Share Posted September 9, 2010 All functions have a separate variable scope. You need to either pass them into your function or declare them as globals inside your function. I would recommend passing them into your function like Matthew's example above. Quote Link to comment https://forums.phpfreaks.com/topic/213006-if-else-cant-get-them-to-work/#findComment-1109368 Share on other sites More sharing options...
fortnox007 Posted September 9, 2010 Share Posted September 9, 2010 also you echo's are using ( ) Quote Link to comment https://forums.phpfreaks.com/topic/213006-if-else-cant-get-them-to-work/#findComment-1109369 Share on other sites More sharing options...
MatthewJ Posted September 9, 2010 Share Posted September 9, 2010 also you echo's are using ( ) echo is actually a function, so quotes are permitted, just not required Quote Link to comment https://forums.phpfreaks.com/topic/213006-if-else-cant-get-them-to-work/#findComment-1109370 Share on other sites More sharing options...
fortnox007 Posted September 9, 2010 Share Posted September 9, 2010 also you echo's are using ( ) echo is actually a function, so quotes are permitted, just not required My bad : ) Quote Link to comment https://forums.phpfreaks.com/topic/213006-if-else-cant-get-them-to-work/#findComment-1109371 Share on other sites More sharing options...
rwwd Posted September 9, 2010 Share Posted September 9, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/213006-if-else-cant-get-them-to-work/#findComment-1109373 Share on other sites More sharing options...
sasa Posted September 10, 2010 Share Posted September 10, 2010 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] Quote Link to comment https://forums.phpfreaks.com/topic/213006-if-else-cant-get-them-to-work/#findComment-1109454 Share on other sites More sharing options...
k00ld00dz Posted September 10, 2010 Author Share Posted September 10, 2010 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'); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/213006-if-else-cant-get-them-to-work/#findComment-1109478 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.