×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Coding

How do you limit the amount of characters shown in a table? by FAM
Posted: 25 May 06

When pulling data from a mysql table you may want to limit the amount of characters shown within a column to control the size of the table.

This basic script connects to a mysql database, pulls 4 columns of data into a html table and using the substr function, it limits 'Column 2' to 30 characters and also appending '...' to the 30th character.

CODE

<?php
$host="localhost";
$user="root";
$pass="";
$database="databasename";

$sConn = mysql_connect($host, $user, $pass) or die("Couldn't connect to database server");
$dConn = mysql_select_db($database, $sConn)or die("Couldn't connect to database $database");

echo '<table border="1" width="100%"><tr align="center"><td>Column 1</td><td>Column 2</td><td>Column 3</td><td>Column 4</td></tr>';

$CharacterLimit=30;     // Define how many characters to be displayed

$sql = "SELECT * FROM tablename";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
    echo "<tr>\n";
    echo "<td width='25%'>".$row['Column1']."</td>\n";
    
    $Column2 = $row['Column2'];
    $ColumnLimit = substr($Column2, 0, $CharacterLimit);
    echo "<td width='25%'>".$ColumnLimit."...</td>\n";
    echo "<td width='25%'>".$row['Column3']."</td>\n";
    echo "<td width='25%'>".$row['Column4']."</td>\n";
    echo "</tr>\n";
}
echo "</table>";
?>
To go more in-depth you could could count the characters of 'Column 2' and if the result was less than 30, you would not need to append the '...'!

Hope this is of some use.

Back to PHP FAQ Index
Back to PHP Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close