Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

using ellipses to truncate data in a cell

Posted on 2008-10-04
13
Medium Priority
?
675 Views
Last Modified: 2012-05-05
I have a  td cell that shows data from a 1000 character input text box.
Can I display this:

<td>Display only the first 100 characters of a ....</td>

instead of

<td>Display all 1000 characters etc, etc.</td>

by automatically inserting an ellipses after a certain number of characters?

<table class="records">
<tr>
        <th>q1</th>
        <th>q2</th>
</tr>
<?
 
mysql_select_db($database_athconn, $athconn);
$query = "SELECT * FROM results";
$result = mysql_query($query, $athconn);
$num = mysql_numrows($result);
mysql_close();
$i=0;
while ($i < $num)
{
        $field1= mysql_result($result,$i,"q1");
        $field2= mysql_result($result,$i,"q2");
 
echo "<tr><td>$field1</td><td>$field2</td></tr>";
 
$i++;
}
 
?>
</table>

Open in new window

0
Comment
Question by:seanpowell
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 3
13 Comments
 
LVL 82

Expert Comment

by:hielo
ID: 22641224

while ($i < $num)
{
        $field1= mysql_result($result,$i,"q1");
        $field2= mysql_result($result,$i,"q2");
 	$field1=preg_replace('\S+$','...',substr($field1,0,100));
 	$field2=preg_replace('\S+$','...',substr($field2,0,100));
echo "<tr><td>$field1</td><td>$field2</td></tr>";
 
$i++;
}

Open in new window

0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 22641950
You can use substr - it is simpler code and might be a bit faster than REGEX (however I have not tested it for speed).

If you need a function to break on word boundaries, please post back here and I'll give you the one I use for "teasers" after the article headlines.

Best, ~Ray
while ($i < $num)
{
        $field1= substr(mysql_result($result,$i,"q1"),0,100) . '...';
        $field2= substr(mysql_result($result,$i,"q2"),0,100) . '...';
 
echo "<tr><td>$field1</td><td>$field2</td></tr>";
 
$i++;
}

Open in new window

0
 
LVL 31

Author Comment

by:seanpowell
ID: 22642281
Well that's a tough call - I think both will likely work for me.
The only problem is that a blank record still returns the ellipses.

Thanks guys,
Sean
0
WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

 
LVL 82

Expert Comment

by:hielo
ID: 22642384
>>You can use substr
The "problem" with that is that the ellipsis are likely to start right in the middle of a word. My suggestion makes sure that the ellipsis starts right after a word.

>>The only problem is that a blank record still returns the ellipses.
try the attached code:
while ($i < $num)
{
        $field1= trim(mysql_result($result,$i,"q1"));
        $field2= trim(mysql_result($result,$i,"q2"));
	   if( strlen($field1) > 100 )
 		$field1=preg_replace('\S+$','...',substr($field1,0,100));
	   if( strlen($field2) > 100 )
 		$field2=preg_replace('\S+$','...',substr($field2,0,100));
echo "<tr><td>$field1</td><td>$field2</td></tr>";
 
$i++;
}

Open in new window

0
 
LVL 31

Author Comment

by:seanpowell
ID: 22644324
Thanks hielo - I believe that is returning blank cells instead of any content.
0
 
LVL 82

Assisted Solution

by:hielo
hielo earned 200 total points
ID: 22644951
sorry, this:
'\S+$'

should have been:
'#\S+$#'

while ($i < $num)
{
        $field1= trim(mysql_result($result,$i,"q1"));
        $field2= trim(mysql_result($result,$i,"q2"));
         if( strlen($field1) > 100 )
             $field1=preg_replace('#\S+$#','...',substr($field1,0,100));
         if( strlen($field2) > 100 )
             $field2=preg_replace('#\S+$#','...',substr($field2,0,100));
echo "<tr><td>$field1</td><td>$field2</td></tr>";
 
$i++;
}


IF tha still does not work try this:
while ($i < $num)
{
        $field1= mysql_result($result,$i,"q1");
        $field2= mysql_result($result,$i,"q2");
 	$field1=preg_replace('#\S+$#','...',substr($field1,0,100));
 	$field2=preg_replace('#\S+$#','...',substr($field2,0,100));
	
	if( $field1=="..."){$field1="";}
	if( $field2=="..."){$field2="";}
echo "<tr><td>$field1</td><td>$field2</td></tr>";
 
$i++;
}

Open in new window

