1

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.

7
  • 2
    You need to "fetch" first. Commented Oct 11, 2016 at 12:23
  • 1
    try posting your full code....not too sure what you are trying to do here. Are you trying to compare the DB values against user input values? Commented Oct 11, 2016 at 12:29
  • Thank you @Fred-ii- , like this: $sth->fetch(PDO::FETCH_ASSOC); ? Commented Oct 11, 2016 at 12:30
  • Dear @Austin I am trying to compare database values and echo only the ones that are different from the older ones. Commented Oct 11, 2016 at 12:31
  • 1
    something to that effect, yes. Commented Oct 11, 2016 at 12:32

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.