, Guest!
Already a Member? Login or Register.

Menu



Showcase


Due to the volume of spam happening on our forums, posting is now restricted to verified members only.  If you're not verified, drop us a note with your username.

Home > FlexCMS Support Forum > User Help > Code Snippets > Articles/News Block

FlexCMS Support Forum


Articles/News Block
Started January 20, 2006 @ 8:54pm by otter
Post Message 
otter
Administrator



Posts: 177
 
Articles/News BlockJanuary 20, 2006 @ 8:54pm
Simply paste the following code into a block and enable PHP on it.

The $ArticlesCount and $Categories at the top work the same as those sections in the articles code that goes in pages.

NOTE: If plan on modifying the code (the MySQL portions especially), it is highly recommended that you do so in a page first and then move it to a block when you're done with it. If you experiment on a live block you may find yourself needing to modify it from the database to get it working again.


Code

$ArticlesCount = '5';
$CategoriesList = '0';

$TeaserLength = 100;

// ========================

$ArticlesCode = '';
$ArticlesPrinted = 0;






if (strpos($ArticlesCount,',') !== false) {
list($ACOffset,$ACCount) = explode(',',$ArticlesCount);
$LimitString = intval($ACOffset).','.intval($ACCount);
}
else {
$LimitString = intval($ArticlesCount);
}

if ($CategoriesList != '') {

$CategoriesArray = explode(',',$CategoriesList);
for ($i = 0; $i < count($CategoriesArray); $i++) {
$CategoriesArray[$i] = intval($CategoriesArray[$i]);
}
$CategoriesList2 = implode(',',$CategoriesArray);

$query_na = "SELECT * FROM `".$Settings['DBPrefix']."na-Articles` WHERE ApprovedBy!='' AND Category IN (".$CategoriesList2.") ORDER BY DateCode DESC LIMIT ".$LimitString;
}
else {
$query_na = "SELECT * FROM `".$Settings['DBPrefix']."na-Articles` WHERE ApprovedBy!='' ORDER BY DateCode DESC LIMIT ".$LimitString;
}

$result_na = mysql_query($query_na) or die (mysql_error());
while ($row_na = mysql_fetch_array($result_na)) {

if ($row_na['Teaser'] == '') {
$StrippedArticle = strip_tags($row_na['Article']);

if (strlen($StrippedArticle) > $TeaserLength) {
$TeaserSection = substr($StrippedArticle,0,$TeaserLength);
while (substr($TeaserSection,strlen($TeaserSection)-1,1) != ' ') {
$TeaserSection = substr($TeaserSection,0,strlen($TeaserSection)-1);
}
while (substr($TeaserSection,strlen($TeaserSection)-1,1) == ' ') {
$TeaserSection = substr($TeaserSection,0,strlen($TeaserSection)-1);
}

$TeaserSection .= '... <font size="1">[<a href="'.$MainURL.'/articles/view/'.$row_na['RecordNumber'].'.html">'.$MsgText[7][78].'</a>]</font>';
$row_na['Title'] = '<a href="'.$MainURL.'/articles/view/'.$row_na['RecordNumber'].'.html">'.$row_na['Title'].'</a>';
$ArticleLinked = 'y';
}
else {
$TeaserSection = $StrippedArticle;
}
}
else {
$TeaserSection = substr($row_na['Teaser'],0,$TeaserLength);

if (strlen($row_na['Teaser']) > strlen($TeaserSection)) {
while (substr($TeaserSection,strlen($TeaserSection)-1,1) != ' ') {
$TeaserSection = substr($TeaserSection,0,strlen($TeaserSection)-1);
}
while (substr($TeaserSection,strlen($TeaserSection)-1,1) == ' ') {
$TeaserSection = substr($TeaserSection,0,strlen($TeaserSection)-1);
}
}

$TeaserSection .= '... <font size="1">[<a href="'.$MainURL.'/articles/view/'.$row_na['RecordNumber'].'.html">'.$MsgText[7][78].'</a>]</font>';
$row_na['Title'] = '<a href="'.$MainURL.'/articles/view/'.$row_na['RecordNumber'].'.html">'.$row_na['Title'].'</a>';
$ArticleLinked = 'y';
}


$ArticlesPrinted++;

if ($ArticlesCode != '') {
$ArticlesCode .= '<br><br>';
}

$ArticlesCode .= '<b>'.$row_na['Title'].'</b><br>'.$TeaserSection;




} // end while rows loop


print $ArticlesCode;



ONLINE TRAINING CLASSES FOR FLEXCMS IN MAY: FlexCMS Basics (101), Sat., May, 15, 2010 & FlexCMS Blocks (Mon., May 10, 2010).
 

Last Edit: August 20, 2006 @ 6:34pm by DCSun
cberks


Posts: 63
 
WHAT IS THE SYNTAXAugust 20, 2006 @ 10:08am
What exactly is the syntax for $categories???

If I have categories 1 CATS 2 DOGS 3 BIRDS and want to select only articles categorized DOGS I have tried


Code


$ArticlesCount = '5';
$Categories = '2';

$ArticlesCount = '5';
$Categories = '2|';

$ArticlesCount = '5';
$Categories = 'DOGS';


but none of these work?
 
DCSun
Administrator



Posts: 625
 
August 20, 2006 @ 1:37pm
Chris,

The first one you pasted in should be the one you want. Without more information it's hard to say why it might not be working.

The only suggestion I can offer with what I have to work with is to make sure the ID of that category is actually 2. If you mouseover the Edit Category link in the admin area you'll see the ID number at the end of the link.

If you want to use more than one category, separate them with commas like this: $Categories = '3,4,5';


David


FlexCMS v3.2 Has Been Released!
 
cberks


Posts: 63
 
NEWS IN A PAGEAugust 20, 2006 @ 2:19pm
Actually I am misusing that code perhaps by using it in page. If you look at www.bennscc.org you will see that I have 3 areas on the page with NEWS in them the bottom right uses the code from the snippet (intended for a block) and shows the latest 5 news articles, fits into the table width="50%" and also is formatted according to a div css I have defined in the template. but it will not recognize the $categories setting.

The 2 left ares pastor's message and anouncements use the "insert news into page" function and does honor categories but b locks the css and forces the cell of the table in the page to be wider than the 50% definition

Should the code snippet work in a page?? (as opposed to a block)?? or is there a way to correct the effects of the "insert news" approach

Do you want me to start a new thread on this rather than clutter up the code snippets forum?

Regards, Chris
 
DCSun
Administrator



Posts: 625
 
August 20, 2006 @ 6:33pm
Chris,

You are correct that code doesn't work as it should. It looks like there was a small typo -- the categories variable should be $CategoriesList not $Categories. I've updated the code above to reflect this. My apologies for the inconvenience. And as previously mentioned, you would separate them by commas if you wanted more than one category used.

As for using code in a block or a page, anything that works in one will work in the other -- there's no difference in how they're processed. The only thing to watch out for is re-using the same variables if you're using code in a page that may also be in a block or the other way around. If you get some unexpected results that's a good place to start looking.

As for formatting, you're probably best to go with the PHP code in the page and then you'll have complete control over all of it.


David


FlexCMS v3.2 Has Been Released!
 

Post Message 




Try & Buy FeedForAll - Easy to use RSS Feed Creator - great for iTunes users!

MEMBERS




All Contents, Code, Scripts and Technologies Copyright 2003-2009 FlexCMS.
All Rights Reserved. Software License Agreement

Processing Time: 0.08471 seconds.
 
Management Login

Powered By FlexCMS
Powered By FlexCMS