Detect Change in variable

Hello,
I will be echoing a list from a database. Say there are two fields ($make and $model). I will call the whole list from the database and sort by $make. Under each make there will be many models.

Everytime the make changes I want to insert a new headline showing the following models are a different make. How would I detect a change in $make?

Here is a rough idea of what I am trying to do:


$sql = "SELECT * FROM `inventory` ORDER BY `make`";
                            $result = mysql_query($sql) or die(mysql_error());
                            if(mysql_num_rows($result) == 0){
                                //No Specials
                                echo "We do not currently have any inventory.";
                            } else {
                                while($Inventory = mysql_fetch_array($result, MYSQL_ASSOC)){
                                    echo"<strong> {$Inventory['make']}</strong><br>{$Inventory['model']}<br>";
                                    
                                }//End while
                            }//End else

This script would output something like:
“Dodge Calibur
Dodge Ram 1500
Dodge Viper
Ford Escort
Ford Explorer
Ford F-150”

What I want the script to do is output this:
"Dodge -
Calibur
Ram 1500
Viper

Ford -
Escort
Explorer
F-150"

Basically it would detect that “make” has changed from Dodge to Ford and echo the make once followed by the models of that make.

Obviously if I knew what the makes would be I could just use if statements. But I do not know what they will have in inventory so I need the script to detect it for me.

Any ideas? Thanks!!

Awesome! That works perfectly.

Thank you!

Try something like this:


$sql = "SELECT * FROM `inventory` ORDER BY `make`";
$result = mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($result) >= 1){
    $make = '';
    while($Inventory = mysql_fetch_array($result, MYSQL_ASSOC)){
        if($make != $Inventory['make']){
            $make = $Inventory['make'];
            echo '<strong> - ' . $Inventory['make'] . '</strong><br />';
        }
        echo ' - - ' . $Inventory['model'] . '<br />';
    }
}
else{
    echo "We do not currently have any inventory.";
}

I haven’t tested it but should work.