...

View Full Version : Limiting text



stfc_boy
08-15-2007, 06:00 PM
Hi Guys,

I have a field in my database named about and I want to limit the amount of characters outputted (to 100)of this, so I thought it would be done like so:



<?php

$query = mysql_query("SELECT * FROM players ORDER BY RAND() LIMIT 1") or die(mysql_error());
$text = substr($about,0,100);


while($row = mysql_fetch_array($query)) {

echo '<h3>'. $row['player_name'] .'</h3>'."\n";
echo '<div class="profile_image"><img src="images_two/odhiambo_delete.jpg" /></div>';
echo '<div class="profile_text">'. $row['text'] .'</div>'."\n";

}
?>


However, that does not seem to work, where am I going wrong?

Thanks

Chris

whizard
08-15-2007, 06:47 PM
Try



<?php

$query = mysql_query("SELECT * FROM players ORDER BY RAND() LIMIT 1") or die(mysql_error());
while($row = mysql_fetch_array($query)) {
$text = substr($row['about'],0,100);
echo '<h3>'. $row['player_name'] ."</h3>\n";
echo '<div class="profile_image"><img src="images_two/odhiambo_delete.jpg" /></div>';
echo '<div class="profile_text">'. $text ."</div>\n";

}
?>


HTH
Dan

meth
08-15-2007, 08:04 PM
Here's a little function I use to truncate text to the last word within a defined char limit. It takes the text to be truncated and the max char limit as arguements respectively. The returned value is a truncated string under the max char limit and ending in a complete word.



// truncateStringToWord
// truncates $str [string] to a max char limit
// and will truncate whole words only.
// returns the truncated string
function truncateStringToWord( $str,$str_limit )
{
$str_short = current(explode("\n", wordwrap($str, $str_limit, "\n")));
return $str_short;
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum