Jump to content

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

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.