nmbbd Posted May 25, 2007 Share Posted May 25, 2007 Hello clever programmers, I am a designer doing my best to get to grips with cut & paste PHP programming - I've done OK so far but have come up against a problem or two. I'd be grateful for any help you can give (in layman's / designers terms). I'm creating a simple content management system. The edit page shows a simple list of all records. I want to truncate a paragraph from each record so it restricts itself to, say, 150 characters. The following function does the job on the first record, but when the next record appears and it gets to the truncated paragraph, nothing else appears - not the paragraph or anything after it. Here is the function I have been using: <?php /** * Add this to your page: * <?php * include "shorten_a_text_string.php"; * echo ShortenText($text); * ?> * where $text is the text you want to shorten. * * Example * Test it using this in a PHP page: * <?php * include "shortentext.php"; * $text = "The rain in Spain falls mainly on the plain."; * echo ShortenText($text); * ?> */ function ShortenText($text) { // Change to the number of characters you want to display $chars = 150; $text = $text." "; $text = substr($text,0,$chars); $text = substr($text,0,strrpos($text,' ')); $text = $text."..."; return $text; } ?> <?php echo ShortenText($row_news['article01_para1']); ?> Please help? Thanks! Max Quote Link to comment https://forums.phpfreaks.com/topic/52964-solved-truncating-text/ Share on other sites More sharing options...
taith Posted May 25, 2007 Share Posted May 25, 2007 you'd be suprised how many times i've put my charlimiting function in here <?php function filter_charlimit($string, $length="50"){ return (strlen($string)<$length) ? $string : trim(substr($string,0,$length)).'...'; } echo filter_charlimit($row_news['article01_para1'],150); ?> Quote Link to comment https://forums.phpfreaks.com/topic/52964-solved-truncating-text/#findComment-261586 Share on other sites More sharing options...
nmbbd Posted May 25, 2007 Author Share Posted May 25, 2007 ... and I bet everyone who has received it has been grateful, as am I. Unfortunately it hasn't solved the problem though - it does successfully truncate the paragraph in the first record, but the second record misses that paragraph entirely and displays nothing from that point on. Thanks for your help though, Max Quote Link to comment https://forums.phpfreaks.com/topic/52964-solved-truncating-text/#findComment-261609 Share on other sites More sharing options...
per1os Posted May 25, 2007 Share Posted May 25, 2007 Maybe show some of the actual code that you are working with and someone can sure help you out. If not everything that people say is a guess. I can easily modify taith's function to take in an array of records and return an array of shortened records or use it to parse multiple paragraphs etc. I just need to know what you need, and the best way to show that is show us your current code. Quote Link to comment https://forums.phpfreaks.com/topic/52964-solved-truncating-text/#findComment-261613 Share on other sites More sharing options...
nmbbd Posted May 25, 2007 Author Share Posted May 25, 2007 Yes - sorry I should have given more information before. Here is the code from the entire page (created using Dreamweaver). It's probably more than you need but I don't want to miss anything relevant. The bit I need to truncate is in bold: <?php require_once('../Connections/seastartcmsdata.php'); ?> <?php if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } } mysql_select_db($database_seastartcmsdata, $seastartcmsdata); $query_news = "SELECT * FROM articles01 ORDER BY article01_order ASC"; $news = mysql_query($query_news, $seastartcmsdata) or die(mysql_error()); $row_news = mysql_fetch_assoc($news); $totalRows_news = mysql_num_rows($news); ?><html> <head> <title>Content Management System</title> <link href="cms_style.css" rel="stylesheet" type="text/css"> <style type="text/css"> <!-- .style1 {font-size: smaller} .style2 {font-size: smaller; color: #FFFFFF; } --> </style> </head> <body> <h2>News and Tips <br> Content Management System</h2> <?php do { ?> <table width="770" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td><table width="700" border="0" align="center" cellpadding="5" cellspacing="5" bordercolor="#2982A2"> <tr> <td width="50" bgcolor="#E1E1E1"><?php echo $row_news['article01_month']; ?> <?php echo $row_news['article01_year']; ?></td> <td width="250" bgcolor="#E1E1E1"><a href="edit-articles_02.php?recordID=<?php echo $row_news['news_code']; ?>" class="style1"><strong> <?php echo $row_news['article01_head']; ?></strong></a></td> <td><span class="smaller style1"> <?php echo $row_news['article01_para1']; ?> </span></td> <td width="60" bgcolor="#DDAE93"><div align="center"><a href="delete-news.php?recordID=<?php echo $row_news['news_code']; ?>" class="style2">delete</a></div></td> </tr> </table></td> </tr> </table> <?php } while ($row_news = mysql_fetch_assoc($news)); ?><br> Total Articles: <?php echo $totalRows_news ?> <p><a href="index.html">Return to main menu</a></p> </div></body> </html> <?php mysql_free_result($news); ?> Quote Link to comment https://forums.phpfreaks.com/topic/52964-solved-truncating-text/#findComment-261621 Share on other sites More sharing options...
per1os Posted May 25, 2007 Share Posted May 25, 2007 First off, I would not recommened the do while. The while does just as well, but yea. Also [ code ] tags are your friends. Now you want the article, which has more than one paragraph split like that for multiple paragraphs? <?php // note this is modified to do multiple paragraphs // note2 this is un-tested but yea should work. function filter_charlimit($string, $length="50"){ if (eregi("\n", $string)) { $string = split("\n", $string); foreach ($string as $parse) { $returnString .= filter_char_limit($string, 150) . "\n<p>"; // remove <p> if wanted } return $returnString; }else { return (strlen($string)<$length) ? $string : trim(substr($string,0,$length)).'...'; } } require_once('../Connections/seastartcmsdata.php'); // no need to go in and out of php there. if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } } mysql_select_db($database_seastartcmsdata, $seastartcmsdata); $query_news = "SELECT * FROM articles01 ORDER BY article01_order ASC"; $news = mysql_query($query_news, $seastartcmsdata) or die(mysql_error()); $row_news = mysql_fetch_assoc($news); $totalRows_news = mysql_num_rows($news); ?><html> <head> <title>Content Management System</title> <link href="cms_style.css" rel="stylesheet" type="text/css"> <style type="text/css"> <!-- .style1 {font-size: smaller} .style2 {font-size: smaller; color: #FFFFFF; } --> </style> </head> <body> <h2>News and Tips Content Management System</h2> <?php do { ?> <table width="770" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td><table width="700" border="0" align="center" cellpadding="5" cellspacing="5" bordercolor="#2982A2"> <tr> <td width="50" bgcolor="#E1E1E1"><?php echo $row_news['article01_month']; ?> <?php echo $row_news['article01_year']; ?></td> <td width="250" bgcolor="#E1E1E1"><a href="edit-articles_02.php?recordID=<?php echo $row_news['news_code']; ?>" class="style1"><strong> <?php echo $row_news['article01_head']; ?></strong>[/url]</td> <td><span class="smaller style1"> <?php echo filter_charlimit($row_news['article01_para1'], 150); ?> </span></td> <td width="60" bgcolor="#DDAE93"><div align="center"><a href="delete-news.php?recordID=<?php echo $row_news['news_code']; ?>" class="style2">delete[/url]</div></td> </tr> </table></td> </tr> </table> <?php } while ($row_news = mysql_fetch_assoc($news)); ?> Total Articles: <?php echo $totalRows_news ?> <p><a href="index.html">Return to main menu[/url]</p> </div></body> </html> <?php mysql_free_result($news); ?> Quote Link to comment https://forums.phpfreaks.com/topic/52964-solved-truncating-text/#findComment-261628 Share on other sites More sharing options...
taith Posted May 25, 2007 Share Posted May 25, 2007 welll theres your problem... <?php echo filter_charlimit($row_news['article01_para1'],150); ?> Quote Link to comment https://forums.phpfreaks.com/topic/52964-solved-truncating-text/#findComment-261629 Share on other sites More sharing options...
per1os Posted May 25, 2007 Share Posted May 25, 2007 AHHH, he has multiple paragraphs defined as columns, ahhh. Yea man you need to do that statement for each of the columns. lol. Quote Link to comment https://forums.phpfreaks.com/topic/52964-solved-truncating-text/#findComment-261634 Share on other sites More sharing options...
nmbbd Posted May 25, 2007 Author Share Posted May 25, 2007 Now you want the article, which has more than one paragraph split like that for multiple paragraphs? Not exactly, no. I want an abbreviated list of all records to appear. The viewer will see a list of all existing articles on this page, including the date, headline, (truncated) first paragraph and a delete link for each entry. They will then click on the headline of their choice to edit them individually. Quote Link to comment https://forums.phpfreaks.com/topic/52964-solved-truncating-text/#findComment-261638 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.