NEWS- display first 50 words, how?

2 posts by 2 authors in: Forums > CMS Builder
Last Post: October 25, 2010   (RSS)

Hi there,

I was wondering if it's possible for me to generate a form which will show only first 50 words of a news entry (no images if any are included in the main article) as well as a link which basically says "find out more" and takes me to the main news section... This is the code which i currently have on my home page:

<?php


// load viewer library
$libraryPath = 'cmsAdmin/lib/viewer_functions.php';
$dirsToCheck = array('/homepages/45/d281998588/htdocs/casaroccapiccola/','','../','../../','../../../');
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($home_pageRecords, $home_pageMetaData) = getRecords(array(
'tableName' => 'home_page',
'where' => 'num=1',
'limit' => '1',
));
$home_pageRecord = @$home_pageRecords[0]; // get first record

// show error message if no matching record is found
if (!$home_pageRecord) {
header("HTTP/1.0 404 Not Found");
print "Record not found!";
exit;
}

?>

<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<?php echo $home_pageRecord['content'] ?>
<!-- /INSTRUCTIONS -->
</DIV>
</DIV>
</DIV>
<!--END HOME COL-->
<DIV id=home-col>
<!--END FEATURE WRAP-->
</DIV>
<!--END HOME COL-->
<DIV id=home-col class=last>
<DIV id=blog-entry-wrap>
<h3 class="style14"><img src="123_files/photos/latest.jpg" width="198" height="36"></h3>
<DIV class="style19" id=blog-entry>

<p align="justify"><!-- /STEP2: Display Records --><?php


// load viewer library
$libraryPath = 'cmsAdmin/lib/viewer_functions.php';
$dirsToCheck = array('/homepages/45/d281998588/htdocs/casaroccapiccola/','','../','../../','../../../');
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($newsRecords, $newsMetaData) = getRecords(array(
'tableName' => 'news',
'where' => 'num=5',
'limit' => '1',
));
$newsRecord = @$newsRecords[0]; // get first record

// show error message if no matching record is found
if (!$newsRecord) {
header("HTTP/1.0 404 Not Found");
print "Record not found!";
exit;
}

?>

<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
</p>
<p align="right">
<!-- INSTRUCTIONS -->
<!-- /INSTRUCTIONS -->
<!-- For date formatting codes see: http://www.php.net/date -->
<?php echo $newsRecord['content'] ?>
<?php if (!$newsRecord): ?>
No record found!
<?php endif ?>
</p>
<p align="right"><span class="style14"><a href="news.php"><img src="123_files/photos/findout.jpg" width="157" height="36" border="0"></a></span></p>
<H3 align="justify" class="textarea style28"><span class="style29"><img src="123_files/photos/featured.jpg" width="198" height="36"></span></H3>
<P align="justify" class="style28">&nbsp;</P>
<?php


// load viewer library
$libraryPath = 'cmsAdmin/lib/viewer_functions.php';
$dirsToCheck = array('/homepages/45/d281998588/htdocs/casaroccapiccola/','','../','../../','../../../');
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($home_pageRecords, $home_pageMetaData) = getRecords(array(
'tableName' => 'home_page',
'where' => 'num=2',
'limit' => '1',
));
$home_pageRecord = @$home_pageRecords[0]; // get first record

// show error message if no matching record is found
if (!$home_pageRecord) {
header("HTTP/1.0 404 Not Found");
print "Record not found!";
exit;
}

?>

<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<p>
<!-- INSTRUCTIONS -->
<!-- /INSTRUCTIONS -->
<?php echo $home_pageRecord['content'] ?></p>



many thanks for all the help!

Re: [mrmzalewski] NEWS- display first 50 words, how?

Pretty easy mrmzalewski,

Here's a recipe from my CMSB Cookbook http://www.thecmsbcookbook.com that describes exactly how to do what you're looking for:

Insert this function at the top of your page, or before you want to invoke the word limiting function:

<?PHP
function maxWords($textOrHtml, $maxWords) {
$text = strip_tags($textOrHtml);
$words = preg_split("/\s+/", $text, $maxWords+1);
if (count($words) > $maxWords) { unset($words[$maxWords]); }
$output = join(' ', $words);

return $output;
}
?>


Then put this code where you want the words limited (to 50 words):

<?PHP echo maxWords($record['content'], 50);
?>
If you want to add ...more and a link, use something like this:

<?PHP echo maxWords($record['content'], 50);
?>...<a href="<?php echo $record['_link']; ?>”>Read More</a>

or

<?PHP echo maxWords($record['content'], 5);
?>...<a class="read-more" href="http://www.your_site.com/your_detail_page.php?<?php echo $record['num'] ?>">(Read More)</a>


To allow certain tags like or <p> to appear in your text, change the strip_tags code:

$text = strip_tags($textOrHtml);
to

$text = strip_tags($textOrHtml, '<br />');

The function is pretty literal, but it seems to automatically include the closing tag, so include all the flavors of all the tags (spaces
and slashes, slashes with no space, etc.) that you want to include. Like this:

$text = strip_tags($textOrHtml, '<br /><p>');

If you’re using a WYSIWYG Editor and are losing some formatting when using the MaxWord function, Jason Sauchuk of Interactive Tools
suggests using this version of the function instead:

<?PHP
function maxWords($textOrHtml, $maxWords) {
$text=str_replace("<p>","*P*",$textOrHtml);
$text= str_replace("</p>","*/P*",$text);
$text = strip_tags($text);
$words = preg_split("/\s+/", $text, $maxWords+1);
if (count($words) > $maxWords) { unset($words[$maxWords]); }
$output = join(' ', $words);
$output=str_replace("*P*","<p>",$output);
$output=str_replace("*/P*","</p>",$output);
$output.="</p>";

return $output;
}
?>


Hope that gets you where you want to go,

Best,

Jerry Kornbluth
Head Chef
The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php