Link to home
Start Free TrialLog in
Avatar of czechmate1976
czechmate1976Flag for United Kingdom of Great Britain and Northern Ireland

asked on

word count returns result only for half of my query results

I have a script to select 5 latest articles and display its title, thumbnail image and 20 words of each  article content in a news column on the homepage, however only 2 of the query results display those 20 words of the article content. Either the first 3 or the last three do not display those 20 words of the article content depending if I ORDER BY date ASC (or DESC). Its very weird and I can't find what is the cause of the behaviour. I am using a function to shorten the article to the required 20 words (function 1 - below) and a code on the homepage to go through the query results and display those 5 shortened articles. Can anyone please help me to find the mystery? Thanks a lot to everyone.
function shorten_string($string, $wordsreturned) {
	//returns the first required number of words ($wordsreturned)
	//if string has less words than $wordsreturned the entire string is returned
	$retval = $string;
	
	$array = explode(" ", $string);
	if(count($array)<= $wordsreturned) { // short enough already
			$retval = $string;
	} else { // removing words over $wordsreturned
			array_splice($array, $wordsreturned);
			//echo $wordsreturned." ";
			$retval = implode(" ", $array)."...";
	}
	return $retval;
}
 
//code from the homepage
<?php
						$query = "SELECT * FROM tblnews ORDER BY created DESC LIMIT 5";
						$news_set = mysql_query($query, $connection);
						confirm_query($result);
						if(mysql_num_rows($news_set)>=1) {
							while ($article = mysql_fetch_array($news_set)) {
								echo "<div class=\"news-feed-column-box\">";
								echo "<a href=\"news.php?nid=".$article['nid']."\"><h4 class=\"news-feed-title\">".$article['newstitle']."</h4></a>";
								if(!empty($article['image_name'])) {
									$image = "../image_dir/".$article['image_name'];
									echo "<img src=\"includes/show_thumbnail.php?filename=".$image."&amp;width=45&height=30\" class=\"news-img\" />";
								}
								echo "<p class=\"news-feed\">".shorten_string($article['content'], 20)."</p>";
								echo "</div>";
							}
						}
					?>

Open in new window

Avatar of flob9
flob9
Flag of France image

perhaps you have multiple spaces ...

$array = explode(" ", ereg_replace(' +',' ',$string));
I afraid that some of your articles beak the HTML. Please generate HTML source and look at.
ASKER CERTIFIED SOLUTION
Avatar of flob9
flob9
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
For safe shortening replace:
$array = explode(" ", $string);
with
$array = explode(" ", strip_tags($string));
Avatar of czechmate1976

ASKER

The thing is why does it work for two out of five and not for all of them when its basically looping through .. if there was something totally wrong.. none of them would display, no? But I may be wrong.. I will still try some of these tips and very grateful, guys, thanks
flob9, thanks a lot!!! You hit the nail! Now, can you please explain to me why my simple statement worked only for 2 and this works for all of them. What exactly does it do? I am still a student and learning. It really helps if PRO's explain these things in plain words sometimes..
The ereg_replace removes duplicates spaces. The strip_tags remove html tags from the string.