Check that all items are equal

Asked

Viewed 75 times

2

I am recovering from the database all expired tickets, with the following query

SELECT user,status,hash 
      FROM ticket 
      WHERE datepay < date_add(now(), interval -1 month) AND user = '119'

The result is as expected, as below. It reads status:usuário

0:118 0:119 0:119

However I need to check if all status is 1 (means paid), if there is at least one user with 0:119 already invalid. I tried using array_unique, but it will check all users and I will not know which one did not pay all.

  • I don’t get it. This way 0:118 0:119 0:119 does not correspond to SELECT user,status,hash... Could clarify?

  • This output is to explain better the structure, in PHP I loop (foreach) and structured status:user just to explain better. Basically if any user has 0:user is already invalid, it is only valid if all are 1:user.

1 answer

1


What you need is to deal with for, the logic is simple

Se user.status == 0 então diga "usuário pendente"; 
  parar;
senão
  "o usuário está em dias";

In practice it would look something like this, considering that the variable $ftc is the object of the query (fetchAll):

for($i = 0; $i <= count($ftc); $i++){
  if($ftc[$i]["status"] == "0"){
    echo "O usuário {$ftc[$i]["user"]} ainda tem fatura pendente".PHP_EOL;
    break;
  }else{
    echo "O usuário {$ftc[$i]["user"]} pode ser liberado".PHP_EOL;
  }
}

Since you said that if at least one ticket is pending is no longer valid, then you don’t need to check all the elements, just stop the X user iteration and continue to the next one recursively.

Cons

If there are too many pending users, the application will be slow, taking a high cost in server performance.

  • Perfect, that’s right :D, I couldn’t come up with a logic, but this way is simple and easy, I thought that break would stop both users, in case one was defaulted. Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.