foreach() only runs on first row

Created on 20 February 2024, about 1 year ago
Updated 26 February 2024, about 1 year ago

foreach() only runs on first row

I’m trying to select a subset of users based on their country.

In either Field (list of countries selected) or Filter (users in countries selected) I only get a result on the first row.

If I fetch a single field from the db, Views PHP will process every row. If I fetch an array and extract the countries using a foreach loop, Views PHP only processes the first row.

Steps to reproduce

In the Setup code I run a database query, get my selected countries and store the array in $static. In the Output I copy the results from $static to $array, run a foreach ($array as $selected) loop and print $selected->country. This works on the first row, but all other rows report no results. Unsetting or resetting $array makes no difference.

There is also a repeated warning: Undefined array key "args" in DatabaseLog->findCaller() (line 156 of /includes/database/log.inc).

Is there some other way to display multiple results in a Views PHP field or make multiple selections on a Views PHP Filter?

💬 Support request
Status

Active

Version

1.1

Component

Field Handler

Created by

🇬🇧United Kingdom jamesbisset

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @jamesbisset
  • 🇬🇧United Kingdom jamesbisset

    If it makes things simpler:

    I have some users in a manager role. Managers are allocated a country to manage. A logged in manager can see only the users they are responsible for.

    This is easily done by rejecting all users whose country does not match the logged-in user country, using a Views PHP filter.

    But if a manager is allocated two or more countries, matched against the user’s country via a foreach loop, then Views PHP will only process the first user.

    Does that help?

  • 🇬🇧United Kingdom jamesbisset

    And if I try to store the manager’s country values in a $row->php field, one value works on every user but multiple values only work on the first user.

    Should this be considered a bug?

Production build 0.71.5 2024