Sign in to follow this  
activestate

Searching fot term in repeater.

Recommended Posts

Hi,
I'm having problems with seatching for a value in repeater.
One of pages have attached repeater called 'insight_repeater'.
This repeater has h1_tag (text field) and i'm trying to search for repeater items that contains 'lor' phrase.

And right now i'm trying to do simple search, looking for phrase 'lor'. I've tried many approaches, but nothins seems to work, so i want to ask what i'm doing wrong here:

<?php $query = $page->get("insight_repeater")->find('h1_tag~=Lor'); ?>

Thanks for help.
 

Share this post


Link to post
Share on other sites
3 minutes ago, kixe said:

Hi @kixe !

Thanks for fast reply, but what is the difference between those two as both uses $pages ? (not $page and $pages).
get() will get repeater from current page, and find() will search in whole project ?
Cheers.

 

Share this post


Link to post
Share on other sites

I've just checked this.
first case returns:
noticee: trying to get property on non object in...

and the second returns nothing ...
I'm using latests dev version.

Cheers.

Recently, i see that my code is actually working, but if h1_tag contains "Lorem" i'm only able to make this work if i'll type:

<?php $query = $page->get("insight_repeater")->find('h1_tag~=Lorem'); ?> and not Lor. Is there any workaround ?

Share this post


Link to post
Share on other sites

One strange thing is:

This page, that containes insight_repeater also have h1_tag, and this query will return page's h1_tag instead of repeater's h1_tag:


 

<?php $query = $pages->find("insight_repeater.h1_tag~=Lorem"); ?>

 

Share this post


Link to post
Share on other sites
3 hours ago, activestate said:

<?php $query = $page->get("insight_repeater")->find('h1_tag~=Lorem'); ?> and not Lor. Is there any workaround ?

Welcome to the forums @activestate

Have a read here about selectors, specifically about MySQL and short words.

Quote

The *= and ~= rely upon MySQL fulltext indexes, which only index words of at least a certain length (configurable, but typically 4 characters). They also don't index common English words called stopwords. So while it's preferable to use *= and ~= for their speed, if you aren't getting the results you need, you should consider using %= instead (if you can handle the speed hit).

 

