php devuelve la última fila con truncar (sbstr)

I've got just a quick problem. I've got code that returns the last post for separate titles for a forum. I want to limit the number of characters shown and put the (... …).

I'm having difficulty with a nested if statement in my while loop:

    $query  = "SELECT comment FROM Assembly WHERE title = 'Checkered' ORDER BY date DESC LIMIT 1";
$result = mysql_query($query);


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

 if(strlen($message) > 10)
  {
   echo substr($message, 0, 10)."…";
  }
 else
  {
    echo    "Last Post:<br>";
    echo    strip_tags("{$row['comment']}");

  }
}

gracias de antemano :)

**edit yes css text-overflow:ellipsis; works great!! Thanks guys

***edit working code incase you want

   $query  = "SELECT comment FROM Assembly WHERE title = 'Checkered' ORDER BY date DESC LIMIT 1";
$result = mysql_query($query);

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

 $comment = strip_tags($row['comment']);
 if(strlen($comment) > 30)
  {
   echo substr($comment, 0, 30)."...";
  }
 else
  {
    echo    "Last Post:<br>";
    echo    $comment;

  }
}

preguntado el 30 de junio de 12 a las 19:06

what kind of difficulty do you encounter here? -

you can use css text-overflow:ellipsis and make some fancy effect/popups to view full text without using ajax -

Ten charac <- That's 10 characters if you leave only alphanumeric characters in the string. Kinda short. -

2 Respuestas

The problem you have with your code is that you have some badly named variables. You assign the result resource returned by mysql_query() a $result, you then try and fetch a row from a variable called $message (which you don't show the assignment of) and you then switch to treating $message as if it were a string, rather than manipulating the value in $row.

Regardless of that, if you just want to fetch the string from the DB and apply an ellipsis, and you don't need the full text, I would get MySQL to do the work for me:

SELECT IF(CHAR_LENGTH(`comment`) > 10, CONCAT(SUBSTRING(`comment`, 1, 10), '...'), `comment`) AS `comment`
FROM `Assembly`
WHERE `title` = 'Checkered'
ORDER BY `date` DESC
LIMIT 1

En PHP:

$query  = "
  SELECT IF(CHAR_LENGTH(`comment`) > 10, CONCAT(SUBSTRING(`comment`, 1, 10), '...'), `comment`) AS `comment`
  FROM `Assembly`
  WHERE `title` = 'Checkered'
  ORDER BY `date` DESC
  LIMIT 1
";
$result = mysql_query($query);

while ($row = mysql_fetch_assoc($result)) {
  echo    "Last Post:<br>";
  echo    htmlspecialchars($row['comment']);
}

EDITAR

Following @Jared's comments about the potential for broken strings displaying partial HTML tags, here is the way to do it that will avoid this:

$query  = "
  SELECT `comment`
  FROM `Assembly`
  WHERE `title` = 'Checkered'
  ORDER BY `date` DESC
  LIMIT 1
";
$result = mysql_query($query);
// Loop is pointless because of the LIMIT 1
$row = mysql_fetch_assoc($result);

$maxlen = 10; // Max length before trim
$comment = strip_tags($row['comment']);
if (strlen($comment) > $maxlen) {
  $comment = substr($comment, 0, $maxlen).'...';
}
$comment = htmlspecialchars($comment);

echo "Last Post:<br>";
echo $comment;

Respondido el 30 de junio de 12 a las 20:06

Where does the ellipsis come in at? - Jared Farrish

@JaredFarrish In the MySQL query. EDITAR Oh wait I forgot the CONCAT - DaveRandom

I was just about to put that in a comment... ;) - Jared Farrish

One other note... The OP seems to suggest that the comment field may contain markup. This might bomb without some stripping means. - Jared Farrish

I've always thought it kinda goofy there isn't a regex capability in MySQL. - Jared Farrish

You give nout over as to the problem but I am going to go out on a limb and guess the problem. Try this:

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

 $comment = strip_tags($row['comment']);
 if(strlen($coment) > 10)
  {
   echo substr($comment, 0, 10)."...";
  }
 else
  {
    echo    "Last Post:<br>";
    echo    $comment;

  }
}

Respondido el 30 de junio de 12 a las 19:06

Yes this works!! See my fixed edit ^top in the question for working code. - pasatiempo

@hobbywebsite - Note, there is a more appropriate way to do this: codepad.org/IfUt3QWK Note, I won't swear on anything that regex is perfect for removing markup, but it looks reasonable. - Jared Farrish

@JaredFarrish Or if you wanna just nuke without parsing: $blurb = preg_replace('/<[^>]*>/', '', $blurb); - sammaye

Or whatnot something something mumble mumble. Regex. Not my specialty. - Jared Farrish

Regex wasn't the point, though, it was the rest of the method that I was being critical of. More or less. :) - Jared Farrish

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.