So, I am creating a history of changes page from database that has one row for each update. I need to represent only values that have changed. I represent those values in foreach loop (to make it more simple I reduced it to 1 value: work stations):
$sth = $pdo->prepare("
SELECT * FROM user_customers_history
");
$sth->execute();
foreach ($sth as $key => $row) {
$crm_workstations = $row['crm_workstations'];
if (($crm_workstations > 0) && ($crm_workstations != $crm_workstations)){
echo '<br/><br/>' . $crm_workstations;
}
I defined $key as an index, but I don't know how to use it. I know that I want to represent it this way: for each index check if the value is equal to previous index value. If they are not equal, show it.
Define a previous value for your variable to compare with the current one:
$sth = $pdo->prepare("
SELECT * FROM user_customers_history
");
$sth->execute();
$prev_workstations = '';
while ($row = $sth->fetch()) {
$crm_workstations = $row['crm_workstations'];
if (($crm_workstations > 0) && ($crm_workstations != $prev_workstations)){
$prev_workstations = $crm_workstations;
echo '<br/><br/>' . $crm_workstations;
}
}
They ask me to edit more then 6 characters I needed to write somthing :D