I have a Codeigniter website where I am displaying some values using a foreach()
. I am trying to ignore specific values from database.
I wrote the following code:
$res="select * from paymentform order by id desc limit 1000";
$query = $this->db->query($res);
foreach ($query->result_array() as $re) {
if($re['authstatus']!='success' || $re['authstatus']!='0300'){
// some processing
}
}
The issue is that the columns containing success
or 0300
are not being filtered out.
3
Answers
change your if to this
I think if you want to get all condition in your if statement, you must using ‘&&’ not ‘||’
We know that statement has different function, when you using || it will ignore some condition when there are 1 condition has true value. But, when you using && it will be checked one by one your condition untill all condition has true.
Let’s try implemented into your code from this …
To this…
The meaning is, when
$re['authstatus']!='success'
it will checked again another condition$re['authstatus']!='0300'
The problem with filtering in PHP (after you have limited the total rows to 1000) is that your resultant array will have an inconsistent number of rows.
If you build your filtration logic into your query, you will have a better chance of hitting that 1000 limit. As others have mentioned, you made a basic mistake in your condition logic regarding "and" and "or" operators. An
authstatus
of0300
satisfies the first condtion ($re['authstatus']!='success'
).Let’s avoid it altogether with a refined query using CI3’s query builder methods.
CI3 Model Code:
The
select()
method is not needed when you want to return all columns from the table. Now you can feed$result
to yourforeach()
loop.