Related Records?

34 posts by 6 authors in: Forums > CMS Builder
Last Post: July 24, 2012   (RSS)

By Chris - August 30, 2010 - edited: August 30, 2010

HI Illume,

The regular expression I posted above will split on a comma followed by any number of spaces. Here it is again for reference:

$keywords = preg_split('/,\s*/', $record['keywords']);

For example, "a quick, brown fox" would turn into two keywords: "a quick" and "brown fox".

If you want to split on commas and/or spaces, you can use this regular expression instead:

$keywords = preg_split('/[,\s]+/', $record['keywords']);

For example, "a quick, brown fox" would turn into four keywords: "a", "quick", "brown", and "fox".

I hope this helps! Please let me know if you have any questions.
All the best,
Chris

By Perchpole - August 30, 2010

Hi, Chris -

Just one thing of interest, where I inserted "showme" - as you suggested - it always throws up the same statement...
0 OR keywords LIKE '%animals%' OR keywords LIKE '%science%'
It does this in every instance, regardless of whether it has found articles with matching keywords or not.

I'm not sure how to interpret it?

:0s

Perch

Re: [Perchpole] Related Records?

By Jason - August 31, 2010

Hi Perch,

What you're outputting is the where clause of you sql query. The statement you posted will return records articles that have animals or science in the keywords field. The where clause is used to find the records, so it will be the same regardless of whether it successfully returns articles or not.

Hope this helps.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] Related Records?

By Perchpole - August 31, 2010

A-ha! Thanks for that, Jason.

Whilst we're on the subject, do you think we could expand the functionality of this script a little...

Would it be possible, for example, to list the returned articles in order of matching "hits"? This would mean an article with 3 keyword matches would appear higher in the order than one that matched only 1 keyword.

In a similar way, would it be possible to stipulate the number of hits required before an article is returned? So again, as an example, if the number were "3" only those articles which match 3 keywords or more would be returned.

This would act as a more concentrated filter for sites with lots of articles.

Do-able?

:0)

Perch

Re: [Perchpole] Related Records?

By Jason - August 31, 2010

Hi Perch,

Yes, this is possible, but it would require some more complicated custom programming. If this is something you're interested in doing, send an email to consulting@interactivetools.com going over the functionality you would want and we can get you a quote on what this would cost.

Hope this helps.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] Related Records?

By Perchpole - August 31, 2010

It was just a thought. Thanks anyway.

Perch

Re: Related Records?

By design9 - May 6, 2011

Hello,
I also want to include a related articles section within my articles. I have an articles section and a category section set-up. My articles are tagged in a category which is how I display them so what Chris was talking about in this thread is exactly what I need:

"The simplest solution I can imagine would be to use a category system and show recent articles in the same category. You don't necessarily have to make your categories visible to the user. "

Can someone help me on how to do this by showing related articles in the same category?

Thanks!
April

Re: [design9] Related Records?

By Jason - May 6, 2011

Hi April,

I'm assuming that you want to do this display on an articles detail page, so we know the category of the current article. In that case, you can use that category in your query to get other articles:

For example:

list($relatedArtilce, $relatedArticlesMetaData) = getRecords(array(
'tableName' => 'articles',
'allowSearch' => false,
'limit' => 5,
'orderBy' => 'createdDate DESC',
'where' => "category = '".mysql_escape($article['category'])."' AND num != '".intval($article['num'])."'",
));


In this example, we're returning the 5 latest articles that have the same category as our $article Record. We add the "num !=" part so that we don't return our current article again.

Hope this helps get you started.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] Related Records?

By design9 - May 9, 2011

Jason,

Thanks for your help. I have tried this code and I am getting the following error:

Notice: Undefined variable: article in D:\Inetpub\charlotteparent\article1.php on line 59

Here is the code I have:

list($relatedArticle, $relatedArticlesMetaData) = getRecords(array(
'tableName' => 'articles',
'allowSearch' => false,
'limit' => 5,
'orderBy' => 'createdDate DESC',
'where' => "category = '".mysql_escape($article['category'])."' AND num != '".intval($article['num'])."'",
));

Should I be using something different for $article['category'] ?

Thanks!

~A

Re: [design9] Related Records?

By Jason - May 9, 2011

Hi,

In the example I posted, $article was the individual article record that you're outputting on a detail page. You'll need to change that name to reflect the name of the variable that you're using on your page. You may also have to change the name "category" to match what is in your section.

Hope this helps.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/