Jump to content

Recommended Posts

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;

}

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

 

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.