Please use code blocks (@see the <> icon) around your code.

  • Like 3
  • Thanks 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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By LMD
      I don't know if this is the right place, it's not really a tutorial, just a tip based on notes I wrote myself in a recent project to get it straight in my own head. I thought it might be useful for others in a similar situation.
      Scenario:
      Create a search function that will search for keywords "foo" and "bar" in multiple fields, but the keywords do not have to be adjacent, in order, or even all in the same field. For eample, the selector must match if "foo" is in "field_a" and "bar" is in "field_b" -- so long as both keywords are present somewhere, the page match is valid.
      It is possible to just split the terms and do multiple queries on each field separately and then combine the results into a single PageArray for pagination (I believe there is a module that helps with this). However, I wanted to see if it was possible to do a basic version with a single query.
      Not The Solution:
      The following selector does not work when keywords appear separately in different fields (operator '~=' - contains all the words):
      $selector = "title|field_a|field_b~=foo bar"; What the selector is saying:
      FIND BOTH "foo" AND "bar" IN title OR FIND BOTH "foo" AND "bar" IN field_a OR FIND BOTH "foo" AND "bar" IN field_b In this case, both "foo" and "bar" have to be in the same field (but not adjacent or in order) to match.
      The Actual Solution
      What we need to use is "named selectors" to let us match each individual keyword separately while still using one selector.
      Using the same example as before:
      $selector = "selector1=(title|field_a|field_b~=foo), selector2=(title|field_a|field_b~=bar)"; What the selector is saying at its most basic level:
      FIND BOTH selector1 AND selector2 Or, to expand on this, it is saying:
      (FIND "foo" IN title OR field_a OR field_b) AND (FIND "bar" IN title OR field_a OR field_b) Crucially, "foo" and "bar" do not have to be in the same field to match.
      Practical Method
      In this example code, I am actually allowing the search for phrases (using "quoted text") as well as individual terms, so a person could enter...
      "foo bar" baz ... and it will keep "foo bar" together aa one term and "baz" as a separate term and match them as an exact phrase.
      // Keywords obtained from $input->get and cleaned (multiple spaces removed)/sanitized etc. $keywords = '"foo bar" baz'; // Split into individual search terms by space (preserve spaces in quoted text) $terms = str_getcsv($keywords, " "); // array("foo bar", "baz") // Build up named selectors $ns = ""; // named selectors string $i=1; // named selector count foreach ($terms as $term) { // operator '*=' - contains the exact word or phrase $ns .= ", ns{$i}=(title|field_a|field_b*=" . trim($term) . ")"; $i++; } //$ns = ", ns1=(title|field_a|field_b*=foo bar), ns2=(title|field_a|field_b*=baz)" // Construct the whole selector (modify/add other general selectors as needed) $selector = "template=my-template, limit=20, sort=-date" . $ns; // Find pages based on selector $results = $pages->find($selector);  
      DISCLAIMER
      I haven't done any tests to see if this method is more efficient than running queries on each field separately and combining the results, I just wanted to see if it was possible!
    • By Xonox
      Hi,
      I have a template that it's working fine in development, however I can't get it to work on production! It shows every information inside repeater fields except the images.
      Here's the template:

      These are the circuit_day_image settings:

      This is the code:
      <?php foreach($page->circuit_days as $circuit_day) { if($circuit_day->circuit_day_image) { $day_image = $circuit_day->circuit_day_image->size(300, 300)->url; echo '<img src="' . $day_image . '" />'; } else { echo 'No image! :('; } echo '<h2>' . $circuit_day->title . '</h2>'; echo $circuit_day->body; } ?> I always get "No image! :("
      I think I'm doing everything right!
      Anyone else with a similar problem?
      Update
      After uploading the production database into my server, the images stopped working. It can be one of two problems:
      1. Bad field configuration;
      2. Something wrong with the Database.
      I can't find the problem. Any suggestion is welcome, thanks,
      Update 2
      I forgot to upload the images. It's working on dev and not on production. Still no clue!
      Clue 1
      When I insert
       <pre><?php print_r($circuit_day); ?></pre>
      On development I get a clean list for each repetition:

      However, on production, the command gets on a weird recursive loop that takes forever (it even slows the browser to a halt):

      What might be going on?
    • By burning
      Hi All, maybe a really stupid question, but is there a way to sort repeater items on -created?
      Like $page->social_activity('limit=10,sort=created'), with 'social_activity' as the repeater field?
      Tried this but it didnt work 😞 
      Any help appreciated! 
    • By verdeandrea
      Hello girls and guys,
      i just saw that if I use a selector on a textarea, for example 
      $pages->find('body%=notes') it also find that key in html classes and attributes. For example it will find
      <p class="text-notes">Lorem ipsum dolor sit amet cosectetur.</p> Is there a way to avoid this anche check only the texts without the html tags?
      Thanks!
    • By theoretic
      Hello there! And thanks for Processwire.
      Got one more interesting question. One of my templates called goods has two repeaters. The first one is variants, each item containing possible sizes, colors and so on. The second one is available_variants, each item should contain a region and a variant which should be selectable from the list of variants.
      So the task is to get items of one repeater from inside the page reference field which is the part of another repeater, both repeater belonging to the same page.
      Processwire gives some options for retrieving selectable pages in this case. We can use:
      Parent. Not an option because we need dynamic parent being in fact the current page which contains both repeaters. Template. Not an option again because we need to select repeater items not only by template but also by page id which is dynamic. Custom find. Could be a solution but have no idea how to substitute the current page id into ID query. Selector string. It's almost what we need, but again i have no idea how to substitute something like "parent={$parent->id}" in this case. Custom PHP code. This needs to be placed inside site/ready.php and should contain a hook which finds pages using all the power of PW API. Think that the last option is the solution, but could not get the result i need.
      The simplest possible way seems to use $event->arguments('page')->variants. But it returns nothing. Tried different selectors like "template=repeater_variants, parent=".$event->arguments('page')->id , but still no result.
      Will be grateful for any help. Thanks in advance!