Jump to content

[SOLVED] Limit number of characters


betsy_ls

Recommended Posts

I know very little PHP, but am trying to help out a friend by editing some of theirs.  They have a news section on their home page that lists the titles of the 3 most recent news items.  My friend wants to limit the number of characters that are displayed with "..." at the end.

 

Here is the code for the news section...

 

<div id="sideheadlines"> 

<p class="title">News</p>
<?php

getHeadlines();
/*
include("admin/nCMS/nSQL.php");
$db = new nSQL("local");
$q = "SELECT nDate, nTitle FROM `gtt_news` ORDER BY nDate DESC LIMIT 5;";
$r = $db->select($q);
while($data = $db->get_row()) {
echo "<div class=\"newsitem\">";
echo "<p class=\"date\">".$data['nDate']."</p>";
    echo "<p class=\"text\">".$data['nTitle']."</p>";
    echo "<p class=\"text\"><a href=\"#\">Read More</a></p>";
echo "</div>"; 
}
*/
?>
</div>

 

Please let me know if you need me to post anymore code.

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/172927-solved-limit-number-of-characters/
Share on other sites

Thank you ldb358.  I hope this is the code you want.  :)

 

<?php

include("includes/nSiteSQL.php");

include("admin/common/print_array.php");



function getHeadlines() {



$newsheadlines = array();




$db = new nSQL();

$q = "SELECT ID, date_format(nDate, '%m.%d.%Y') as nDateF, nTitle, nDate FROM `gtt_news` ORDER BY `nDate` DESC LIMIT 5;";

$db->select($q);



while($data = $db->get_row(MYSQL_NUM)) {

	$key = $data[3] . "news";

	$newsheadlines[$key] = $data;

	$newsheadlines[$key][4] = "news";

}



$new = array_merge($newsheadlines);

krsort($new);



$count = 0;

foreach($new as $key => $val) {

	if($count<3) {

		$type = $val[3];

		if($type == 'blog') {

			$link = "blogarchive.php?blogID=".$val[0];

		} else {

			$link = "newsarchive.php?newsID=".$val[0];

		}



		echo "<div class=\"newsitem\">";

		echo "<p class=\"date\">".$val[1]."</p>";

		echo "<p class=\"text\">".$val[2]."</p>";

		echo "<p class=\"text\"><a href=\"$link\">read more</a></p>";

		echo "</div>"; 



		$count++;

	}

}



}


?>

 

Also, I tried the code you posted, but it didn't work.  Where am I supposed to put it.  Remember I know very little php.

try this:

<?php

include("includes/nSiteSQL.php");

include("admin/common/print_array.php");



function getHeadlines() {



   $newsheadlines = array();


   

   $db = new nSQL();

   $q = "SELECT ID, date_format(nDate, '%m.%d.%Y') as nDateF, nTitle, nDate FROM `gtt_news` ORDER BY `nDate` DESC LIMIT 5;";

   $db->select($q);

   

   while($data = $db->get_row(MYSQL_NUM)) {

      $key = $data[3] . "news";

      $newsheadlines[$key] = $data;

      $newsheadlines[$key][4] = "news";

   }

   

   $new = array_merge($newsheadlines);

   krsort($new);



   $count = 0;

   foreach($new as $key => $val) {

      if($count<3) {

         $type = $val[3];

         if($type == 'blog') {

            $link = "blogarchive.php?blogID=".$val[0];

         } else {

            $link = "newsarchive.php?newsID=".$val[0];

         }
         $numberofcharaters = 50;
         if(strlen($val[2]) > $numberofcharaters){
                         list($body, $extra)  = str_split($val[2], $numberofcharaters);
                         $body = $body . "...";
         }else{
                $body = $val['2'];
        }

         echo "<div class=\"newsitem\">";

         echo "<p class=\"date\">".$val[1]."</p>";

         echo "<p class=\"text\">$body</p>";

         echo "<p class=\"text\"><a href=\"$link\">read more</a></p>";

         echo "</div>"; 

         

         $count++;

      }

   }



}


?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.