Jump to content
hansv

Can I use a variable in a find selector

Recommended Posts

I extended my users-template with a lot af fields?  One of them is 'instrument'

The code beyond is working fine (see screenshot 1).  'instrument' is a page-select-field in the user template (the pages are violin, cello, clarinet, ...).  But this is not nice and efficient coding!!

// Violin
$person = $users->find('instrument=violin');
echo "violin";
foreach ($person as $p) {
  // $user fields are shown
    echo $p->first_name;
}

// cello
$person = $users->find('instrument=cello');
echo "cello";
foreach ($person as $p) {
  // $user fields are shown
   echo $p->first_name;
}

// clarinet
$person = $users->find('instrument=clarinet');
echo "clarinet";
foreach ($person as $p) {
  // $user fields are shown
    echo $p->first_name;
}

// and so on

screenshot 1.PNG

With a foreach the code could me more efficient, but the find-selector givns a problem with the variable $instr.  
 

// all instrument in a foreach
foreach ($user->instrument as $instr){
   $person = $users->find('instrument=$instr');  // does not work, neither do find('$instr') and find($instr)
      echo $instr->title;
	 foreach ($person as $p) {
         // $user fields are shown
         echo $p->first_name;
     }
}

With $person = $users-find($instr);  only the value of $instr is shown (e.g. violin) but not  the values of $p (e.g. first name of person. (see screenshot 2)

screenshot 2.PNG

 

How can I use a variable as find-selector?

 

Share this post


Link to post
Share on other sites
$person = $users->find("instrument=$instr");

or:

$person = $users->find('instrument='.$instr);

Variables won't be parsed inside single quotes!

  • Like 2

Share this post


Link to post
Share on other sites

Hi,

 

try this.

$person = $users->find('instrument=' . $instr);

or

$person = $users->find("instrument=$instr");
// or
$person = $users->find("instrument={$instr}");

 

Variables inside single quote is just text / string. With double quotes php parses variables. 

  • Like 2

Share this post


Link to post
Share on other sites

Thx adrian and pwfoo for this quick answer.  This code is working very well

$person = $users->find('instrument=' . $instr);

 

But my foreach loop doesn't work.  I only get the first instrument.  

Share this post


Link to post
Share on other sites

I think the problem is that you need to foreach all the instruments, not just the ones in the user's instrument field. Probably something like this:

foreach ($pages->get("/instruments/")->children() as $instr){
   $person = $users->find('instrument='.$instr);
  • Like 2

Share this post


Link to post
Share on other sites


@adrian  Als I see it know, this answer is the logic itself and it works very well.   PW and its community is fantastic, thx

  • Like 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By snck
      Hi there,
      I have a problem constructing a selector that finds all pages that refer to pages with a specific template.
      I have pages using an event template and I want to show events based on a specific context. In this example I want to filter the results and only show event pages that relate to a specific template (exhibitions) in their page field related_pages.
      What I tried:
      $events = $pages->find("template=event, related_pages.template.name=exhibition"); Unfortunately it does not work (0 results).
      Same with this:
      $events = $pages->find("template=event, related_pages=[template.name=exhibition]"); At the moment I am helping myself with the following lines, but I have a strong feeling that there is a more efficient solution:
      $events = $pages->find("template=event"); foreach($events as $event){ if(!count($event->related_pages->find("template=exhibition"))){ $events->remove($event); } }  
      I really hope that one of you can help me out.
      Thanks in advance!
      Flo
    • By Kiwi Chris
      The selector in the following code included in a template is returning nothing, however if I take out the compId.resultsdate<={$today} bit, it works fine, although obviously not filtered on the date field.
      $today = strtotime(date('Y-m-d')); $setImages = $pages->find("template=competitionImage, compId={$page->id}, compId.resultsdate<={$today}, compSubject.name=s, imageRating.title=Merit|Honours,check_access=0"); Here's the results of an example from Tracey Debugger
      templates_id=79, resultsdate<=1587729600, status<2048   SELECT pages.id,pages.parent_id,pages.templates_id FROM `pages` JOIN field_resultsdate AS field_resultsdate ON field_resultsdate.pages_id=pages.id AND (((field_resultsdate.data<='2020-04-25 00:00:00' ) )) WHERE (pages.templates_id=79) AND (pages.status<2048) GROUP BY pages.id Over in my ready.php I have inside a hook that refers directly to the page template that's used for the pages in the page field above:
      $today = strtotime(date('Y-m-d')) $event->return = $event->pages->find("template=competition,eventEnd>={$today},eventStart<={$today}"); In this case the filtering on date fields (albeit different ones) works fine. Can anyone suggest why the filter on the date subfield of the page field isn't working?
      Just to confirm, I do have a date value in the field, and it is a date before today. 🙂
      The problem may be something blatantly obvious, but I can't for the life of me figure out why the selector is returning no results when I include the date filter.
    • By Roberts R
      I have simple page structure:
      category subcategory simple-product simple-product simple-product simple-product subcategory .... category2 so Im at category page and running
      $pages->find("template=simple-product, has_parent=$page, limit=8"); and it returns 0 pages
       
      but this returns 4 pages
      $pages->find("template=simple-product, has_parent=$page"); Can someone explain why limit does not work here?
      EDIT:
      So I did some tests and it seems that any limit=n where n is >= actual page count that is possible ... selector return 0 results.
    • By schwarzdesign
      ProcessWire added the numReferences property in 3.0.107, which returns the count of all pages having a reference to the page object. However, apparently it's not possible to use this property inside a selector. I'm trying to find all pages that are referenced at least once:
      $pages->find('template=service, numReferences>0'); This throws an error: "Field does not exist: numReferences".
      Is there another way to filter by the number of references? Of course, I could manually filter the results of the find query, but that feels overly complicated. Ideally, I would also like to filter the number of references from a specific field; that is, find all pages that are referenced at least once in one specific page reference field.
      Is there a way to do this? I guess supporting numReferences in selectors would be a feature request - if so, is it feasible?
      Thanks!
       
    • By jds43
      Hello, I have a Page Reference by template radio button field to promote a certain page (only two options), but I'd like to target the page that isn't selected. This would be used dynamically throughout the site.
      Does anyone know how I could accomplish this? Would I use something like remove() or not()?
       
×
×
  • Create New...