Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/52964-solved-truncating-text/
Share on other sites

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);
?>

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

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.

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

?>

 

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);
?>

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.

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.