betsy_ls Posted September 3, 2009 Share Posted September 3, 2009 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 More sharing options...
ldb358 Posted September 3, 2009 Share Posted September 3, 2009 can you post the getHeadlines() function also this should work: if(strlen($data) > $numberofcharaters){ list($body, $extra) = str_split($data, $numberofcharaters); } echo $body . "..."; Link to comment https://forums.phpfreaks.com/topic/172927-solved-limit-number-of-characters/#findComment-911442 Share on other sites More sharing options...
betsy_ls Posted September 3, 2009 Author Share Posted September 3, 2009 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 https://forums.phpfreaks.com/topic/172927-solved-limit-number-of-characters/#findComment-911675 Share on other sites More sharing options...
ldb358 Posted September 3, 2009 Share Posted September 3, 2009 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 https://forums.phpfreaks.com/topic/172927-solved-limit-number-of-characters/#findComment-911744 Share on other sites More sharing options...
betsy_ls Posted September 3, 2009 Author Share Posted September 3, 2009 It worked. Thank you so much! Link to comment https://forums.phpfreaks.com/topic/172927-solved-limit-number-of-characters/#findComment-911812 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.