Jump to:

23471 Posts in 18941 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Searching through a string

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 143 Views
  • Bagzli
    Community Member
    31 Posts

    Searching through a string Link to this post


    I am trying to create filter functionality on the website and I'm going about it with the following logic. I have a text field where user can assign whichever keyword he wants to the news article, so he can type things like: "History, News, Other"

    Now I want to loop through every article and have it split the string by the commas and compare each word to see if it is a news article. if it is it will keep it in the variable, if it is not then it will remove it.

    Here is some code that I am trying however it is not working out.

    public function PaginatedPages() {
    $paginatedItems = new PaginatedList(getNewsArticles()->sort('Date DESC'), $this->request);
    return $paginatedItems;
    public function getNewsArticles(){
    $newsArticles = ArticlePage::get();

    foreach ($newsArticles as article){
    $categories = article::get('category');

    return $newsArticles;

    My foreach loop is breaking and I'm not quite sure why. How do I loop through the article to achieve this effect? If there are better ways of doing this, I'm all ears for that too. The idea is that the user can enter however many categories they want and they can be whatever they want, so that when we do a search on categories, I can loop through all the articles and bring back the result.

    Something that just occurred to me, can I do this on a database level so that it returns ArticlePage object that have that keyword in the string? Might be easier way to do it, but again not sure how to accomplish it.

    Any and all help is much appreciated!

Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.