Jump to content

[SOLVED] Display a maximum 20 letters


Warptweet

Recommended Posts

I have this code that will retreive the names of Flash Movies/Games from my website...

 

<?php
include("include/connect.php");
$sql = "SELECT * FROM flashid ORDER BY flashid DESC LIMIT 20";
$res = mysql_query($sql) or die (mysql_error());
while($r=mysql_fetch_assoc($res)){
echo "<a href=\"viewflash.php?flashid=".$r['flashid']."\">".$r['flashname']."</a></br>";
}

?>

 

Now, how would I make it so that if the name is longer than 20 letters, it will replace the last 3 letters with "..."? Because names longer than 20 letters will stretch my tables, so I suppose "..." will be good enough to simply signify the name is longer.

Link to comment
https://forums.phpfreaks.com/topic/40612-solved-display-a-maximum-20-letters/
Share on other sites

in my previous post, we're manipulating a string. this string is called $string. now, for you, it would be whatever string that you wanted to manipulate... so that would be $r['flashname']. so this is how you impliment it into your code:

<?php
        include("include/connect.php");
        $sql = "SELECT * FROM flashid ORDER BY flashid DESC LIMIT 20";
        $res = mysql_query($sql) or die (mysql_error());

        while($r=mysql_fetch_assoc($res)){
                if(strlen($r['flashname']) >= 20){
                        $r['flashname'] = substr($r['flashname'], 0, 17) ."...";
                }
                echo "<a href=\"viewflash.php?flashid=".$r['flashid']."\">".$r['flashname']."</a></br>";
        }
?>

 

 

 

P.S. don't forget to click the 'topic solved' mod at the bottom-left of this thread.

Archived

This topic is now archived and is closed to further replies.

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