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
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
Share on other sites

onthespot:  yeah, if you want to send them to another page then you don't need the javascript.  You can use kingphilip's solution to get the truncated string and use a regular query to get the full string on the other page.

Link to comment
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.