Jump to content

Keeping a single line in a table.


elmas156

Recommended Posts

This sounds like it should be pretty simple but I've looked for a while and I'm having trouble finding an answer.  What I have is a simple table with 3 columns with predetermined widths.  When the text that is queried from the database is inserted into the table, and the text string is longer than the column width, it pushes the text to a second line, which throws the whole table out of whack.  Instead, I want the text to be cut off before going to the next line.  For example:

 

This is what is happening:

 

This is a sample string of

text.

 

This is what I'm trying to do:

 

This is a sample string...

 

Can anyone tell me where to find the solution to this problem?

 

Not that it's needed, but here is some simple code that I'm working with:

<?php

$message = "This is a sample string of text.";

echo "<table width=\"150\" height=\"20\"border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
echo "<tr>";
echo "<td width=\"50\" height=\"20\" align=\"left" valign=\"top\">Column 1</td>";
echo "<td width=\"50\" height=\"20\" align=\"left" valign=\"top\">Column 2</td>";
echo "<td width=\"50\" height=\"20\" align=\"left" valign=\"top\">$message</td>";
echo "</tr>";
echo "</table>";

?>

Link to comment
Share on other sites

I've had this problem too.  What I do is put this around the text: <div style="overflow:auto; width:100px;">text goes here</div>.  You have to set the width to the same width as your column though.  The problem is, this doesn't add the "..." that you want.

Link to comment
Share on other sites

Pikachu, can you tell me more about your solution?  It sounds like it would be worth a try but I'm not sure how I would use substr() to truncate if I don't know how long the string is before hand.  Is there a way to truncate by determining how many characters you want displayed rather than how many characters you DON'T want displayed?

Link to comment
Share on other sites

I found it... but I edited my last message to this:

 

I'm not sure how I would use substr() to truncate if I don't know how long the string is before hand.  Is there a way to truncate by determining how many characters you want displayed rather than how many characters you DON'T want displayed?

 

By the way thanks for your help!

Link to comment
Share on other sites

Since you've created this topic, I'm really interested in adding this to my site as well.  I found this code online.  It appears to be exactly what we're looking for, but it needs to be edited some:

 

<?php

 

$file = "Hellothisfilehasmorethan30charactersandthisfayl.exe";

 

function funclongwords($file)

{

if (strlen($file) > 30)

{

$vartypesf = strrchr($file,".");

$vartypesf_len = strlen($vartypesf);

$word_l_w = substr($file,0,15);

$word_r_w = substr($file,-15);

$word_r_a = substr($word_r_w,0,-$vartypesf_len);

 

return $word_l_w."...".$word_r_a.$vartypesf;

}

else

return $file;

}

 

?>

 

One problem with this code is that it puts the "..." in the middle of the words, which is good if you want to see the beginning and end of the line, but I want the "..." at the end of the words.

 

Will you do me a favor and give me your edited version if it works for you?  I'm not that good at PHP :confused:

Link to comment
Share on other sites

Never mind, I was able to edit it.  I made it short and simple:

 

<?php

 

if (strlen($words) > 10)

{

$short = substr($words,0,10);

echo ''.$short.'...'; // This will display 10 letters and ... (Textistool...)

}

else

{

echo 'The text that isn't too long goes here';

}

 

?>

 

As you can see, it's easy to change how long you want the text to be before the ...

 

I did notice one problem with using this code though.  If one of your columns has 10 W's (WWWWWWWWWW), it's much longer than 10 a's (aaaaaaaaaa), so it still may go over to the next line :(.  It's not a huge problem I guess.  To fix this problem, I use this before the text: <div style="overflow:auto; width:100px;">TEXT HERE</div>  Make it the width of your column, and those W's will just be cut off instead of go to the next line, but you won't be able to see the "..." because it will be cut off too..

Link to comment
Share on other sites

OK Ethan, here is the solution to our problem.  It works exactly how we want it, with the "..." at the end of the string.  Plus it's a lot simpler than the other code.

 

<?php
$text = "This is whatever text you want to go into the table.";

if (strlen($text) > 40) {

$short_text = substr($text,0,40);

echo "$short_text...";

} else {

echo "$text";

}
?>

 

Thanks for your help in figuring this one out.

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.