joshgarrod Posted June 10, 2009 Share Posted June 10, 2009 I have a loop that prints a list of information, I want it to do something different if the value of a certain variable changes? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/161679-solved-find-out-when-a-variables-value-has-changed/ Share on other sites More sharing options...
Maq Posted June 10, 2009 Share Posted June 10, 2009 Temporarily store it, if it doesn't equal the temp var, then do something. Quote Link to comment https://forums.phpfreaks.com/topic/161679-solved-find-out-when-a-variables-value-has-changed/#findComment-853095 Share on other sites More sharing options...
joshgarrod Posted June 10, 2009 Author Share Posted June 10, 2009 can you show me an example please, I am not sure how to structure it. Thank you for your time Quote Link to comment https://forums.phpfreaks.com/topic/161679-solved-find-out-when-a-variables-value-has-changed/#findComment-853096 Share on other sites More sharing options...
byte1918 Posted June 10, 2009 Share Posted June 10, 2009 can you show me an example please, I am not sure how to structure it. Thank you for your time  $temp_variable = $variable #code goes here... if ($variable != $temp_variable){    #variable has changed;} else{   #variable is the same; } Quote Link to comment https://forums.phpfreaks.com/topic/161679-solved-find-out-when-a-variables-value-has-changed/#findComment-853098 Share on other sites More sharing options...
joshgarrod Posted June 10, 2009 Author Share Posted June 10, 2009 But with a loop it won't work as it resets the variable back every time around, or is there a way to prevent this? Quote Link to comment https://forums.phpfreaks.com/topic/161679-solved-find-out-when-a-variables-value-has-changed/#findComment-853317 Share on other sites More sharing options...
taquitosensei Posted June 10, 2009 Share Posted June 10, 2009 something along these lines at the end of your loop set $oldvariable $variable=3; for($a=0;$a<=20;$a++) { $variable=$a; if($variable!=$oldvariable) { // do whatever here } $oldvariable=$variable; } Quote Link to comment https://forums.phpfreaks.com/topic/161679-solved-find-out-when-a-variables-value-has-changed/#findComment-853319 Share on other sites More sharing options...
joshgarrod Posted June 10, 2009 Author Share Posted June 10, 2009 but when you compare $variable and $oldvariable, you haven't actually set $oldvariable, sorry this ois really confusing me... Quote Link to comment https://forums.phpfreaks.com/topic/161679-solved-find-out-when-a-variables-value-has-changed/#findComment-853321 Share on other sites More sharing options...
PFMaBiSmAd Posted June 10, 2009 Share Posted June 10, 2009 If you show the loop you are currently using, someone can directly show you the implementation that would be used with that loop. Â But in general, you initialize the $old variable before the loop to a value it will never see as data, such as an empty string ''. Then you test if the value is not equal to the $old variable. If the values are not equal, you do the sepcial processing AND you set the $old variable equal to the variable you at testing so that you will detect the next change in the value. Quote Link to comment https://forums.phpfreaks.com/topic/161679-solved-find-out-when-a-variables-value-has-changed/#findComment-853325 Share on other sites More sharing options...
joshgarrod Posted June 10, 2009 Author Share Posted June 10, 2009 Ok here is my code, thanks: Â <?php mysql_connect("host", "user", "pass") or die(mysql_error()); mysql_select_db("quest") or die(mysql_error()); $query = ("SELECT * FROM directory ORDER BY category ASC"); $result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error()); while($row = mysql_fetch_object($result)) { $ref = $row->ref; $category = $row->category; $count = 120; $company = $row->company; $descr = $row->descr; $descrshort = substr($descr,0,$count); //limiting the amount of characters set by var count $web = $row->web; $tel = $row->tel; $road = $row->road; $town = $row->town; $county = $row->county; $temp_category = $category; if ($category != $temp_category){ Â Â Â #variable has changed;} echo "<h1>$category</h1>"; } else { Â Â #variable is the same; echo "<div id=\"show_row\"><h1>$company</h1><strong>Location: </strong>$town, $county<br /><br />$descrshort ...<a href=\"view_company_details.php?ref=$ref\">More>></a></div>"; $temp_category = $category; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/161679-solved-find-out-when-a-variables-value-has-changed/#findComment-853327 Share on other sites More sharing options...
PFMaBiSmAd Posted June 10, 2009 Share Posted June 10, 2009 you initialize the $old variable before the loop to a value it will never see as data, such as an empty string ''. Then you test if the value is not equal to the $old variable. If the values are not equal, you do the sepcial processing AND you set the $old variable equal to the variable you at testing so that you will detect the next change in the value. Â $temp_category = ''; while($row = mysql_fetch_object($result)) { .... if ($category != $temp_category){ Â Â Â #variable has changed;} Â Â echo "<h1>$category</h1>"; Â Â $temp_category = $category; Â Â } else { Â Â #variable is the same; Â Â echo "<div id=\"show_row\"><h1>$company</h1><strong>Location: </strong>$town, $county<br /><br />$descrshort ...<a href=\"view_company_details.php?ref=$ref\">More>></a></div>"; } } Â Quote Link to comment https://forums.phpfreaks.com/topic/161679-solved-find-out-when-a-variables-value-has-changed/#findComment-853333 Share on other sites More sharing options...
joshgarrod Posted June 10, 2009 Author Share Posted June 10, 2009 How do I get the information to set the variable $temp_category before the loop? Quote Link to comment https://forums.phpfreaks.com/topic/161679-solved-find-out-when-a-variables-value-has-changed/#findComment-853335 Share on other sites More sharing options...
joshgarrod Posted June 10, 2009 Author Share Posted June 10, 2009 Ok, sorry I get it. Set the variable before the loop with no value. I will try it. Quote Link to comment https://forums.phpfreaks.com/topic/161679-solved-find-out-when-a-variables-value-has-changed/#findComment-853339 Share on other sites More sharing options...
joshgarrod Posted June 10, 2009 Author Share Posted June 10, 2009 Worked, thanks for your help guys Quote Link to comment https://forums.phpfreaks.com/topic/161679-solved-find-out-when-a-variables-value-has-changed/#findComment-853346 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.