Page 1 of 1

How to detect variable change

Posted: Sat Feb 07, 2009 8:47 pm
by markjohnson
Is there a way I can detect change in a variable value?

This is what I want to achieve:

I have a normal select query that lists records with their group_ids... like so...

group_id --- number
333
333
333
333
345
345
345
345

The number field at the moment is null, and I would like to update that field in each of these records, starting with 1, then 2 and so on... And as soon as group_id changes, from 333 to 345, I would the numbering to restart.

At the moment I have this:

Code: Select all

<?php
$x=1;
        do {
?>
    <tr>
      <td><?php echo $row_rsOptionGroups['id']; ?></td>
      <td><?php echo $row_rsOptionGroups['group_id']; ?></td>
      <td><?php echo $row_rsOptionGroups['display_value']; ?></td>
      <td><?
        $g_id=$row_rsOptionGroups['group_id'];
        $y=$g_id;
        if ($y != $row_rsOptionGroups['group_id']) {
            $x=1;
            $g_id=$row_rsOptionGroups['group_id'];
        }
      echo $x;
      $x=$x+1;
      ?></td>
      <td><?php echo $row_rsOptionGroups['option_price']; ?></td>
    </tr>
    <?php 
    } while ($row_rsOptionGroups = mysql_fetch_assoc($rsOptionGroups)); 
 ?>
 
And it obviously doesn't work.

Is there a way I can do:

Code: Select all

If ($row_rsOptionGroups['group_id'] changes from what it previously was) {
   $x=1;
}
Many thanks!

Re: How to detect variable change

Posted: Sat Feb 07, 2009 11:24 pm
by Skoalbasher
markjohnson wrote:Is there a way I can detect change in a variable value?

The number field at the moment is null, and I would like to update that field in each of these records, starting with 1, then 2 and so on... And as soon as group_id changes, from 333 to 345, I would the numbering to restart.

At the moment I have this:

Code: Select all

<?php
$x=1;
        do {
?>
    <tr>
      <td><?php echo $row_rsOptionGroups['id']; ?></td>
      <td><?php echo $row_rsOptionGroups['group_id']; ?></td>
      <td><?php echo $row_rsOptionGroups['display_value']; ?></td>
      <td><?
        $g_id=$row_rsOptionGroups['group_id'];
        $y=$g_id;
        if ($y != $row_rsOptionGroups['group_id']) {
            $x=1;
            $g_id=$row_rsOptionGroups['group_id'];
        }
      echo $x;
      $x=$x+1;
      ?></td>
      <td><?php echo $row_rsOptionGroups['option_price']; ?></td>
    </tr>
    <?php 
    } while ($row_rsOptionGroups = mysql_fetch_assoc($rsOptionGroups)); 
 ?>
 
And it obviously doesn't work.

Is there a way I can do:

Code: Select all

If ($row_rsOptionGroups['group_id'] changes from what it previously was) {
   $x=1;
}
Many thanks!
I'm doing this right before I go to bed, so excuse any errors. This may work for you.

Code: Select all

 
$query = "SELECT * FROM blah blah blah";
$result = mysql_query($query) or die(mysql_error());
 
$tempval = "";
while($row = mysql_fetch_array($result))
{
   if($row['group_id'] == $tempval)  // this won't run the first time around since $tempval = ""
   {
      $id = $row['id'];
      
      $q = "UPDATE tablename SET number = '$i' WHERE id='$id'";
      $r = mysql_query($q) or die(mysql_error());
      $tempval = $row['group_id'];
      $i++;
   } else {  // this will run the first time and anytime $row['group_id'] changes.
      $id = $row['id'];
      $i = 1;
      $q = "UPDATE tablename SET number = '$i' WHERE id='$id'";
      $r = mysql_query($q) or die(mysql_error());
      $tempval = $row['group_id'];
      $i++;
    }
}
 
I think this should do what you want

Re: How to detect variable change

Posted: Sun Feb 08, 2009 9:08 am
by markjohnson
Thank you very much!

It worked!