0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 22644969
Here is the function that I use - sorry I could not get back to post sooner, but hope it's still helpful to you. ~Ray
/* ************************************************************************** */
// RETURN FIRST FEW WHOLE WORDS FOLLOWED BY ELLIPSES
// $length IS MINIMUM TRUNCATION CHARACTER COUNT
function teaser_fragment($text, $length=32) {
	if (strlen($text) < $length) {
return $text; }
 
	$x = 0;
	while ($x == 0) {
		if (substr($text,$length,1) == ' ') { $x = 1; } else { $length++; }
	}
	$text = substr($text,0,$length) . "...";
return $text;
}
/* ************************************************************************** */

Open in new window

0
 
LVL 31

Author Comment

by:seanpowell
ID: 22649106
hielo - I'm sorry, that doesn't work as expected, it only adds ellipses if the count is reached in the middle of a word.

Ray - I guess I'm putting the 2 functions together incorrectly? I like the simplicity of the code...
function teaser_fragment($text, $length=32) {
        if (strlen($text) < $length) {
return $text; }
 
        $x = 0;
        while ($x == 0) {
                if (substr($text,$length,1) == ' ') { $x = 1; } else { $length++; }
        }
        $text = substr($text,0,$length) . "...";
return $text;
}
 
while ($i < $num)
{
        $field1= substr(mysql_result($result,$i,"q1"),0,100) . '...';
        $field2= substr(mysql_result($result,$i,"q2"),0,100) . '...';
 
echo "<tr><td>$field1</td><td>$field2</td></tr>";
 
$i++;
}

Open in new window

0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 22649876
You might want to ACTUALLY CALL THE FUNCTION!  ;-)

Try this and see if it doesn't get you where you want to go.  You may have to tinker with the length parameter (presently default = 32 and your setting = 100).  You might decrease it to 96, etc. just to get the best looking output.

Best, ~Ray
function teaser_fragment($text, $length=32) {
        if (strlen($text) < $length) {
return $text; }
 
        $x = 0;
        while ($x == 0) {
                if (substr($text,$length,1) == ' ') { $x = 1; } else { $length++; }
        }
        $text = substr($text,0,$length) . "...";
return $text;
}
 
while ($i < $num)
{
        $field1= teaser_fragment(mysql_result($result,$i,"q1"),100);
        $field2= teaser_fragment(mysql_result($result,$i,"q2"),100);
 
echo "<tr><td>$field1</td><td>$field2</td></tr>";
 
$i++;
}

Open in new window

0
 
LVL 31

Author Comment

by:seanpowell
ID: 22649990
Yes - that might help...

The db is timing out arounf row 9 - in your experience is there anything in that code that may cause that to happen?
0
 
LVL 31

Author Comment

by:seanpowell
ID: 22650086
Displays correctly again once the code is removed.
<?php require_once('Connections/athconn.php'); ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
 
<table class="records">
	<tr>
		<th>#</th>
		<th>q1</th>
		<th>q2</th>
		<th>q3</th>
	</tr>
 
<?
mysql_select_db($database_athconn, $athconn);
$query = "SELECT * FROM results";
$result = mysql_query($query, $athconn);
$num = mysql_numrows($result);
mysql_close();
$i=0;
function teaser_fragment($text, $length=32) {
        if (strlen($text) < $length) {
return $text; }
        $x = 0;
        while ($x == 0) {
                if (substr($text,$length,1) == ' ') { $x = 1; } else { $length++; }
        }
        $text = substr($text,0,$length) . "...";
return $text;
}
 
while ($i < $num) {
	$field1= mysql_result($result,$i,"q1");
	$field2= teaser_fragment(mysql_result($result,$i,"q2"),96);
	$field3= teaser_fragment(mysql_result($result,$i,"q3"),96);
	$i++;
	echo "<tr>";
	echo "<td>$i</td><td>$field1</td><td>$field2</td><td>$field3</td>";
	echo "</tr>";
}
?>
</table>
 
</body>
</html>

Open in new window

0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 1800 total points
ID: 22650182
Well, if there is never a blank at the end of the input, it will loop infinitely.  Let's try this modified function (my original is too dependent on my data).
function teaser_fragment($text, $length=32) {
        if (strlen($text) < $length) {
return $text; }
 
// ADD A BLANK AT THE END TO FORCE STOP
        $text .= ' ';
        $x = 0;
        while ($x == 0) {
                if (substr($text,$length,1) == ' ') { $x = 1; } else { $length++; }
        }
        $text = substr($text,0,$length) . "...";
return $text;
}

Open in new window

0
 
LVL 31

Author Comment

by:seanpowell
ID: 22650338
Thank you guys - all is working perfectly now. The fragment seems to be a really nice way to handle that.
0

Featured Post

WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question