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
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.

Link to comment
Share on other sites

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++;

      }

   }



}


?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.