Jump to content

[SOLVED] query length


onthespot

Recommended Posts

Hey guys, i have searched high and low online for an answer to this but possibly am not searching for the correct thing.

 

I am looking to limit the amount of characters shown by a mysql_query.

For instance if various fields that are returned are all different lengths, I want just the first 10 characters to show and then ......, of course I will provide a link to click to reveal the rest.

 

$res=mysql_query("SELECT * FROM ".TBL_COMMENTS." ORDER BY commentdate DESC LIMIT 5") or die(mysql_error);

while($row=mysql_fetch_assoc($res)){

$comment=$row['comment'];

 

That is what I have so far, I have managed to bring up the 5 most recent comments. If you know how I could achieve this, I would bre grateful of a push in the right direction, thankyou

Link to comment
https://forums.phpfreaks.com/topic/165388-solved-query-length/
Share on other sites

What I understand you to mean is that you are pulling a set of comments from a database and you want to show a truncated version of those comments, with a link to show more.

$res=mysql_query("SELECT * FROM ".TBL_COMMENTS." ORDER BY commentdate DESC LIMIT 5") or die(mysql_error);
while($row=mysql_fetch_assoc($res)){
  $comment=$row['comment'];
  if(strlen($comment)>$your_max_length-3) {
    $x=$your_max_length/2;
    $short_c=substr($comment,0,$x-3).'...'.substr($comment,-$x);
    echo "<p onclick='javascript_reveal_full_comment();'>$short_c</p>";
    echo "<p class='hidden_full_comment'>$comment</p>";
  } else {
    echo "<p>$comment</p>";
  }
}

 

This is off the top of my head, but should do the trick.  You have to supply your own CSS and javascript.

Link to comment
https://forums.phpfreaks.com/topic/165388-solved-query-length/#findComment-872255
Share on other sites

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.