Jump to content

[SOLVED] Find out when a variable's value has changed


joshgarrod

Recommended Posts

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.

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;
}

}

?>

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>";
}
}

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.