Reputation: 13
function getFeed($start, $value) {
$this->subscriptions = $this->getSubscriptionsList();
// Allowed types (if it's empty, return false to cancel the query)
$allowedType = $this->listTypes(($this->subscriptions) ? $this->subscriptions : false);
$allowedDates = $this->listDates(($this->subscriptions) ? $this->subscriptions : false);
// If the $start value is 0, empty the query;
if($start == 0) {
$start = '';
} else {
// Else, build up the query
$start = 'AND messages.id < \''.$this->db->real_escape_string($start).'\'';
}
if(in_array($value, $allowedType)) {
$query = sprintf("SELECT * FROM messages, users WHERE messages.uid IN (%s) AND messages.type = '%s' AND messages.public = '1' AND messages.uid = users.idu %s ORDER BY messages.id DESC LIMIT %s", $this->id.','.$this->subscriptions, $this->db->real_escape_string($value), $start, ($this->per_page + 1));
$value = '\''.$value.'\'';
} elseif(in_array ($value, $allowedDates)) {
$query = sprintf("SELECT * FROM messages, users WHERE messages.uid IN (%s) AND extract(YEAR_MONTH from `time`) = '%s' AND messages.public = '1' AND messages.uid = users.idu %s ORDER BY messages.id DESC LIMIT %s", $this->id.','.$this->subscriptions, $this->db->real_escape_string($value), $start, ($this->per_page + 1));
$value = '\''.$value.'\'';
} else {
// The query to select the subscribed users
$query = sprintf("SELECT * FROM messages, users WHERE messages.uid IN (%s) AND messages.public = '1' AND messages.uid = users.idu %s ORDER BY messages.id DESC LIMIT %s", $this->id.','.$this->subscriptions, $start, ($this->per_page + 1));
$value = '\'\'';
}
// If the user subscribed to other users get the messages (prevents fatal error because of empty IN () query)
if(!empty($this->subscriptions)) {
return $this->getMessages($query, 'loadFeed', $value);
} else {
return $this->showError('welcome_feed');
}
}
Exactly the problem here:
elseif(in_array ($value, $allowedDates)) {
$query = sprintf("SELECT * FROM messages, users WHERE messages.uid IN (%s) AND extract(YEAR_MONTH from `time`) = '%s' AND messages.public = '1' AND messages.uid = users.idu %s ORDER BY messages.id DESC LIMIT %s", $this->id.','.$this->subscriptions, $this->db->real_escape_string($value), $start, ($this->per_page + 1));
$value = '\''.$value.'\'';
}
Between the lines there, but I can not resolve this error. logical errors, but where I'm doing with php? If you help me thank you very much.
Upvotes: 1
Views: 9273
Reputation: 129
You can't be sure that $allowedDates is an array as
$allowedDates = $this->listDates(($this->subscriptions) ? $this->subscriptions : false);
states that it is a boolean in case (see "ternary operators")
$this->subscriptions
is FALSE, NULL, 0 or whatever, not true in any case.
So you need to make sure later, that you actually are handling an array. Like
elseif(is_array($allowedDates) && in_array ($value, $allowedDates)) {
[...]
}
for example.
It's always good practice to check for types when you're expecting a certain one. Also naming your variables accordingly helps a lot.
As the script stands now, you might want to name the var "$mixAllowedDates". If you choose to make sure the ternary operator returns an array in any case, you'd name the variable "$arrAllowedDates".
That way you always know what you are working with.
Upvotes: 5
Reputation: 10613
It's quite simple, the error tells you what the problem is.
At the top of your code snippet you have:
$allowedDates = $this->listDates(($this->subscriptions) ? $this->subscriptions : false);
in your comment above that line, you say you want to set the variable to false, to "cancel the query" (which im interpretting as meaning to make the IF statement here get skipped:
} elseif(in_array ($value, $allowedDates)) {
However, as the error message says, you cannot pass a boolean (instead of an array) to in_array
, yet you set the value to false in order to skip it?
You have two options:
Instead of using false
in your ternary operator, use an empty array array()
instead, since its empty, the value will not exist in it, and the condition will skip as expected, and it will be the correct data type.
If you really must use false in that variable, you need to check if its false first, something like this:
} elseif(false !== $allowedDates && in_array ($value, $allowedDates)) {
Upvotes: 0
Reputation: 69977
The result of
$allowedType = $this->listTypes(($this->subscriptions) ? $this->subscriptions : false);
is returning a boolean value rather than an array.
Do you mean to do:
$allowedType = ($this->subscriptions) ? $this->listTypes($this->subscriptions) : array();
Otherwise check that the method listTypes()
always returns an array. It is returning a boolean value which is causing the error you are getting.
Upvotes: 0