I seem to have problems with the substr() method. I think that's the cause of the problem, anyway.
In my database I have a collection of "news". On the splashpage of my website I want to display the abstracts of the two latest news entries. Each abstract should be no more than 160 letters long and should end on the last ".". What I have done is write the function like this:
function Abstract($text,$length)
{
//shorten the text to length
$text=substr($text,0,$length);
//search for position of last '. '
$lastdotpos=strrpos($text,".");
//shorten the text to position of '.'
return substr($text,0,$lastdotpos+1);
}
and I am calling it with this line:
$abstract = Abstract($Row[content],160);
This actually works fine, but when I use it in the entire context of
receiving data out of the database and putting it into two tables, it stuffs up, depending on the content of the news. It's so weird - in some circumstances it works, but then I change the content of the database (row: content) and it suddenly not only does not stop on the last ".", but it also sometimes creates three or four news abstracts instead of only two. This is unusual as the amount of entries that should be displayed are limited by a variable counter.
Probably best if I post the entire code for that section. Hope somebody can see where I went wrong.
Thanks heaps for your feedback!
$varOne = 0; // this variable controls how many news abstracts are being
displayed
// connect to database
$Link = mysql_connect($DBhost,$DBuser,$DBpass);
$Query = "SELECT * FROM news Order BY date desc";
$Result = mysql_db_query ($DBName, $Query, $Link);
//print content from database to screen
if (mysql_db_query ($DBName, $Query, $Link)){
// function to create abstract/snippet from news
function Abstract($text,$length)
{
//shorten the text to length
$text=substr($text,0,$length);
//search for position of last '. '
$lastdotpos=strrpos($text,".");
//shorten the text to position of '.'
return substr($text,0,$lastdotpos+1);
}
WHILE ($Row = mysql_fetch_array ($Result) AND $varOne<2){
$abstract = Abstract($Row[content],160);
print ("<table cellpadding='0' cellspacing='0' border='0'
width='100%'>");
print ("<tr>");
print ("<td valign='top'><img src='images/news_top_left.gif'
width='32' height='27' alt='' border='0'></td>");
print ("<td width='100%' valign='top'
background='images/news_mid_top.gif'><img src='images/spacer.gif' width='1'
height='1' border='0' alt=''></td>");
print ("<td valign='top'><img src='images/news_top_right.gif'
width='21' height='27' alt='' border='0'></td>");
print ("<td><img src='images/spacer.gif' width='7' height='1'
alt='' border='0'></td>");
print ("</tr>");
print ("<tr>");
print ("<td background='images/news_mid_left.gif'><img
src='images/spacer.gif' width='1' height='1' alt='' border='0'></td>");
print ("<td bgcolor='#EBEAEA'><div class='newsTitle'><a
href='about/news.php#news$Row[id]' class='newsTitle'>$thisDate,
$Row[title]</a></div><br><div class='news'>$abstract...</div></td>");
print ("<td background='images/news_mid_right.gif'><img
src='images/spacer.gif' width='1' height='1' alt='' border='0'></td>");
print ("<td><img src='images/spacer.gif' width='7' height='1'
alt='' border='0'></td>");
print ("</tr>");
print ("<tr>");
print ("<td valign='top'><img src='images/news_bottom_left.gif'
width='32' height='26' alt='' border='0'></td>");
print ("<td width='100%' valign='top'
background='images/news_mid_bottom.gif'><img src='images/spacer.gif'
width='1' height='1' border='0' alt=''></td>");
print ("<td valign='top'><img src='images/news_bottom_right.gif'
width='21' height='26' alt='' border='0'></td>");
print ("<td><img src='images/spacer.gif' width='7' height='1'
alt='' border='0'></td>");
print ("</tr>");
print ("</table>");
//stop news from showing more than two entries
$varOne = $varOne+1;
}
} else {
WHILE ($Row = mysql_fetch_array ($Result)){
print ("&var$Row[topic]=Sorry, the server did not respond. Please
refresh your browser or try again later.");
}
}