Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions /

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

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

Searching through a string

Go to End



Community Member, 71 Posts

4 February 2014 at 1:13pm


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!