Related Records?

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

By design9 - May 9, 2011

Jason,

I have it ready now and it is working but it doesn't seem to pull articles correctly. My category field is a list field with a multi-select so users can tag different categories for the articles and is pulled from database in my article_category table. Since the category is a multi-select, would that make it not pull the articles correctly. I am thinking it may not know which category to pull from, thus causing the issue.

Thanks!

April

Re: [design9] Related Records?

By Jason - May 10, 2011

Hi April,

What we'll need to do then is to go through each of the categories of the selected article and create an individual piece of the WHERE clause for it.

Try something like this:
(NOTE: in this example, we're still using the variable $article, so you'll need to change this to match what you have on your page)

$where = "";
$articleCategories = array();


$articleCategories = explode("\t", trim($article['category'], "\t"));

foreach ($articleCategories as $category) {
$where .= " category LIKE '%\t$category\t%' OR";
}

$where = rtrim($where, "OR"); //remove the last "OR"

if ($where) {
$where = "($where) AND num != '".intval($article['num'])."'";
}
else {
$where = "num != '".intval($article['num'])."'";
}

list($relatedArtilce, $relatedArticlesMetaData) = getRecords(array(
'tableName' => 'articles',
'allowSearch' => false,
'limit' => 5,
'orderBy' => 'createdDate DESC',
'where' => $where,
));


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/

By design9 - May 13, 2011

Thanks Jason...worked perfectly!

April

By osga - July 18, 2012


What we'll need to do then is to go through each of the categories of the selected article and create an individual piece of the WHERE clause for it.

Try something like this:
(NOTE: in this example, we're still using the variable $article, so you'll need to change this to match what you have on your page)

$where = "";
$articleCategories = array();


$articleCategories = explode("\t", trim($article['category'], "\t"));

foreach ($articleCategories as $category) {
$where .= " category LIKE '%\t$category\t%' OR";
}

$where = rtrim($where, "OR"); //remove the last "OR"

if ($where) {
$where = "($where) AND num != '".intval($article['num'])."'";
}
else {
$where = "num != '".intval($article['num'])."'";
}

list($relatedArtilce, $relatedArticlesMetaData) = getRecords(array(
'tableName' => 'articles',
'allowSearch' => false,
'limit' => 5,
'orderBy' => 'createdDate DESC',
'where' => $where,
));


Hope this helps




We're trying to do the same thing, but I am not good enough with php to understand:
1- Where to put the code in my page,
2- where to find the code in my page that i should replace "$articles" with

can u walk me through this?
Seth

Re: [osga] Related Records?

By Jason - July 18, 2012

Hi Seth,

This code would go on your detail page, probably right underneath the code that was selecting your single record for that page.

Here are some things you will need to customize:

1) $article needs to be changed to the variable you're using to output your article details on the page

2) ['category'] needs to be changed to the name of the category field in your article section (if different)

3) change "category" highlighted in blue in the code below to match the name of the category field in your article section:

$where .= " category LIKE '%\t$category\t%' OR";

There's a typo in the getRecords() call that you might want to change:

list($relatedArticles, $relatedArticlesMetaData) = getRecords(array(
'tableName' => 'articles',
'allowSearch' => false,
'limit' => 5,
'orderBy' => 'createdDate DESC',
'where' => $where,
));



That should do it. You should then have a variable called $relatedArticles that you can output anywhere you like.

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/

By osga - July 19, 2012

this is what i have so far...is it in the right spot?

<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php


// load viewer library
$libraryPath = 'cmsAdmin/lib/viewer_functions.php';
$dirsToCheck = array('/var/www/html/osga.com/','','../','../../','../../../');
foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }

// load records
list($articlesRecords, $articlesMetaData) = getRecords(array(
'tableName' => 'articles',
'where' => whereRecordNumberInUrl(1),
'limit' => '1',
));

$where .= " category LIKE '%\t$category\t%' OR";
list($relatedArticles, $relatedArticlesMetaData) = getRecords(array(
'tableName' => 'articles',
'allowSearch' => false,
'limit' => 5,
'orderBy' => 'createdDate DESC',
'where' => $where,
));



$articlesRecord = @$articlesRecords[0]; // get first record


// show error message if no matching record is found
if (!$articlesRecord) { dieWith404("Record not found!"); }



?>

Re: [osga] Related Records?

By osga - July 19, 2012

err...no, that blew up my page...lol

here is my code from the top of the page...where does the code u gave me go?

<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php


// load viewer library
$libraryPath = 'cmsAdmin/lib/viewer_functions.php';
$dirsToCheck = array('/var/www/html/osga.com/','','../','../../','../../../');
foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }

// load records
list($articlesRecords, $articlesMetaData) = getRecords(array(
'tableName' => 'articles',
'where' => whereRecordNumberInUrl(1),
'limit' => '1',
));


$articlesRecord = @$articlesRecords[0]; // get first record


// show error message if no matching record is found
if (!$articlesRecord) { dieWith404("Record not found!"); }



?>

By osga - July 20, 2012

didnt work, but its not blowing up the page either...so we must be close....

http://www.osga.com/online_gaming_articles.php


any other ideas?

Re: [osga] Related Records?

By Jason - July 21, 2012

Hi,

Sorry about that. Since your articles can only belong to 1 category, and not multiple, the code is a lot simpler.

Try this:

$where = "";

if ($where) {
$where = "categoryNum = '".mysql_escape($articlesRecord['categoryNum'])."' AND num != '".intval($articlesRecord['num'])."'";
}


list($relatedArtilce, $relatedArticlesMetaData) = getRecords(array(
'tableName' => 'articles',
'allowSearch' => false,
'limit' => 5,
'orderBy' => 'createdDate DESC',
'where' => $where,
));


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/