Forum Moderators: coopster
So...say I run this query:
SELECT review FROM bookreview WHERE id=1
That will give me a rather lengthy review. What I would like to do is display only the first X number of words or characters (preferably words), add a ... and then link it to the rest of the story.
My question is how to cut short "review" after X words or characters. I presume it can be done in either PHP or MYSQL but I have no idea how.
I am uncertain if this is important, but this query is going to loop at least several times so I need to be able to work whatever code you give me into a loop so I can pull multiple reviews.
i.e.
// $text is the result from the query. Note that I'm guessing here, the actual code may vary, but to give you the idea...
$text = trim(substr($text, 0, strrchar($text, ' ')));
Basically, you're just getting the text from the beginning(0) to the last occurence of a space (' ').
My SQL is as follows: SELECT * FROM review ORDER BY timestamp DESC LIMIT 3
while ($row = mysql_fetch_array($sql)) {
excerpt_shorten = $row["excerpt"];
if (strlen($excerpt_shorten >= 100)) {
$excerpt_shorten = '...';
}
}
This is not working. $excerpt_shorten ends up being the same as $excerpt regardless of the length of the longtext field from SQL.
while ($row = mysql_fetch_array($sql)) {
$excerpt_shorten = $row["excerpt"];
if (strlen($excerpt_shorten >= 100)) {
$excerpt_shorten = substr($excerpt_shorten, 0, 100);
$excerpt_shorten = trim(substr($excerpt_shorten, 0, strrchar($text, ' '))).'...';
}
}
$excerpt_shorten should now be around 100 chars long and without the last word cut off. One other note, you're overwriting the excerpt_shorten value with the subsequent calls.
Hope that helps.
Storm
One other note, you're overwriting the excerpt_shorten value with the subsequent calls
If you're calling more than one row, you can't overwrite the excerpt_shorten value. You could then do this:
while ($row = mysql_fetch_array($sql)) {// get the exceprt data
$exc = $row["excerpt"];// check to see if it's longer than 100 characters...
if (strlen($exc >= 100)) {
$exc = substr($exc, 0, 100);
}
// get rid of the last word or partial word and add '...'
$excerpts[$row['timestamp']] = trim(substr($exc, 0, strrchar($text, ' '))).'...';
}
Which would give you a new array called $excerpts with the timestamp as the key and the excerpt as the value.
Yes, I did try my code :) I always do. Even made a dummy database for the test :)
<?
include ("connect.php");
$queryString = "SELECT * FROM review ORDER BY timestamp DESC LIMIT 3";
$sql = mysql_query($queryString);
$alternate = "2";
while ($row = mysql_fetch_array($sql)) {
$excerpt1 = $row["excerpt"];
$yorn = $row["yorn"];
$titleid = $row["titleid"];
$authorid = $row["authorid"];
$reviewerid = $row["reviewerid"];
// get the exceprt data
$exc = $row["excerpt"];
// check to see if it's longer than 100 characters...
if (strlen($exc >= 100)) {
$exc = substr($exc, 0, 100);
}
// get rid of the last word or partial word and add '...'
$excerpts[$row['timestamp']] = trim(substr($exc, 0, strrchar($text, ' '))).'...';
}
A couple of questions.
First, what variable do I want to echo back? It is $excerpts?
Second, what is $text?
Third, is strrchar correct or should it be strrchr?
Try this code:
<?php
include ("connect.php");
$queryString = "SELECT * FROM review ORDER BY timestamp DESC LIMIT 3";
$sql = mysql_query($queryString);$alternate = "2";
while ($row = mysql_fetch_array($sql)) {
$excerpt = $row["excerpt"]; // Raw $excerpt
$titleid = $row["titleid"];
$reviewerid = $row["reviewerid"];
$authorid = $row["authorid"];
$yorn = $row["yorn"];// Is $exceprt longer than 100 chars?
if (strlen($excerpt) > 100) {
$excerpt = substr($excerpt, 0, 100); // Yep, trim it down.
}// Get rid of the last word or partial word and add '...'.
$excerpt = trim(substr($excerpt, 0, strrpos($excerpt, ' '))).'...';// Print the data out to make sure it's working.
$output = 'Title ID: '.$titleid.'<br />';
$output.= 'Excerpt: '.$excerpt.'<br />';
$output.= 'Reviewew ID: '.$reviewerid.'<br />';
$output.= 'Author ID: '.$authorid.'<br />';
$output.= 'Yorn: '.$yorn.'<br /><br />';echo $output;
}
?>