Anakin Vader Anakin Vader - 20 days ago 6
PHP Question

PHP function to check for a id

I have a sql query that look like this :

$sql = "SELECT weight, added_date, etid
FROM balance_events
WHERE added_date BETWEEN '".$from_date."'
AND '".$to_date."'
ORDER BY `balance_events`.`added_date` ASC";


After some operations I got a object that looks like this:

array(34) {
[0]=>
object(stdClass)#55 (3) {
["weight"]=>
string(4) "5520"
["added_date"]=>
string(19) "2016-08-01 12:51:22"
["etid"]=>
string(1) "3"
}
[1]=>
object(stdClass)#56 (3) {
["weight"]=>
string(5) "16860"
["added_date"]=>
string(19) "2016-08-01 12:51:24"
["etid"]=>
string(1) "3"
}
[2]=>
object(stdClass)#57 (3) {
["weight"]=>
string(5) "17040"
["added_date"]=>
string(19) "2016-08-01 12:51:25"
["etid"]=>
string(1) "3"
}
[3]=>
object(stdClass)#58 (3) {
["weight"]=>
string(5) "16460"
["added_date"]=>
string(19) "2016-08-01 12:51:26"
["etid"]=>
string(1) "3"
}
[4]=>
object(stdClass)#59 (3) {
["weight"]=>
string(5) "16280"
["added_date"]=>
string(19) "2016-08-01 12:51:27"
["etid"]=>
string(1) "3"
}
[5]=>
object(stdClass)#60 (3) {
["weight"]=>
string(5) "40200"
["added_date"]=>
string(19) "2016-08-01 12:51:29"
["etid"]=>
string(1) "3"
}
[6]=>
object(stdClass)#61 (3) {
["weight"]=>
string(5) "42120"
["added_date"]=>
string(19) "2016-08-01 12:51:31"
["etid"]=>
string(1) "3"
}
[7]=>
object(stdClass)#62 (3) {
["weight"]=>
string(5) "42060"
["added_date"]=>
string(19) "2016-08-01 12:51:33"
["etid"]=>
string(1) "3"
}
[8]=>
object(stdClass)#63 (3) {
["weight"]=>
string(5) "42040"
["added_date"]=>
string(19) "2016-08-01 12:51:48"
["etid"]=>
string(1) "3"
}
[9]=>
object(stdClass)#64 (3) {
["weight"]=>
string(5) "42060"
["added_date"]=>
string(19) "2016-08-01 12:51:49"
["etid"]=>
string(1) "3"
}
[10]=>
object(stdClass)#65 (3) {
["weight"]=>
string(5) "42040"
["added_date"]=>
string(19) "2016-08-01 12:51:50"
["etid"]=>
string(1) "3"
}
[11]=>
object(stdClass)#66 (3) {
["weight"]=>
string(5) "42060"
["added_date"]=>
string(19) "2016-08-01 12:52:23"
["etid"]=>
string(1) "3"
}
[12]=>
object(stdClass)#67 (3) {
["weight"]=>
string(5) "42040"
["added_date"]=>
string(19) "2016-08-01 12:52:24"
["etid"]=>
string(1) "3"
}
[13]=>
object(stdClass)#68 (3) {
["weight"]=>
string(5) "42100"
["added_date"]=>
string(19) "2016-08-01 12:52:26"
["etid"]=>
string(1) "3"
}
[14]=>
object(stdClass)#69 (3) {
["weight"]=>
string(5) "42040"
["added_date"]=>
string(19) "2016-08-01 12:52:27"
["etid"]=>
string(1) "3"
}
[15]=>
object(stdClass)#70 (3) {
["weight"]=>
string(5) "42060"
["added_date"]=>
string(19) "2016-08-01 12:52:45"
["etid"]=>
string(1) "3"
}
[16]=>
object(stdClass)#71 (3) {
["weight"]=>
string(5) "42040"
["added_date"]=>
string(19) "2016-08-01 12:52:49"
["etid"]=>
string(1) "3"
}
[17]=>
object(stdClass)#72 (3) {
["weight"]=>
string(5) "42080"
["added_date"]=>
string(19) "2016-08-01 12:52:50"
["etid"]=>
string(1) "3"
}
[18]=>
object(stdClass)#73 (3) {
["weight"]=>
string(5) "42040"
["added_date"]=>
string(19) "2016-08-01 12:52:51"
["etid"]=>
string(1) "3"
}
[19]=>
object(stdClass)#74 (3) {
["weight"]=>
string(5) "42060"
["added_date"]=>
string(19) "2016-08-01 12:54:14"
["etid"]=>
string(1) "3"
}
[20]=>
object(stdClass)#75 (3) {
["weight"]=>
string(5) "42040"
["added_date"]=>
string(19) "2016-08-01 12:57:05"
["etid"]=>
string(1) "3"
}
[21]=>
object(stdClass)#76 (3) {
["weight"]=>
string(5) "42060"
["added_date"]=>
string(19) "2016-08-01 12:57:47"
["etid"]=>
string(1) "3"
}
[22]=>
object(stdClass)#77 (3) {
["weight"]=>
string(5) "42060"
["added_date"]=>
string(19) "2016-08-01 13:02:19"
["etid"]=>
string(1) "4"
}
[23]=>
object(stdClass)#78 (3) {
["weight"]=>
string(5) "42120"
["added_date"]=>
string(19) "2016-08-01 13:02:48"
["etid"]=>
string(1) "3"
}
[24]=>
object(stdClass)#79 (3) {
["weight"]=>
string(5) "42140"
["added_date"]=>
string(19) "2016-08-01 13:02:51"
["etid"]=>
string(1) "3"
}
[25]=>
object(stdClass)#80 (3) {
["weight"]=>
string(5) "42160"
["added_date"]=>
string(19) "2016-08-01 13:02:55"
["etid"]=>
string(1) "3"
}
[26]=>
object(stdClass)#81 (3) {
["weight"]=>
string(5) "42140"
["added_date"]=>
string(19) "2016-08-01 13:02:58"
["etid"]=>
string(1) "3"
}
[27]=>
object(stdClass)#82 (3) {
["weight"]=>
string(5) "42240"
["added_date"]=>
string(19) "2016-08-01 13:03:00"
["etid"]=>
string(1) "3"
}
[28]=>
object(stdClass)#83 (3) {
["weight"]=>
string(5) "41800"
["added_date"]=>
string(19) "2016-08-01 13:03:02"
["etid"]=>
string(1) "3"
}
[29]=>
object(stdClass)#84 (3) {
["weight"]=>
string(5) "30340"
["added_date"]=>
string(19) "2016-08-01 13:03:04"
["etid"]=>
string(1) "3"
}
[30]=>
object(stdClass)#85 (3) {
["weight"]=>
string(5) "25780"
["added_date"]=>
string(19) "2016-08-01 13:03:05"
["etid"]=>
string(1) "3"
}
[31]=>
object(stdClass)#86 (3) {
["weight"]=>
string(5) "24980"
["added_date"]=>
string(19) "2016-08-01 13:03:06"
["etid"]=>
string(1) "3"
}
[32]=>
object(stdClass)#87 (3) {
["weight"]=>
string(5) "26380"
["added_date"]=>
string(19) "2016-08-01 13:03:07"
["etid"]=>
string(1) "3"
}
[33]=>
object(stdClass)#88 (3) {
["weight"]=>
string(1) "0"
["added_date"]=>
string(19) "2016-08-01 13:03:12"
["etid"]=>
string(1) "3"
}
}


Now at this point I want to test if on that interval there is a etid with the value of 4 or 5 ( and there is one at 2016-08-01 13:02:19), in that case I want to ignore this interval and go to the next one.

This is how my function looks like:

private function _checketid($list){
foreach($list as $row){
if ($row->etid == 4 or $row->etid ==5){
return False;
}
return True;
}
}


And this is how I call it:

if ($this->_checketid($newinterval) != False){
$finallist[]= $this->_maxvalueinterval($newinterval);
}


But that list finallist will append evan if the etid is 4 or 5 on some point in the interval. What is wrong? Thank you!

Answer

You add return true inside the loop so the loop runs only once... Move that return true outside the loop to let the loop run ;)

private function _checketid($list){
         foreach($list as $row){
            if ($row->etid == 4 or $row->etid ==5){
                 return False;
             }

         }
         return True;
}
Comments